xsl-list
[Top] [All Lists]

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

2012-11-02 04:16:01
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

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--