xml 如何计算节点中的不同值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/153156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to count distinct values in a node?
提问by Daniel Silveira
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>
回答by JeniT
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:
然后您可以使用以下方法获取<Artist_by_Country>具有不同国家/地区的元素:
/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))
回答by samjudson
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 中的元素越多,所需的时间就越长,因为它会检查每个元素的每个前面的同级元素。
回答by Chris Marasti-Georg
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 followingaxis.
IMO 表达式的有趣之处在于以下轴。
You could probably also remove the first /text(), and replace the second with .
您也可以删除第一个/text(),然后将第二个替换为.
回答by Nick Allen
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']" />

