gpt4 book ai didi

xml - 获取 XSLT for-each 循环中元素(迭代)的总数

转载 作者:数据小太阳 更新时间:2023-10-29 02:00:49 29 4
gpt4 key购买 nike

我通常使用 jquery 模板来处理这类事情,但我继承了一个需要更新的 XSLT 文件,但我找不到获取特定模板调用的元素(迭代)总数的方法。

使用 jquery 模板我会做这样的事情,它会给我循环遍历的 Assets 总数。

<span id="${spnID}">
${GroupName} (${Assets.length})
</span>

如果循环中有五个元素,这将返回“Product x (5)”。

这看起来很简单,但我似乎找不到用 XSLT 做同样事情的方法。我认为是这样的:

<span id="{$SpnId}">
<xsl:value-of select="$GroupName"/> (<xsl:value-of select="$total-number-of-elements"/>)
</span>

最佳答案

如果您正在遍历某些 $set,则输出 count($set) 以获得要迭代的项目总数。例如,试试这个样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:template match="/">
<xsl:variable name="set" select="/table/row" />
<xsl:variable name="count" select="count($set)" />
<xsl:for-each select="$set">
<xsl:value-of select="concat(position(), ' of ', $count, '&#xa;')" />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

针对这个输入:

<table>
<row id="1" />
<row id="2" />
<row id="3" />
<row id="4" />
<row id="5" />
<row id="6" />
</table>

输出:

1 of 6
2 of 6
3 of 6
4 of 6
5 of 6
6 of 6

请注意,我们正在遍历 /table/row 选择的节点并输出 count(/table/row) 以获得迭代次数。

关于xml - 获取 XSLT for-each 循环中元素(迭代)的总数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8659545/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com