tags:

views:

1216

answers:

3

I'm trying to output a CDATA section in the result of XSLT using Xalan 2.7.1. I have applied this XSL to the XML in a tool and the result contains CDATA. In the method below, no CDATA is in the result and no exception is thrown. I feel like I'm missing something here.

test.xml

<?xml version="1.0" encoding="UTF-8"?>
<parentelem>
    <childelem>Test text</childelem>
</parentelem>

test.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
<xsl:output method="xml" encoding="UTF-8" cdata-section-elements="newchildelem" />
<xsl:template match="/">
<parentelem>
    <newchildelem><xsl:value-of select="/parentelem/childelem" /></newchildelem>
</parentelem>
</xsl:template>
</xsl:stylesheet>

Transform.java

import java.io.FileReader;
import java.io.StringWriter;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stax.StAXResult;
import javax.xml.transform.stax.StAXSource;

public class Transform {

    public static void main (String[] args){
    try {
        XMLStreamReader xmlReader = XMLInputFactory.newInstance().createXMLStreamReader(
   new FileReader("test.xml"));

  XMLStreamReader xslReader = XMLInputFactory.newInstance().createXMLStreamReader(
    new FileReader("test.xsl"));

  TransformerFactory transformerFactory = TransformerFactory.newInstance();
  Source xslSource = new StAXSource(xslReader);
  Source xmlSource = new StAXSource(xmlReader);
  Transformer transf = transformerFactory.newTransformer(xslSource);

  StringWriter xmlString = new StringWriter();
  XMLStreamWriter xmlWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(
    xmlString);

  Result transformedXml = new StAXResult(xmlWriter);
  transf.transform(xmlSource, transformedXml);

  xmlWriter.flush();
  System.out.println(xmlString.toString());
 } catch (Exception e) {
  e.printStackTrace();
 }
}
}

console output

<?xml version="1.0" encoding="UTF-8"?><parentelem><newchildelem>Test text</newchildelem></parentelem>
+1  A: 

Are you saying you want to output the CDATA as part of the element?

<newchildelem><xsl:value-of select="/parentelem/childelem" /></newchildelem>

with

<newchildelem><xsl:text>&lt;![CDATA[
</xsl:text><xsl:value-of select="/parentelem/childelem" /><xsl:text>]]&gt</xsl:text></newchildelem>

or some other form, but with the escaped characters to omit

<newchildelem><![CDATA[Test text]]></newchildelem>

or am I misunderstanding the question perhaps?

Mark Schultheiss
that is what i'd like to do and your solution works. i thought that i could use the cdata-section-elements here to achieve the same thing. using the cdata-section-elements approach works when i apply the above xsl to the above xml in an XML tool, i get the desired result. only when I use the above code to apply the xsl do i not get the expected result
hydeph
+1  A: 

It works for me, with Xalan 2.7.1, not sure why it doesn't work for you.

I simplified the code fragment, but I don't think there's any functional difference, but try it anyway:

import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.apache.xalan.Version;

public class Transform {

    public static void main(String[] args) throws Exception {
        System.out.println(Version.getVersion());
     Source xslSource = new StreamSource(Transform.class.getResourceAsStream("test.xsl"));
     Source xmlSource = new StreamSource(Transform.class.getResourceAsStream("test.xml"));

     Transformer transf = TransformerFactory.newInstance().newTransformer(xslSource);

     StreamResult transformedXml = new StreamResult(System.out);
     transf.transform(xmlSource, transformedXml);
    }
}

Output is:

Xalan Java 2.4.1
<?xml version="1.0" encoding="UTF-8"?>
<parentelem><newchildelem><![CDATA[Test text]]></newchildelem></parentelem>

What is odd is that Xalan's Version.getVersion() returns 2.4.1, not 2.7.1, and I'm definitely using 2.7.1 here.

skaffman
A: 

1) I guess it turns out i haven't been using Xalan 2.7.1. The code in skaffman's answer made me think to check Version.getVersion() and the signature is "XL TXE Java 1.0.7". This appears to be the default when using IBM java [IBM J9 VM (build 2.4, J2RE 1.6.0)].

2) I switched from using StAXSource and StAXResult to using StreamSource and StreamResult and it works fine (like in skaffman's answer). Specifically the change from StAXResult to StreamResult is what worked. Using StAXSource with StreamResult works too.

hydeph