Ryan Peters
Ryan Peters

Reputation: 7718

Escape special (HTML tag) characters in XML attribute?

As part of an XML node attribute, I need to pass up HTML characters as part of an attribute value, such as hello" />. I can't use CDATA as part of the value of the node, as lots of other systems use this method and I cannot afford to break or rewrite that process, so I'm stuck with this.

I can't HTML encode the values, as they're used inside of an email and are subsequently outputted literally as HTML encoded values (<br >hello, for example).

Is there a way to escape HTML (specifically, the < character) and allow me to keep un-encoded HTML inline as an attribute? Thanks.

Upvotes: 2

Views: 5142

Answers (1)

Joop Eggen
Joop Eggen

Reputation: 109613

The XML characters <>&" must be escaped identical to the HTML entities &lt; and so on. Using XML APIS will receive/store the original character. Other character entities in HTML should be converted to UTF-8. Numeric entities, hex (&#xFC;) and decimal (&#2285;) are simple, but for named entities (&bull;) one needs a Library. (If one wants to achieve completeness.)

Upvotes: 3

Related Questions