How to count distinct values in a node?

醉酒当歌 提交于 2019-11-28 21:30:39

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:

<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.

Of course in XSLT 2.0, it's as simple as

count(distinct-values(/Artists_by_Countries/Artist_by_Country/Country))

In XSLT 1.0 this isn't obvious, but the following should give you an idea of the requirement:

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.

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.

You could probably also remove the first /text(), and replace the second with .

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.

You could then do

<xsl:for-each select="Artists_by_Countries/Artist_by_Country/Country[@distinct='true']" />
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!