You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xalan.apache.org by AW <an...@gmail.com> on 2008/08/20 00:32:38 UTC

Losing CDATA when parsing a String to XML

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.


RE: Losing CDATA when parsing a String to XML

Posted by ke...@us.ibm.com.
The XPath data model does not distinguish between <![CDATA[]]> markup and 
escaping individual characters, and in fact XML's semantics don't 
distinguish between them. 

Xalan does not promise to preserve that distinction. We've made some 
attempts to do so in the past simply as a courtesy to the users, but there 
are real limits on how much can be achieved and it probably isn't worth 
the effort.

A stylesheet can be told to output certain elements using  <![CDATA[]]> 
sections. See the documentation for <xsl:output>'s cdata-section-elements 
attribute. But my recommendation would be to fix/educate whatever/whoever 
is insisting on seeing these in the output.




______________________________________
"... Three things see no end: A loop with exit code done wrong,
A semaphore untested, And the change that comes along. ..."
  -- "Threes" Rev 1.1 - Duane Elms / Leslie Fish (
http://www.ovff.org/pegasus/songs/threes-rev-11.html)

RE: Losing CDATA when parsing a String to XML

Posted by AW <an...@gmail.com>.
Could you please tell me how to overcome this problem ? I want to retain the
CDATA tag.

Dave Brosius-2 wrote:
> 
> 
> A CDATA section is a kludge for the manual xml writer to allow for
> handling a bunch of problematic characters. In memory, there is no
> difference between a CDATA section and a text block, and therefore when
> parsing, parsers just return a string version.
> 
> 
> -----Original Message-----
> From: "AW" <an...@gmail.com>
> Sent: Tuesday, August 19, 2008 6:32pm
> To: xalan-j-users@xml.apache.org
> Subject: Losing CDATA when parsing a String to XML
> 
> 
> 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.
> 
> 
> 
> 
> 

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


RE: Losing CDATA when parsing a String to XML

Posted by ke...@us.ibm.com.
> I've an application wherein I allow the users to upload stylesheets. I
> receive the XSL as a string and hence convert to Xml using the Java
> extension I mentioned.  I won't have any prior knowledge of the elements
> that will contain CDATA sections.

CDATA sections in the stylesheet are always parsed down to ordinary text.

Stylesheet processing will discard the CDATA section distinction unless 
you reintroduce it via the directive in xsl:output.

That's normal XSLT operation.

RE: Losing CDATA when parsing a String to XML

Posted by AW <an...@gmail.com>.
I've an application wherein I allow the users to upload stylesheets. I
receive the XSL as a string and hence convert to Xml using the Java
extension I mentioned.  I won't have any prior knowledge of the elements
that will contain CDATA sections.


Dave Brosius-2 wrote:
> 
> Not sure i follow what you are doing, but I think you can use
> OutputKeys.CDATA_SECTION_ELEMENTS to specify for what elements, child text
> nodes should be output as cdata sections when you transform a document
> back out. This probably doesn't have the granularity that you want,
> however, and i can't say I ever tried using it.
> 
> 
> 
> -----Original Message-----
> From: "AW" <an...@gmail.com>
> Sent: Tuesday, August 19, 2008 6:47pm
> To: xalan-j-users@xml.apache.org
> Subject: RE: Losing CDATA when parsing a String to XML
> 
> 
> Could you please tell me how to overcome this problem ? I want to retain
> the
> CDATA tag.
> 
> Dave Brosius-2 wrote:
>> 
>> 
>> A CDATA section is a kludge for the manual xml writer to allow for
>> handling a bunch of problematic characters. In memory, there is no
>> difference between a CDATA section and a text block, and therefore when
>> parsing, parsers just return a string version.
>> 
>> 
>> -----Original Message-----
>> From: "AW" <an...@gmail.com>
>> Sent: Tuesday, August 19, 2008 6:32pm
>> To: xalan-j-users@xml.apache.org
>> Subject: Losing CDATA when parsing a String to XML
>> 
>> 
>> 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.
>> 
>> 
>> 
>> 
>> 
> 
> -- 
> View this message in context:
> http://www.nabble.com/Losing-CDATA-when-parsing-a-String-to-XML-tp19060000p19060170.html
> Sent from the Xalan - J - Users mailing list archive at Nabble.com.
> 
> 
> 
> 
> 

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