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

时间:2022-10-18 19:15:15

I normally use jquery templates for this sort of thing but I inherited an XSLT file I need to update but I can't find a way to get the total number of elements (iterations) for a specific template call.

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

With jquery templates I'd do something like this and it would give me total number of assets that I'm looping through.

使用jquery模板,我会做这样的事情,它会给我我正在循环的资产总数。

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

This would return me "Product x (5)" if there were five elements in the loop.

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

It seems simple but I can't seem to find a way to do the same thing with XSLT. Something like this, I think:

这似乎很简单,但我似乎无法找到一种方法来使用XSLT做同样的事情。我认为这样的事情:

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

2 个解决方案

#1


8  

If you're looping over some $set then output count($set) to get the total number of items to be iterated. For example, try this stylesheet:

如果你循环一些$ set然后输出计数($ 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>

Against this input:

反对这一输入:

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

Output:

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

Note that we're looping over the nodes selected by /table/row and outputting count(/table/row) to get the number of iterations.

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

#2


1  

Wayne's answer works, and may be necessary in certain circumstances, when there are other requirements that must be met. But, if you've got a simple situation, you can do it more efficiently by using the Last() function. As soon as the for-each is processed, Last() contains the upper bound, or count, of the set.

Wayne的答案是有效的,并且在某些情况下可能是必要的,当必须满足其他要求时。但是,如果你有一个简单的情况,你可以使用Last()函数更有效地完成它。处理for-each后,Last()包含集合的上限或计数。

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

Run against the same XML, the output is identical to Wayne's result.

运行相同的XML,输出与Wayne的结果相同。

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

#1


8  

If you're looping over some $set then output count($set) to get the total number of items to be iterated. For example, try this stylesheet:

如果你循环一些$ set然后输出计数($ 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>

Against this input:

反对这一输入:

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

Output:

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

Note that we're looping over the nodes selected by /table/row and outputting count(/table/row) to get the number of iterations.

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

#2


1  

Wayne's answer works, and may be necessary in certain circumstances, when there are other requirements that must be met. But, if you've got a simple situation, you can do it more efficiently by using the Last() function. As soon as the for-each is processed, Last() contains the upper bound, or count, of the set.

Wayne的答案是有效的,并且在某些情况下可能是必要的,当必须满足其他要求时。但是,如果你有一个简单的情况,你可以使用Last()函数更有效地完成它。处理for-each后,Last()包含集合的上限或计数。

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

Run against the same XML, the output is identical to Wayne's result.

运行相同的XML,输出与Wayne的结果相同。

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