XML technologies / XSLT / Introduction to XSLT 2.0 / XSLT and XPath – round two / Sorting with xsl:sort / Sorting sequences

(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 2.)

The instruction xsl:sort can be used wherever sequences are created, primarily with instruction xsl:apply-templates. Instruction xsl:sort can also be used with the loop instructions xsl:for-each and xsl:for-each-group described two sections later.

For example, the much-used book list can easily be sorted by adding xsl:sort to the construction of the book-element sequence:

<xsl:template match="buchhandel">
  <xsl:apply-templates select="buch">
    <xsl:sort/>
  </xsl:apply-templates>
</xsl:template>

The result appears to be sorted by book title:

Aale im Streiflicht von Braun, Bert; Verlag: Ozean Verlag; Preis: 14.00 Euro (ISBN: 3-890-77608-5) Another Dog's Guide von Murr, Kater; Verlag: Miau Publishing; Preis: 23.95 Dollar (ISBN liegt nicht vor.) Another Hitchhiker's Guide to the Galaxy von Adams, Douglas; Verlag: Spacetrotter Publishing; Preis: 15.00 Dollar (ISBN: 3-899-77617-4) Bergwandern in den Alpen von Assmann, Peter; Verlag: Berg Verlag; Preis: 14.00 Euro (ISBN: 3-899-77611-7)

This is usable, but the actual sort key is not the book title, which is the first element in <buch> in document order, but the element’s entire string value.

Sorting by a specific criterion is more interesting. To order the list by price, use the select attribute of xsl:sort:

<xsl:template match="buchhandel">
  <xsl:apply-templates select="buch">
    <xsl:sort select="preis"/>
  </xsl:apply-templates>
</xsl:template>

Note that the sort expression already starts from the assembled sequence. Therefore, to sort by the price in a child element of <buch>, write only select="preis" rather than—as one might incorrectly infer from the current node of the template rule— select="buch/preis" . The latter produces no result. This gives:

Siddhartha von Hesse, Hermann; Verlag: Suhrkamp; Preis: 11.80 Euro (ISBN: 3-518-36682-3) Wenn der Berg ruft von Ass­mann, Peter; Verlag: Berg Verlag; Preis: 12.00 Euro (ISBN: 3-899-776129-7)

This looks correct but is not: farther down, at the end of the list, is a book priced at 7.50 euros. Why? The answer is simple: sorting again used string values, the default behavior of xsl:sort. To request numeric sorting, this must be specified explicitly.

Another attribute, data-type, must have value "number". At the same time, the sort direction can be reversed using order="descending":

<xsl:template match="buchhandel">
  <xsl:apply-templates select="buch">
    <xsl:sort select="preis" data-type="number" order="descending"/>
  </xsl:apply-templates>
</xsl:template>

The books are now sorted by descending price. The book priced at 7.50 euros is again last, but this time correctly:

Java für Anfänger von Tiger, Theobald; Verlag: Java Verlag; Preis: 45.00 Euro (ISBN: 3-899-77618-4) Java für Praktiker von Jobst, Julius; Verlag: EDV Verlag; Preis: 42.95 Euro (ISBN-Nummer liegt nicht vor.)

   

back to top