XSL Tips - Disable-output-escaping

William Pohlhaus' Web Site

Home > XSL Tips > Disable-output-escaping


Side Links
Home
Resume
Jennifer (wedding picutes)
Seda (baby pictures)
Java
Presentation
XSL Tips
My Other Homepage (Dust Boy)
Summer 2003 Independent Studies
 
XSL disable-output-escaping
  This example was hugely helpful to me when converting xml to html because html does not follow the well-formness rules of xml and xsl. I know that xhtml does but old browser to not handle xhtml too well and when coding for the least common denominator this was very helpful so I included up here. Note that the &nbsp; is between both test most xsl compilers will not handle &nbsp; because it is not define by default. It is also helpful when doing html that does not comply with well-formness such-as <br> (I know that there are other ways around the <br> problem). The most help is when you are dealing with CDATA in your xml and do not know what state it is in, this allows for a quick shortcut but probably is not the best solution.
XML
<?xml version="1.0" encoding="US-ASCII" ?>

<test>
  <![CDATA[<b>test</b>&nbsp;<u>test</u>]]>
</test>

XSL
<?xml version="1.0" encoding="US-ASCII" ?>
  <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="html" omit-xml-declaration="no" indent="yes" />
    <xsl:template match="test">
      <xsl:value-of select="." disable-output-escaping="yes" />
      <xsl:text disable-output-escaping="yes">
        <![CDATA[<b>t&nbsp;t</b>]]>
      </xsl:text>

  </xsl:template>
</xsl:stylesheet>
Result
test test t t