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" <[EMAIL PROTECTED]> > 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" <[EMAIL PROTECTED]> >> 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.