我正在尝试使用Xalan 2.7.1在XSLT的结果中输出CDATA部分。我已经在工具中将此XSL应用于XML,结果包含CDATA。在下面的方法中,结果中没有CDATA,也没有抛出异常。我觉得我在这里错过了一些东西。
的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">
<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();
}
}
}
控制台输出
<?xml version="1.0" encoding="UTF-8"?><parentelem><newchildelem>Test text</newchildelem></parentelem>
答案 0 :(得分:1)
您是说要将CDATA作为元素的一部分输出?
<newchildelem><xsl:value-of select="/parentelem/childelem" /></newchildelem>
带
<newchildelem><xsl:text><![CDATA[
</xsl:text><xsl:value-of select="/parentelem/childelem" /><xsl:text>]]></xsl:text></newchildelem>
或其他形式,但使用转义字符省略
<newchildelem><![CDATA[Test text]]></newchildelem>
或者我是否误解了这个问题?
答案 1 :(得分:1)
它适用于我,使用Xalan 2.7.1,不确定它为什么不适合你。
我简化了代码片段,但我认为没有任何功能差异,但无论如何都要尝试:
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);
}
}
输出是:
Xalan Java 2.4.1
<?xml version="1.0" encoding="UTF-8"?>
<parentelem><newchildelem><![CDATA[Test text]]></newchildelem></parentelem>
奇怪的是,Xalan的Version.getVersion()返回2.4.1而不是2.7.1,而我肯定使用2.7.1。
答案 2 :(得分:0)
1)我猜结果我没有使用Xalan 2.7.1。 skaffman's回答中的代码让我考虑检查Version.getVersion(),签名是“XL TXE Java 1.0.7”。这在使用IBM java [IBM J9 VM(build 2.4,J2RE 1.6.0)]时似乎是默认值。
2)我从使用StAXSource和StAXResult切换到使用StreamSource和StreamResult,它工作正常(如在skaffman's回答中)。具体来说,从StAXResult到StreamResult的变化是有效的。在StreamResult中使用StAXSource也可以。