I'm using Xalan 2.7.1. I've written the following Java extension to transform
a string into XML. My problem is the variable in my XSLT (xmlNode) which
received the XML loses the CDATA tag. I'm not sure why Cdata section gets
removed. 
 
public Node ToXml(String xmlString)
    {
        DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        Document document = null;

        try
        {
            DocumentBuilder builder = factory.newDocumentBuilder();

            document = builder.parse(new InputSource(new
StringReader(xmlString)));
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
        if (document != null)
            return document.getDocumentElement();
        else
            return null;
}

XSLT:
<xsl:variable name="xmlConverter" select="conv:new()" />                
<xsl:variable name="xmlNode"
select="conv:ToXml($xmlConverter,./docXSLT/text())"/>  
<XSLTdoc>
        <xsl:copy-of select="$xmlNode"/>
</XSLTdoc>


Input:

<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0">
<xsl:template match="/">
  <html>
  <body>
    <h2>My CD Collection</h2>
    <table border="1">
    <tr bgcolor="#9acd32">
      <th align="left">Title</th>
      <th align="left">Artist</th>
    </tr>
    <xsl:for-each select="catalog/cd">
    <tr>
      <td><xsl:value-of select="title"/></td>
      <td><xsl:value-of select="artist"/></td>
      <![CDATA[" and ends with "]]>
    </tr>
    </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

Output:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">
<xsl:template match="/">
  <html>
  <body>
    <h2>My CD Collection</h2>
    <table border="1">
    <tr bgcolor="#9acd32">
      <th align="left">Title</th>
      <th align="left">Artist</th>
    </tr>
    <xsl:for-each select="catalog/cd">
    <tr>
      <td>
<xsl:value-of select="title"/>
</td>
      <td>
<xsl:value-of select="artist"/>
</td>
      " and ends with "    (The Cdata section is no longer there)
    </tr>
    </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

Could you please help me with this ?

-- 
View this message in context: 
http://www.nabble.com/Losing-CDATA-when-parsing-a-String-to-XML-tp19060000p19060000.html
Sent from the Xalan - J - Users mailing list archive at Nabble.com.

Reply via email to