I keep getting "XML parser failure: Unterminated attribute" with my parser when I attempt to put HTML text or CDATA inside my XML attribute. Is there a way to do this or is this not allowed by the standard?
In a Document Type Definition (DTD) an attributes type can be set to be CDATA. The resulting attribute within an XML document may contain arbitrary character data. So basically the DTD CDATA type is a string type with no restrictions placed on it, it can contain any textual data (as long as its suitably escaped).
Note that CDATA sections should not be used within HTML; they only work in XML.
Note that the ampersand (&) and less-than (<) characters are not permitted in XML attribute values.
CDATA sections can appear inside element content and allow < and & character literals to appear. A CDATA section begins with the character sequence <! [CDATA[ and ends with the character sequence ]]>. Between the two character sequences, an XML processor ignores all markup characters such as <, >, and &.
No, The markup denoting a CDATA Section is not permitted as the value of an attribute.
According to the specification, this prohibition is indirect rather than direct. The spec says that the Attribute value must not have an open angle bracket. Open angle brackets and ampersand must be escaped. Therefore you cannot insert a CDATA section. womp womp.
A CData Section is interpreted only when it is in a text node of an element.
Attributes can only have plain text inside, no tags, comments, or other structured data. You need to escape any special characters by using character entities. For example:
<code text="<a href="/">">
That would give the text
attribute the value <a href="/">
. Note that this is just plain text so if you wanted to treat it as HTML you'd have to run that string through an HTML parser yourself. The XML DOM wouldn't parse the text
attribute for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With