Best Practices for Using HTML / XHTML Content in XML Elements

Does anyone know what the best practices are or is there general advice about HTML / XHTML content in an XML element? Is it better to use CDATA or just HTML encode it HTML?

+2


source to share


4 answers


I would recommend CDATA; this will make XML more compact and human readable.

However, make sure you choose ]]>

how ]]>]]<![CDATA[

.



EDIT . As other people have said, if you are in control of the HTML you embed and you know it will always be valid XHTML, then you should embed it directly without leaving it.

However, if you are not in control of HTML, I do not recommend doing this. Even if it is valid now, it may become invalid one day, and you don't want your system to suddenly crash because of it. Obviously, this depends on the circumstances and the precedent; if you would like a more precise recommendation, please let us know in more detail.

+6


source


Option three: HTML, usually embedded in XML, is much more flexible than encoding it or embedding it in CDATA. This allows parsers to parse the entire document, including HTML, at a high level. It allows you to use XSL transformations on both containing XML and HTML data.



So, I suggest to directly embed it if your HTML is not valid XML, in which case encoding or CDATA would be the only option.

+2


source


Since a lot of HTML is not well-formed as XML (ie missing end tags such as </p>, </li>, and <br />), it may be less work to simply use a CDATA wrapper.

It depends on where you get the HTML from. If you create it yourself, you have complete control over its shape, but if you pull it from some other source (for example, by pulling it from some other website) you probably won't have the luxury of reformatting it not to be XHTML compliant.

+1


source


I would place the namespaced XHTML directly in the document (as opposed to "as strings", which is what your two suggested options suggest).

If you don't, then it doesn't matter which one of you uses,

-1


source







All Articles