Re: [xsl] Finding distinct nodes using a function in XSLT 2.0

Subject: Re: [xsl] Finding distinct nodes using a function in XSLT 2.0
From: "Dimitre Novatchev" <dnovatchev@xxxxxxxxx>
Date: Wed, 18 Apr 2007 06:59:46 -0700
On 4/18/07, Abel Braaksma <abel.online@xxxxxxxxx> wrote:
Dimitre Novatchev wrote:
> On 4/17/07, Scott Lynch <slynch@xxxxxxxxxx> wrote:
>> Dimitre,
>>
>> That works nicely.
>>
>> However, is there any difference between
>> <xsl:sequence select="."/>
>> (which works) rather than say
>> <xsl:sequence select="current-group()[1]"/>
>> which also works, in the overall template logic?
>
> Hi Scott,
>
> No there isn't any difference (in this case), because as the XSLT2.0
> spec says:
>
>

But note that (the other case) you can use current-group() still when
the context item has changed. I.e., in another template which is called
or indirectly invoked from your for-each-group. In which case the dot
'.' will contain something else then current-group()[1]

Btw, you can also do the distinct nodes in a single xpath (the title of
your post seems to suggest you were looking for a function), as long as
your identity is based on a value (of @id):

   <xsl:variable name="files"
         select="document(('file1.xml', 'file2.xml'))" />

   <xsl:template match="/" name="main">
       <xsl:copy-of select="
           for $i in distinct-values($files/*/*/@id)
           return ($files/*/*[@id = $i])[1]" />
   </xsl:template>


If you need sorting, you can use xsl:perform-sort, change the xsl:copy-of like this:

  <!-- or, with sorting -->
  <xsl:perform-sort select="
        for $i in distinct-values($files/*/bar/@id)
        return ($files/*/*[@id = $i])[1]" >

      <xsl:sort select="@id" />
  </xsl:perform-sort>


(btw: note the double parentheses around the fn:document() function, they are on purpose)


Sure, current-group() and distinct-values() have their use cases.

However, I personally prefer to leave the "black work" to the XSLT
processor doing <xsl:for-each-group .../> and all these things behind
the scene for me.

Also, it is quite possible that this more compactly expressed action
(saying "what" and not so much "how") would better lend itself to
optimisation.


-- Cheers, Dimitre Novatchev --------------------------------------- Truly great madness cannot be achieved without significant intelligence. --------------------------------------- To invent, you need a good imagination and a pile of junk ------------------------------------- You've achieved success in your field when you don't know whether what you're doing is work or play

Current Thread