[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

[xsl] Sort list by a combination of elements


Subject: [xsl] Sort list by a combination of elements
From: Manuel Strehl <manuel.strehl@xxxxxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 21 Dec 2006 14:28:03 +0100

Hi.

Consider some kind of bookshelf XML:

<bookshelf>
    <series id="LoTR">
       <author>Tolkien</author>
    </series>
    ...
    <book>
       <title>Two Towers</title>
       <belongs_to ref="LoTR">
    </book>
    ...
    <book>
       <title>It</title>
       <author>King</author>
    </book>
</bookshelf>

So we have <book>s with <author> elements and <book>s with a
<belongs_to> referring to a series of some author.

Problem: Sort the list of books by the author.

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.

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:

<choose>
    <when test="author"><sort select="author">
    <otherwise><sort select="series/author">
</choose>

Best Regards
Manuel


Current Thread