In XML, Can CData exist alongside another node?

44 views Asked by At

Is this a valid XML?

<foo>
    <![CDATA[<bar>]]>
    <sibling></sibling>
</foo>

Can all XML Parsers parse this?

2

There are 2 answers

0
har07 On

CData alongside other node is fine, it doesn't break well-formed-ness of the XML document. Given that fact, all decent XML parser should be able to parse such structure.

From XML 1.0 specification :

[Definition: CDATA sections may occur anywhere character data may occur; they are used to escape blocks of text containing characters which would otherwise be recognized as markup. CDATA sections begin with the string " <![CDATA[ " and end with the string " ]]> ":].

0
Andreas Veithen On

That XML fragment is certainly well formed. Whether it is valid depends on the XML schema (or DTD) that you use to validate it.