Do I need to replace double / single quote in XML text?

Please correct my terminology here if it is turned off:

5 character substitutions for XML:

  • & amp; (and)
  • & l; (<)
  • & Gt; (>)
  • "(")
  • & APOS; (')

Should all these substitutions be done in the text of the element? Or just the attribute text? (terminological correction?)

eg. Is this valid XML?

<myelement>x && y</myelement> <myelement>And I quote, "no"</myelement> 

&gt; and &lt; seem obvious for substitution in this context, but I don’t understand whether the substitution rules are global for the whole XML document or whether they apply differently to different parts of the document (for example, cdata sections apply different rules).

Assumption: this is invalid XML:

 <myelement field="no & allowed here"/> <myelement field="no <> allowed here"/> 

Quotations are obvious attribute separators, and <> are obvious element text separators.

+6
source share
1 answer

In the element content you only need to run & and < ; you never need to avoid single or double quotes, and you need to exit > only if it appears as part of the sequence ]]> (many people replace it unconditionally because it is easier).

In the attribute contents you only need to exit & and < and either ' , or " , depending on which one was used as the attribute separator.

Objects starting with & are not recognized in comment or CDATA sections or in element or attribute names, therefore special characters should not be escaped in these contexts.

+10
source

Source: https://habr.com/ru/post/970199/


All Articles