使用 if 条件更改“<Output>”元素

Change in `<Output>' element with if condition

我有两个输入文件,例如:

a.xml

<a>
   <data>...</data>
</a>

b.xml

<b>
   <data>...</data>
</b>

我的 xslt

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 version="2.0">
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

我想检查第一个根元素,如果它是 <a>,那么 <xsl:output> 就是

<xsl:output method="xml" encoding="UTF-8" indent="yes"/>

其他

<xsl:output method="xml" encoding="us-ascii" indent="no"/>

提前致谢。

嗯,XSLT 是 XML,因此您当然可以使用 XSLT 创建另一个具有所需输出编码的 XSLT,然后您可以 运行 在单独的步骤中创建它。

作为替代方案,您可以检查根元素不是 a 元素,然后将输出委托给 xsl:result-document,您可以在其中更改 encoding:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="document-node()[*[not(self::a)]]">
        <xsl:result-document encoding="US-ASCII">
            <xsl:apply-templates/>
        </xsl:result-document>
    </xsl:template>

</xsl:stylesheet>

http://xsltfiddle.liberty-development.net/3Nqn5Y8