Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a reason that we can't put an unescaped ampersand in XML

Tags:

xml

My customer wants to write my xml file as <name>Smith & Jones</name>, not <name>Smith &amp; Jones</name>.

I can't find a quality reference discussing this.

like image 518
David Thielen Avatar asked Nov 30 '25 04:11

David Thielen


2 Answers

From the XML specification (§2.4):

The ampersand character (&) and the left angle bracket (<) may appear in their literal form only when used as markup delimiters, or within a comment, a processing instruction, or a CDATA section. They are also legal within the literal entity value of an internal entity declaration; see "4.3.2 Well-Formed Parsed Entities". If they are needed elsewhere, they must be escaped using either numeric character references or the strings "&" and "<" respectively.

Since this circumstance fits into none of the stated categories, it is illegal.

like image 122
lonesomeday Avatar answered Dec 02 '25 19:12

lonesomeday


Use the CDDATA tag to insert these characters within the XML tags without XML parsing them:

<name>Smith & Jones</name>

becomes

<name><![CDATA[ Smith & Jones ]]></name>

this way you can also put plain html withing xml.

example: http://www.w3schools.com/xml/xml_cdata.asp

like image 45
Maurice Avatar answered Dec 02 '25 17:12

Maurice



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!