[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

[xsl] [Summary] Three ways to express in XPath that there are no duplicates in a list of items


Subject: [xsl] [Summary] Three ways to express in XPath that there are no duplicates in a list of items
From: "Costello, Roger L." <costello@xxxxxxxxx>
Date: Fri, 2 Nov 2012 09:15:33 +0000

Hi Folks,

Thanks David, Dimitre, and Michael for your excellent feedback.

Let's recap the 3 ways to implement using XPath a boolean test that there are
no duplicates in a list of items.

Problem: Write an XPath expression that returns true if there are no duplicate
websites in the following list, false otherwise:

    <Websites>
        <Website id="Amazon"> http://www.amazon.com </Website>
        <Website id="Apple"> http://www.apple.com </Website>
        <Website id="Ebay"> http://www.ebay.com </Website>
        <Website id="Google"> http://www.google.com </Website>
        <Website id="Microsoft"> http://www.microsoft.com </Website>
        <Website id="VirginAtlantic"> http://www.virgin-atlantic.com
</Website>
    </Websites>

Here's how to implement it in XPath 1.0 and in XPath 2.0.

XPath 1.0:

	not(Websites/*[. = preceding-sibling::*])

XPath 2.0:

	empty(Websites/*[index-of(../*,.)[2]])
	count(Websites/*) = count(distinct-values(Websites/*))

The preferred XPath is the last one because it has the best performance. The
first two take on the order of n-squared time (where n is the number of
websites in the list) whereas the last XPath expression takes on the order of
n log n time.

/Roger


Current Thread
Keywords