【问题标题】:XSLT Refer to the current node of a template within for-eachXSLT 在 for-each 中引用模板的当前节点
【发布时间】:2018-08-30 17:44:53
【问题描述】:

如何在for-each 循环中引用模板匹配的当前节点。

<xsl:template match="product[@category='foo']">
    <count>
        <xsl:for-each select="preceding::product">
                <current>
                    <xsl:value-of select="count(current()/preceding::product)"/>
                </current>
                <context>
                    <xsl:value-of select="count(./preceding::product)"/>
                </context>
        </xsl:for-each>
    </count>
</xsl:template>

count().current() 函数返回的结果相同。

有没有办法引用模板的匹配节点,以便计算它的所有前面的节点,以便&lt;current/&gt;内部的count()函数的结果对于每个for-each都是相同的一步。

谢谢!

【问题讨论】:

    标签: xslt


    【解决方案1】:

    将当前节点放入一个变量中......

    <xsl:template match="product[@category='foo']">
        <xsl:variable name="current" select="." />
        <count>
            <xsl:for-each select="preceding::product">
                    <current>
                        <xsl:value-of select="count($current/preceding::product)"/>
                    </current>
                    <context>
                        <xsl:value-of select="count(./preceding::product)"/>
                    </context>
            </xsl:for-each>
        </count>
    </xsl:template>
    

    不过,在这个例子中,你最好将计数结果放在一个变量中......

    将当前节点放入一个变量中......

    <xsl:template match="product[@category='foo']">
        <xsl:variable name="count" select="count(preceding::product)" />
        <count>
            <xsl:for-each select="preceding::product">
                    <current>
                        <xsl:value-of select="$count"/>
                    </current>
                    <context>
                        <xsl:value-of select="count(./preceding::product)"/>
                    </context>
            </xsl:for-each>
        </count>
    </xsl:template>
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-02
    • 1970-01-01
    相关资源
    最近更新 更多