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.

posted @ Monday, April 25, 2005 7:46 AM

Print

Comments on this entry:

# re: Selecting Distinct Values using XPath Expression

Left by Thiyagu at 7/11/2005 10:06 AM
Gravatar
I want to query distinct values from XML file

# re: Selecting Distinct Values using XPath Expression

Left by Himanshu Chawla at 8/13/2005 4:58 AM
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!

# re: Selecting Distinct Values using XPath Expression

Left by apilkoirala at 8/25/2005 2:47 AM
Gravatar
distinct-values(//catalog/cd/price)
It will select distinct price

# re: Selecting Distinct Values using XPath Expression

Left by John Mee at 11/1/2006 11:54 PM
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.

# re: Selecting Distinct Values using XPath Expression

Left by Diiar at 2/28/2008 11:13 AM
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?

# re: Selecting Distinct Values using XPath Expression

Left by azdırıcı at 11/30/2008 4:43 PM
Gravatar
Do you have any idea how to solve this

# re: Selecting Distinct Values using XPath Expression

Left by dhina at 6/1/2009 6:49 PM
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 ':'


# re: Selecting Distinct Values using XPath Expression

Left by Trunkate at 6/18/2009 11:54 PM
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.
*/

# re: Selecting Distinct Values using XPath Expression

Left by mario oyunları at 9/22/2009 12:12 AM
Gravatar
just returns the name of the first node of the siblings after the current node.

Your comment:



 (will not be displayed)


 
 
 
 
 

Live Comment Preview:

 
«November»
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345