Re: [xsl] amp and &

Subject: Re: [xsl] amp and &
From: Mike Brown <mike@xxxxxxxx>
Date: Thu, 26 Dec 2002 22:44:31 -0700 (MST)
Nikhil G. Daddikar wrote:
> That doesn't work
> 
> & inside a CDATA still gets converted to &amp;

CDATA sections are for telling an XML parser that the text inside them is not
to be interpreted as markup. There's no difference between these:

  <![CDATA[foo & bar]]>
  
  foo &amp; bar

Both mean the string of character data "foo & bar", and this is what the XML
parser reports to the XSLT processor, and is what ends up in the text node or
attribute node in the XPath/XSLT trees. When these trees are serialized as
HTML or XML, "&" and "<" are escaped automatically in order to prevent them
from being misinterpreted as markup.

Don't worry about the input. An XSLT processor, when using the html output
method, is supposed to leave the content of a <script> element unescaped. If
your XSLT processor is not doing that, tell us which one it is and report a
bug to the vendor. Seems to work for me:


cdata.xml
=========
<data>document.write('&lt;p&gt;foo &amp; bar&lt;/p&gt;');</data>


cdata.xsl
=========
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

  <xsl:output method="html" indent="yes" encoding="iso-8859-1"/>

  <xsl:template match="/">
    <html>
      <head>
        <title>test</title>
      </head>
      <body>
        <h1>test</h1>
        <script type="text/javascript">
          <xsl:value-of select="data"/>
        </script>
      </body>
    </html>
  </xsl:template>

</xsl:stylesheet>


result of running with 4 different XSLT processors
==================================================

# saxon cdata.xml cdata.xsl
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
   
      <title>test</title>
   </head>
   <body>
      <h1>test</h1><script type="text/javascript">document.write('<p>foo & bar</p>');</script></body>
</html>


# 4xslt cdata.xml cdata.xsl
<html>
  <head>
    <meta content='text/html; charset=iso-8859-1' http-equiv='Content-Type'>
    <title>test</title>
  </head>
  <body>
    <h1>test</h1>
    <script type='text/javascript'>document.write('<p>foo & bar</p>');</script>
  </body>
</html>


# java org.apache.xalan.xslt.Process -IN test/cdata.xml -XSL test/cdata.xsl
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>test</title>
</head>
<body>
<h1>test</h1>
<script type="text/javascript">document.write('<p>foo & bar</p>');</script>
</body>
</html>


#C:\> msxsl cdata.xml cdata.xsl
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>test</title>
</head>
<body>
<h1>test</h1><script type="text/javascript">document.write('<p>foo & bar</p>');</script></body>
</html>


Mike

-- 
  Mike J. Brown   |  http://skew.org/~mike/resume/
  Denver, CO, USA |  http://skew.org/xml/

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread