0

我已经定义了一个变量 $NodeVariable,例如:

<xsl:variable name="NodeVariable">
    <aT>
        <aT2>foo</aT2>
        <aT3>bar</aT3>
    </aT>
</xsl:variable>

在代码的不同部分,我想将不同的模板“应用”到 myVariable。不幸的是,我不知道它的语法是什么。

我尝试了以下方法:

<xsl:for-each select="$NodeVariable"> 
    <xsl:call-template name="ns:ExtractInfo1"/>
</xsl:for-each>

<xsl:copy-of select="$NodeVariable"> 
    <xsl:call-template name="ns:ExtractInfo2"/>
</xsl:for-each>

<xsl:copy-of select="$NodeVariable"> 
    <xsl:call-template name="ns:ExtractInfo3"/>
</xsl:for-each>

这是行不通的。

如何将模板应用于树片段?

4

1 回答 1

2

假设您使用 XSLT 1.0 处理器,您需要先将结果树片段转换为节点集:

<xsl:variable name="NodeVariable">
    <aT>
        <aT2>foo</aT2>
        <aT3>bar</aT3>
    </aT>
</xsl:variable>

<xsl:variable name="NodeSet" select="exsl:node-set($NodeVariable)"/>

(样式表声明的地方xmlns:exsl="http://exslt.org/common"),然后您可以根据需要以不同的模式应用模板,例如

<xsl:apply-templates select="$NodeSet/aT" mode="m1"/>

并为该模式编写模板,例如

<xsl:template match="aT" mode="m1">
  <xsl:value-of select="aT2"/>
</xsl:template>

当然,如果您真的想调用命名模板,您也可以这样做,但在我看来,使用应用模板和模式进行不同的处理步骤是 XSLT 中的首选方式。

于 2014-07-03T08:41:55.290 回答