Antoine
Antoine

Reputation: 5245

distinct in Xpath?

I have this XML file, from which I'd like to count the number of users referenced in it. But they can appear in more than one category, and I'd like these duplicates not to be taken into account.
In the example below, the query should return 3 and not 4. Is there a way in XPath to do so? Users are not sorted at all.

<list>
  <group name='QA'>
    <user name='name1'>name1@email</user>
    <user name='name2'>name2@email</user>
  </group>
  <group name='DEV'>
    <user name='name3'>name3@email</user>
    <user name='name2'>name2@email</user>
  </group>
</list>

Upvotes: 50

Views: 59721

Answers (6)

xmen-5
xmen-5

Reputation: 1906

count(//user[not(./@name = preceding::user/@name)])

I think the best way is to try to draw your xml data on paper to see how you can solve it easily

Upvotes: 0

Mitchel Sellers
Mitchel Sellers

Reputation: 63126

You will need to use two functions like this.

count(distinct-values(//list/group/user))

First get the distinct values, then count them

Upvotes: 0

Raghavendra
Raghavendra

Reputation: 3580

I have a better answer

count(//user[not(. = following::user/.)])

Upvotes: 4

gingerbreadboy
gingerbreadboy

Reputation: 7769

using the functions namespace http://www.w3.org/2005/xpath-functions you can use

distinct-values(//list/group/user)

UPDATE:

At the top of your xsl/xslt file you should have a stylesheet element, map the url above to the prefix fn as below...

<xsl:stylesheet version="1.0"
 xmlns:fn="http://www.w3.org/2005/xpath-functions"
 >

then you can use

select="fn:distinct-values(//list/group/user)"

this would assume you are doing this in templates and not in some xpathdocument object inwhich case you need to use a namespacemanager class.

links...

XSLT: Add namespace to root element

http://www.xqueryfunctions.com/xq/fn_distinct-values.html

http://msdn.microsoft.com/en-us/library/d6730bwt(VS.80).aspx

Otherwise try Dimitre Novatchev's answer.

Upvotes: 25

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243449

A pure XPath 1.0 -- one-liner:

Use:

count(/*/group/user[not(. = ../following-sibling::group/user)])

Upvotes: 35

JSprang
JSprang

Reputation: 12909

Not sure if you could do it in XPath, but it could be done easily using System.Linq:

string xml = "<list><group name='QA'><user name='name1'>name1@email</user><user name='name2'>name2@email</user></group><group name='DEV'><user name='name3'>name3@email</user><user name='name2'>name2@email</user></group></list>";
        XElement xe = XElement.Parse(xml);
        int distinctCount = xe.Elements().Elements().Select(n => n.Value).Distinct().Count();

In this example, distinctCount will equal 3.

Upvotes: 1

Related Questions