Re: [xsl] Sort list by a combination of elements

Subject: Re: [xsl] Sort list by a combination of elements
From: David Carlisle <davidc@xxxxxxxxx>
Date: Thu, 21 Dec 2006 13:42:14 GMT
> Solution: Normally one would write something like <xsl:sort
> select="//book/author" />. In this case, I found a solution by
> concatenating: <xsl:sort select="concat( //book/author, //series[ @id =
> current()/belongs_to/@ref ]/author )" />. This works, BUT it "feels like
> a hack", if you know, what I mean.

in both cases you wouldn't want to start the xpaths with // otherwise
all elements would get the same sort key (as these are absolute paths
not depending on the element being sorted)


> I would prefer a more XSLT-like solution, that determines, if there is
> an <author> element, sorts by this and uses the <series> author as a
> fallback. Does anyone know, if and how this could be done? Schematically:

in xslt2
<xsl:key name="series" match="series" use="@id"/>
...

<xsl:for-each select="book">
<xsl:sort select="(author,key('series',belongs_to_ref)/author)[1]"/>

in xslt1

<xsl:key name="series" match="series" use="@id"/>
...

<xsl:for-each select="book">
<xsl:sort select="(author|key('series',belongs_to_ref)/author)[last()]"/>

The xslt2 version does exactly what you ask, thehe xslt 1 version has
the additional restriction (that could be removed) that if a book has
both an author and a series ref, that the series element appears before
the book in document order.

David

Current Thread