从 XSLT 中排除处理指令

Exclude processing instruction from XSLT

我有一个简单的纯文本生成 XSLT,我正在像这样应用它(使用参考实现):

    StreamSource schemasource = new StreamSource(getClass().getResourceAsStream("source.xml"));
    StreamSource stylesource = new StreamSource(getClass().getResourceAsStream("transform.xslt"));
    Transformer transformer = TransformerFactory.newInstance().newTransformer(stylesource);

    StringWriter writer = new StringWriter();
    transformer.transform(schemasource, new StreamResult(domWriter));
    System.out.println("XML after transform:");
    System.out.println(writer.toString());

"transform" 调用总是在输出中插入处理指令。有什么方法可以配置它不这样做吗?

(例如对于一个非常简单的节点身份转换

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xsl:template match="*">
        <xsl:copy/>
        <xsl:apply-templates/>
    </xsl:template>
</xsl:stylesheet>

应用于

<hello/>

我明白了

<?xml version="1.0" encoding="UTF-8"?>
<hello/>

)

非常感谢,安迪

严格来说<?xml声明不是处理指令。但是,您应该可以在此处使用 xsl:output 命令来指定不应输出 xml 声明:

<xsl:output method="xml" omit-xml-declaration="yes"  indent="yes" />

这应该作为 xsl:stylesheet 元素的直接子元素放置。