为模板规则指定临时文档而不是初始源文档

specify a temporary document instead of initial source document for a template rule

<xsl:template>的匹配属性定义了哪些节点 模板规则适用于。我认为,匹配的节点是隐含的来自初始源文档。

例如,这是我的 XSLT 模板的一部分:

<xsl:mode name="unroll" on-no-match="shallow-copy"/>
<xsl:template match="StructFormat[@repeat]" mode="unroll">
   ...
</xsl:template>
<xsl:variable name="complete-struct">
    <xsl:apply-templates mode="unroll"/>
</xsl:variable>

此模板处理初始源文件,结果保存在一个变量中。如何让此模板规则应用于 document() 函数加载的临时文档?我这样试过,但没有用:

<xsl:template match="/" mode="unroll">
    <xsl:apply-templates select="document('a.xml')/*"/>
</xsl:template>
<xsl:template match="@*|node()" mode="unroll">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

有全局变量的部分

<xsl:variable name="complete-struct">
    <xsl:apply-templates mode="unroll"/>
</xsl:variable>

构建一个变量处理全局上下文项的子节点(https://www.w3.org/TR/xslt-30/#dt-global-context-item)

您可以将其更改为

<xsl:variable name="complete-struct">
    <xsl:apply-templates select="doc('a.xml')/node()" mode="unroll"/>
</xsl:variable>

处理来自另一个文档的节点,或者如果您 运行 您的 XSLT 处理器及其 API 检查 where/how 您可以将该全局上下文项设置为您的特定文档,如果 needed/wanted(参见 http://saxonica.com/html/documentation/javadoc/net/sf/saxon/s9api/Xslt30Transformer.html#setGlobalContextItem-net.sf.saxon.s9api.XdmItem- 了解 Saxon 9.9)。

我认为你尝试添加

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

是错误的,您的初始代码有一个声明 <xsl:mode name="unroll" on-no-match="shallow-copy"/> 应该没问题,如果您想拼写出来,您需要

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