Re: [xsl] How to find distinct nodes by value?

Subject: Re: [xsl] How to find distinct nodes by value?
From: Victor Toni <xsl-list@xxxxxxxxx>
Date: Fri, 13 Oct 2006 02:01:09 +0200
I should have mentioned that I am using XPath only (no XSLT was harmed
:-) )
When using Saxon 8.8 the return value is a List of Strings so there is
no more information about the nodes.

After taking the original expression for identity and tweaking it I
found this:

$arg[empty(index-of(distinct-values(subsequence($arg, 1, position()-1)),
text()))]

to work when "arg = //myElement".

Strangely replacing the variable "arg" with the expression such as

//myElement[empty(index-of(distinct-values(subsequence(//myElement, 1,
position()-1)), text()))]

doesn't show the expected results.
After some more digging I found the much simpler

//myElement[not(preceding::myElement = .)]

to work too but being easier to remember.

Mukul Gandhi wrote:
> You can use XSLT 2.0 distinct-values function.
>
> The sample stylesheet is below:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet version="2.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
>
> <xsl:output method="xml" indent="yes" />
>
> <xsl:template match="/">
>  <output>
>    <xsl:for-each select="distinct-values(//myElement)">
>      <myElement><xsl:value-of select="." /></myElement>
>    </xsl:for-each>
>  </output>
> </xsl:template>
>
> </xsl:stylesheet>
>
> On 10/11/06, Victor Toni <xsl-list@xxxxxxxxx> wrote:
>> I would like to retrieve only the distinct nodes (by value) from a
>> "//myElement"-like  expression; <myElement> is either empty or contains
>> only text.
>>
>> From this document
>>
>> <a>
>>    <myElement>aaa</myElement>
>>    <b>
>>       <myElement>aaa</myElement>
>>       <myElement>xyz</myElement>
>>       <myElement>efg</myElement>
>>    </b>
>>    <c>
>>       <d>
>>           <myElement>khl</myElement>
>>           <myElement>xyz</myElement>
>>       </d>
>>    </c>
>> </a>
>>
>> the result should be (the order doesn't matter)
>>
>> <myElement>aaa</myElement>
>> <myElement>xyz</myElement>
>> <myElement>efg</myElement>
>> <myElement>khl</myElement>
>>
>> In early XPath "Working Draft"s there seems to have been a function
>> called "distinct-nodes()" which should have done something similar but
>> it used the identity of the nodes.
>> This document still shows the function
>> http://www.w3.org/TR/2003/WD-xpath-functions-20030502/#func-distinct-nodes
>>
>> and it was removed in
>> http://www.w3.org/TR/2003/WD-xpath-functions-20031112/
>>
>> A replacement for this function found in
>> http://www.w3.org/TR/2005/WD-xpath-functions-20050404/#func-distinct-nodes-stable
>>
>>
>> basically tells that
>>
>> //myElement[empty(subsequence(//myElement, 1, position()-1) intersect
>> .)]
>>
>> should work to select distinct nodes (by identity).
>>
>> But I haven't been able to find a solution for distinct nodes by value.
>>
>> Any suggestions?

Current Thread