Including HTML data in an xml document
September 04, 2007
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
Ads by Google

Posted by ellen at September 04, 2007 04:26 PM
<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_entry id="0">
<title>Entry Title</title>
This is an excerpt that contains some HTML data. <a href="" target="_blank">testing URL's in cdata section</a>
<text>This is the rest of the article and contains some HTML code</text>
<author>Author name</author>
<blog_entry id="1">
<title>Entry Title 2</title>
Short excerpt here.<a href="" target="_blank">testing URL's in cdata section</a>
<![CDATA[ Longer excerpt here<a href="" target="_blank">testing URL's in cdata section</a> ]]>
<author>another author</author>

Example of XSL fragment which displays the data:

<?xml version="1.0" encoding="UTF-8"?>

<!-- DWXMLSource="blog.xml" -->
<!DOCTYPE xsl:stylesheet [
<!ENTITY nbsp "&#160;">
<!ENTITY copy "&#169;">
<!ENTITY reg "&#174;">
<!ENTITY trade "&#8482;">
<!ENTITY mdash "&#8212;">
<!ENTITY ldquo "&#8220;">
<!ENTITY rdquo "&#8221;">
<!ENTITY pound "&#163;">
<!ENTITY yen "&#165;">
<!ENTITY euro "&#8364;">
<xsl:stylesheet version="1.0" xmlns:xsl="">

<xsl:output method="html" encoding="UTF-8"/>
<xsl:template match="/">
<style type="text/css">
.blogText{font:11px Verdana, Arial, Helvetica;}


<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"/>

For more information on this topic see:
  • How To Display HTML in XSL Style Sheet

  • Ads by Google

    Ads by Google

     RSS   |   Contact Me

    Ads by Google