Re: [xsl] Sorting using helper structure, position(), xsl:key

Subject: Re: [xsl] Sorting using helper structure, position(), xsl:key
From: Michael Ludwig <mlu@xxxxxxxxxxxxx>
Date: Tue, 15 Apr 2008 18:18:42 +0200
Olivier Mengui schrieb:
2008/4/10, David Carlisle <davidc@xxxxxxxxx>:
in xslt1 you have to be in the right document already, which means
that in teh restricted context of xsl:sort you can only use a key if
your mappig element is in teh same document as the nodes over which
you are iterating.

Does it means that I can not use a key on the default input document when I'm iterating (for-each) on nodes of another document (which contains constants similar to Michael's categories) ?

In general, you *can*, by switching back to the default document using the following idiom:

<xsl:key name="bla-lookup" match="ABC" use="@bla"/>

<!-- top level variable -->
<xsl:variable name="main-doc" select="/"/>

<!-- in some template -->
<xsl:for-each select="document($secondary-doc)/A/B">
  <xsl:variable name="bla" select="@bla"/>
  <xsl:for-each select="$main-doc">
    <xsl:value-of select="key('bla-lookup', $bla)"/>
    ...

But in the case of xsl:sort, you *cannot* use this idiom, as xsl:sort
does not allow you to embed a context switch so you're restricted to
what's visible within the context of the secondary document. You must
use a variable in this case, which, of course, is unfortunate if the
node-set to search is large.

At least that's what I understood.

This seems to be an issue I currently have. I'm using xsltproc, so
XSLT 1.0.

In XSLT 2.0, this is much easier as the key() function takes a third argument providing the context. So you can embed the context switch within xsl:sort.

Michael

Current Thread