[英]XSL count number of nodes with parameter
I have the following xml : 我有以下xml:
<pax>
<age>5</age>
</pax>
<pax>
<age>5</age>
</pax>
<pax>
<age>12</age>
</pax>
And the follwoing xslt: 和下面的xslt:
<xsl:for-each select="paxes/pax">
<xsl:variable name="PaxAge" select="age"/>
<COUNT>
<xsl:value-of select='count(paxes/pax/age[text()=$PaxAge])'/>
</COUNT>
</xsl:for-each>
However the result I get in count is always 但是我得到的结果总是
<COUNT>0</COUNT>
<COUNT>0</COUNT>
<COUNT>0</COUNT>
If I change it to 如果我将其更改为
<xsl:value-of select='count(paxes/pax/age[text()="5"])'/>
I will receive 我会收到
<COUNT>2</COUNT>
<COUNT>2</COUNT>
<COUNT>2</COUNT>
The output should be: 输出应为:
<COUNT>2</COUNT>
<COUNT>1</COUNT>
How can I get the write count for each iterate? 如何获得每个迭代的写入计数?
Assuming you're trying to count the occurrences this should work, 假设您正在尝试计算应该发生的次数,
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="age" match="/paxes/pax/age" use="." />
<xsl:template match="/">
<xsl:for-each select="paxes/pax/age[generate-id(.) = generate-id(key('age', .)[1])]">
<COUNT>
<!-- optional -->
<xsl:attribute name="age">
<xsl:value-of select="."/>
</xsl:attribute>
<xsl:value-of select="count(key('age',.))"/>
</COUNT>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
should give an xml like: 应该给一个像这样的xml:
<COUNT age="5">2</COUNT>
<COUNT age="12">1</COUNT>
You say you want one COUNT element in the output for each distinct value of age. 您说您要在输出中为年龄的每个不同值分配一个COUNT元素。 That makes it a grouping query.
这使其成为一个分组查询。 In XSLT 2.0, you can therefore use the grouping query:
因此,在XSLT 2.0中,可以使用分组查询:
<xsl:for-each-group select="paxes/pax/age" group-by=".">
<COUNT><xsl:value-of select="count(current-group())"/></COUNT>
</xsl:for-each-group>
If you're stuck with XSLT 1.0, grouping queries are much tricker, but you can use the technnique of Muenchian grouping with keys, as illustrated in the answer from @Rnet. 如果您对XSLT 1.0感到困惑,则分组查询会更加棘手,但是您可以使用Muenchian密钥分组技术,如@Rnet的答案所示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.