简体   繁体   中英

Should RegEx be stored in XML as CDATA or as Attributes?

I am trying to output a RegEx in an xml file as an Attribute.

The problem is that the RegEx generated in the XML output is different than the one i have in Database:

-- database
[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@([a-z0-9_-]+\.)+(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum|[a-z]{2})

-- generated
[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@([a-z0-9_-]+\.)+(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum|[a-z]{2})

Should i save the RegEx as a <![CDATA[ regex ]]> or is this just the default behavior of xml files (encoding some characters and decode them automatically when i read it back) ?

This is the code i have to generate the element:

XElement attr =
    new XElement("Attribute",
        new XAttribute("RegEx", item.RegularExpression)
    );

And this is what it generates:

<Attribute RegEx="[a-z0-9!#$%&amp;amp;&amp;apos;*+\/=?^_`{|}~-]+(\.[a-z0-9!#$%&amp;amp;&amp;apos;*+\/=?^_`{|}~-]+)*@([a-z0-9_-]+\.)+(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum|[a-z]{2})" />

The problem is that the regex in the database already uses XML escaping, and when copying it into the XML file, you have added another layer of escaping, so &apos; has become &amp;apos; .

There are two ways of escaping special characters, you can turn & into &amp; as done here, or you can wrap it in CDATA. In this case you don't want to do either, because it is already escaped.

I'm not familiar with link-to-xml so I don't know how to do this correctly in that environment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM