Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have HTML text or CDATA inside an XML attribute?

Tags:

html

xml

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?

like image 518
Boon Avatar asked Aug 17 '09 18:08

Boon


People also ask

Can we use CDATA in XML attribute?

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).

Can you use CDATA in HTML?

Note that CDATA sections should not be used within HTML; they only work in XML.

Which of the character is not allowed in XML attribute?

Note that the ampersand (&) and less-than (<) characters are not permitted in XML attribute values.

How do I add CDATA to XML?

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 &.


2 Answers

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.

like image 55
JMP Avatar answered Sep 28 '22 07:09

JMP


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="&lt;a href=&quot;/&quot;&gt;"> 

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.

like image 31
John Kugelman Avatar answered Sep 28 '22 07:09

John Kugelman