Working with HTML elements in XML

Usually XML and HTML meet rarely, but there are some cases when HTML elements must be contained within an XML document. This won't be a problem, since both languages derive from SGML and thus are mutually compatible. Instead, problems arise when you want to display such HTML elements with their default display role and with their attributes, such as style. Since XML treats these elements by default as anonymous elements, one solution would be using XSLT to transform HTML elements. But there's also another solution. Consider for example the case of RSS and Atom feeds. We have all noticed that these formats allow for the use of HTML elements inside their element, for example description or entry. How this can be done without XSLT?

This is possible thanks to the default XHTML namespace ( that can be specified on an element or more elements. When a browser encounters this namespace, it automatically binds the element(s) to the XHTML DTD. Browsers don't have a validating parser, so they don't read the DTD attached to a DOCTYPE declaration, but they're able to use namespaces to assign roles to elements. For example, if you have something like this:

<description xmlns="">

  <p>Some text.</p>


then the elements within description will be displayed with the default role assigned to normal h2 and p elements because they're both contained inside the XHTML namespace. This works because XHTML is basically XML, including the way browsers handle namespaces.

This entry was posted in by Gabriele Romanato. Bookmark the permalink.

Leave a Reply

Note: Only a member of this blog may post a comment.