Selecting Distinct Values using XPath Expression

Hi,

XPath is one of the flexible standard for querying an xml document.

Let us consider the following CD xml as example:-

<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<cd country="USA">
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<price>10.90</price>
</cd>
<cd country="UK">
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<price>9.90</price>
</cd>
<cd country="USA">
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<price>9.90</price>
</cd>
<cd country="UK">
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<price>9.90</price>
</cd>
</catalog>

In the above xml, if we would like to get distinct country names, then we can make use of the preceding-sibling property to check for any
existence of the country name so as to filter and show only distinct country names.

/catalog/cd[not(@country=preceding-sibling::cd/@country)]/@country

The above expression would select only distinct country names i.e. UK and USA only once each.

Cheers.

Print | posted on Monday, April 25, 2005 7:46 AM

Comments on this post

# re: Selecting Distinct Values using XPath Expression

Requesting Gravatar...
I want to query distinct values from XML file
Left by Thiyagu on Jul 11, 2005 10:06 AM

# re: Selecting Distinct Values using XPath Expression

Requesting Gravatar...
Have you resolved the problem, as in above case where china is skiped, to get distinct value for the tag? If you have solved the problem then do let me know.

Thank you!
Left by Himanshu Chawla on Aug 13, 2005 4:58 AM

# re: Selecting Distinct Values using XPath Expression

Requesting Gravatar...
distinct-values(//catalog/cd/price)
It will select distinct price
Left by apilkoirala on Aug 25, 2005 2:47 AM

# re: Selecting Distinct Values using XPath Expression

Requesting Gravatar...
Find the distinct node names...

*[not( name()=name(following-sibling::*) )]

returns the last nodes in the set with unique names. Use preceding-sibling to get the first node, but be prepared for a significant performance hit.
Left by John Mee on Nov 01, 2006 11:54 PM

# re: Selecting Distinct Values using XPath Expression

Requesting Gravatar...
*[not( name()=name(following-sibling::*) )]

does not return the distinct node names because the call name(following-sibling::*) just returns the name of the first node of the siblings after the current node. This is only valid if the document has the same name of consecutive nodes like in this case:
<cd>...</cd>
<cd>...</cd>
<cd>...</cd>

But not when we have the nodes like:
<key>...</key>
<string>...</string>
<key>...</key>
<string>...</string>
<key>...</key>
<string>...</string>

With this query, it will still return 6 nodes.

Do you have any idea how to solve this?
Left by Diiar on Feb 28, 2008 11:13 AM

# re: Selecting Distinct Values using XPath Expression

Requesting Gravatar...
Do you have any idea how to solve this
Left by azdırıcı on Nov 30, 2008 4:43 PM

# re: Selecting Distinct Values using XPath Expression

Requesting Gravatar...
I used this xpath in my serverside code and is working absolutely fine, but when using the same in javascript is throwing error

xPath : "/catalog/cd[not(@country=preceding-sibling::cd/@country)]/@country",

Error : msxml3.dll: Expected token ')' found ':'


Left by dhina on Jun 01, 2009 6:49 PM

# re: Selecting Distinct Values using XPath Expression

Requesting Gravatar...
xmldoc=new ActiveXObject("Microsoft.XMLDOM");
xmldoc.setProperty("SelectionLanguage", "XPath");
xmldoc.selectNodes(..)

/*
Used in MSXML 3.0 to specify whether the DOM object should use XPath language ("XPath") or the old XSLPattern language (default) as the query language.

This property is supported in MSXML 3.0, 4.0, 5.0, and 6.0. The default value is "XSLPattern" for 3.0. The default value is "XPath" for 4.0, 5.0, and 6.0.
*/
Left by Trunkate on Jun 18, 2009 11:54 PM

# re: Selecting Distinct Values using XPath Expression

Requesting Gravatar...
just returns the name of the first node of the siblings after the current node.
Left by mario oyunları on Sep 22, 2009 12:12 AM

# re: Selecting Distinct Values using XPath Expression

Requesting Gravatar...
here is my solution to select unique fields regrding to there caption attribute, convert the nodes and attributes names regarding to your use:

<xsl:variable name="list" select=".//fields/field[@type!='block' and @caption]"/>
<xsl:for-each select="$list">
<xsl:variable name="p" select="position()"/>
<xsl:if test="not(@caption=$list[position() &lt; $p]/@caption)">
<xsl:apply-templates select="."/>
</xsl:if>
</xsl:for-each>
Left by Firas Nizam on May 03, 2010 12:15 AM

# how can I pass parameters in xml through coldfusion

Requesting Gravatar...
It must be able to view on a mobile I have them hard coded but I need to call each one when clicked from a listings page
Left by steve on Dec 05, 2013 10:49 PM

Your comment:

 (will show your gravatar)