Including HTML data in an xml document
When developing an xml-based application, you may come across the need to include HTML data in xml nodes. The problem is that unless this data is escaped in some way, it is interpreted as xml and will cause a malformed structure. The easiest way I’ve found so far to use a CDATA attribute to escape the code automatically. Then, when you are creating an xsl fragment to edit or display the data, use
<xsl:value-of select="../excerpt" disable-output-escaping="yes"/&rt;Example of xml which contains HTML data:
<?xml version="1.0" encoding="UTF-8"?>
<blog_entries>
<blog_entry id="0">
<date>09/01/2007</date>
<title>Entry Title</title>
<excerpt>
<![CDATA[
This is an excerpt that contains some HTML data. <a href="http://google.com" target="_blank">testing URL's in cdata section</a>
]]>
</excerpt>
<text>This is the rest of the article and contains some HTML code</text>
<author>Author name</author>
</blog_entry>
<blog_entry id="1">
<date>09/01/2007</date>
<title>Entry Title 2</title>
<excerpt>
<![CDATA[
Short excerpt here.<a href="http://google.com" target="_blank">testing URL's in cdata section</a>
]]>
</excerpt>
<text>
<![CDATA[ Longer excerpt here<a href="http://google.com" target="_blank">testing URL's in cdata section</a> ]]>
</text>
<author>another author</author>
</blog_entry>
</blog_entries>
Example of XSL fragment which displays the data:
<?xml version="1.0" encoding="UTF-8"?>
<!-- DWXMLSource="blog.xml" -->
<!DOCTYPE xsl:stylesheet [
<!ENTITY nbsp " ">
<!ENTITY copy "©">
<!ENTITY reg "®">
<!ENTITY trade "™">
<!ENTITY mdash "—">
<!ENTITY ldquo "“">
<!ENTITY rdquo "”">
<!ENTITY pound "£">
<!ENTITY yen "¥">
<!ENTITY euro "€">
]>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="html" encoding="UTF-8"/>
<xsl:template match="/">
<style type="text/css">
<xsl:comment>
.blogText{font:11px Verdana, Arial, Helvetica;}</xsl:comment>
</style><xsl:for-each select="blog_entries/blog_entry/date">
<div align="left" class="blogText">
<xsl:value-of select="."/><br />
<b><xsl:value-of select="../title"/></b><br />
<xsl:value-of select="../excerpt" disable-output-escaping="yes"/><br />
<xsl:value-of select="../text" disable-output-escaping="yes"/><br />
<xsl:value-of select="../author"/>
</div>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
—-