How to count distinct values in a node in XSLT?
如何计算XSLT中节点中的不同值?
Example: I want to count the number of existing countries in Country nodes, in this case, it would be 3.
示例:我想计算Country节点中现有国家/地区的数量,在这种情况下,它将为3。
<Artists_by_Countries>
<Artist_by_Country>
<Location_ID>62</Location_ID>
<Artist_ID>212</Artist_ID>
<Country>Argentina</Country>
</Artist_by_Country>
<Artist_by_Country>
<Location_ID>4</Location_ID>
<Artist_ID>108</Artist_ID>
<Country>Australia</Country>
</Artist_by_Country>
<Artist_by_Country>
<Location_ID>4</Location_ID>
<Artist_ID>111</Artist_ID>
<Country>Australia</Country>
</Artist_by_Country>
<Artist_by_Country>
<Location_ID>12</Location_ID>
<Artist_ID>78</Artist_ID>
<Country>Germany</Country>
</Artist_by_Country>
</Artists_by_Countries>
4 个解决方案
#1
25
If you have a large document, you probably want to use the "Muenchian Method", which is usually used for grouping, to identify the distinct nodes. Declare a key that indexes the things you want to count by the values that are distinct:
如果您有一个大文档,您可能希望使用通常用于分组的“Muenchian方法”来识别不同的节点。声明一个键,用于通过不同的值索引要计数的内容:
<xsl:key name="artists-by-country" match="Artist_by_Country" use="Country" />
Then you can get the <Artist_by_Country>
elements that have distinct countries using:
然后,您可以使用以下方式获取具有不同国家/地区的
/Artists_by_Countries
/Artist_by_Country
[generate-id(.) =
generate-id(key('artists-by-country', Country)[1])]
and you can count them by wrapping that in a call to the count()
function.
你可以通过在count()函数的调用中包装它来计算它们。
Of course in XSLT 2.0, it's as simple as
当然在XSLT 2.0中,它就像它一样简单
count(distinct-values(/Artists_by_Countries/Artist_by_Country/Country))
#2
6
In XSLT 1.0 this isn't obvious, but the following should give you an idea of the requirement:
在XSLT 1.0中,这并不明显,但以下内容应该让您了解该要求:
count(//Artist_by_Country[not(Location_ID=preceding-sibling::Artist_by_Country/Location_ID)]/Location_ID)
The more elements in your XML the longer this takes, as it checks every single preceding sibling of every single element.
XML中的元素越多,所需的时间就越长,因为它会检查每个元素的每个前面的兄弟元素。
#3
3
Try something like this:
尝试这样的事情:
count(//Country[not(following::Country/text() = text())])
"Give me the count of all Country nodes without a following Country with matching text"
“给我所有国家节点的计数,没有跟随国家和匹配的文本”
The interesting bit of that expression, IMO, is the following axis.
该表达式的有趣位IMO是跟随轴。
You could probably also remove the first /text()
, and replace the second with .
您也可以删除第一个/ text(),并用第二个替换。
#4
0
If you have control of the xml generation on the first occurence of a country you could add an attribute to the country node such as distinct='true' flag the country as "used" and not subsequently add the distinct attribute if you come across that country again.
如果您在国家的第一次出现时控制了xml生成,则可以向国家/地区节点添加属性,例如distinct ='true'将该国家标记为“已使用”,如果遇到该国家,则不会添加该属性国家了。
You could then do
那你可以做
<xsl:for-each select="Artists_by_Countries/Artist_by_Country/Country[@distinct='true']" />
#1
25
If you have a large document, you probably want to use the "Muenchian Method", which is usually used for grouping, to identify the distinct nodes. Declare a key that indexes the things you want to count by the values that are distinct:
如果您有一个大文档,您可能希望使用通常用于分组的“Muenchian方法”来识别不同的节点。声明一个键,用于通过不同的值索引要计数的内容:
<xsl:key name="artists-by-country" match="Artist_by_Country" use="Country" />
Then you can get the <Artist_by_Country>
elements that have distinct countries using:
然后,您可以使用以下方式获取具有不同国家/地区的
/Artists_by_Countries
/Artist_by_Country
[generate-id(.) =
generate-id(key('artists-by-country', Country)[1])]
and you can count them by wrapping that in a call to the count()
function.
你可以通过在count()函数的调用中包装它来计算它们。
Of course in XSLT 2.0, it's as simple as
当然在XSLT 2.0中,它就像它一样简单
count(distinct-values(/Artists_by_Countries/Artist_by_Country/Country))
#2
6
In XSLT 1.0 this isn't obvious, but the following should give you an idea of the requirement:
在XSLT 1.0中,这并不明显,但以下内容应该让您了解该要求:
count(//Artist_by_Country[not(Location_ID=preceding-sibling::Artist_by_Country/Location_ID)]/Location_ID)
The more elements in your XML the longer this takes, as it checks every single preceding sibling of every single element.
XML中的元素越多,所需的时间就越长,因为它会检查每个元素的每个前面的兄弟元素。
#3
3
Try something like this:
尝试这样的事情:
count(//Country[not(following::Country/text() = text())])
"Give me the count of all Country nodes without a following Country with matching text"
“给我所有国家节点的计数,没有跟随国家和匹配的文本”
The interesting bit of that expression, IMO, is the following axis.
该表达式的有趣位IMO是跟随轴。
You could probably also remove the first /text()
, and replace the second with .
您也可以删除第一个/ text(),并用第二个替换。
#4
0
If you have control of the xml generation on the first occurence of a country you could add an attribute to the country node such as distinct='true' flag the country as "used" and not subsequently add the distinct attribute if you come across that country again.
如果您在国家的第一次出现时控制了xml生成,则可以向国家/地区节点添加属性,例如distinct ='true'将该国家标记为“已使用”,如果遇到该国家,则不会添加该属性国家了。
You could then do
那你可以做
<xsl:for-each select="Artists_by_Countries/Artist_by_Country/Country[@distinct='true']" />