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: Abel Braaksma <abel.online@xxxxxxxxx>
Date: Wed, 18 Apr 2007 14:49:20 +0200
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)


Cheers,
-- Abel Braaksma

Current Thread