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

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

Sorting a sequence removes any original relationship to document order. Its items are most easily numbered using functions fn:position() and fn:last(), unless HTML numbering is possible. Instruction xsl:number also supports sorted sequences through attribute value.

Simple numbering with fn:position()

Function fn:position() returns the context position, an item’s index in the current sequence. It can therefore generate simple numbering.

This example numbers a sorted sequence of all elements <datensatz> of an imaginary input document. Function fn:last() returns the element count in the sequence, equal to that in the document, and function fn:position() returns the position of the current element <zeile>:

<xsl:apply-templates select="//datensatz">
  <xsl:sort/>
</xsl:apply-templates>
<xsl:template match="datensatz">
  <p>Zeile <xsl:value-of select="fn:position()"/> von <xsl:value-of select="fn:last()"/>: <xsl:value-of select="."/></p>
</xsl:template>

yields, for example:

<p>Zeile 1 von 50: Sortierter Datensatz.</p>
<p>Zeile 2 von 50: Sortierter Datensatz.</p>
<p>Zeile 3 von 50: Sortierter Datensatz.</p>
<p>Zeile 4 von 50: Sortierter Datensatz.</p>
...

This is beautifully simple, but you can use only the integers returned by fn:position() or fn:last(). The excellent format-string capabilities of xsl:number are unavailable here.

Numbering with xsl:number and the value attribute

This is not a real problem. The output of xsl:number can easily be detached from document order and coupled to sequence sorting using fn:position().

Use attribute value of xsl:number to supply a numeric value determining the number placed before the numbered element. This value can come from fn:position(). Now attribute format is also available for the desired numbering format.

For this source document:

...
<liste>
  <listenelement>B Listenelement</listenelement>
  <listenelement>C Listenelement</listenelement>
  <listenelement>A Listenelement</listenelement>
</liste>
...

first use this template to generate a sequence sorted by string value of <listenelement> nodes:

<xsl:template match="liste">
  <div>
    <xsl:apply-templates>
      <xsl:sort/>
    </xsl:apply-templates>
  </div>
</xsl:template>

When processing this template, numbering can no longer follow document order because of sorting. Use the position in the current sequence with attribute value:

<xsl:template match="listenelement">
  <p><xsl:number value="fn:position()" format="i) " /><xsl:apply-templates/></p>
</xsl:template>

Note:
Since the value comes from the XPath expression, attributes count, level and from are unnecessary and ignored if present.

This produces the following result:

...
  <div>
    <p>i) A Listenelement</p>
    <p>ii) B Listenelement</p>
    <p>iii) C Listenelement</p>
  </div>
...

Normal numbering without value="fn:position()" would produce incorrect numbers based on actual document order:

...
  <div>
    <p>iii) A Listenelement</p>
    <p>i) B Listenelement</p>
    <p>ii) C Listenelement</p>
  </div>
...

The book list sorted and numbered

This also applies to the book list sorted by title. Numbering with <xsl:number format="001.) "/> without accounting for sorting scrambles the numbers:

009.) Aale im Streiflicht von Braun, Bert; Verlag: Ozean Verlag; Preis: 14.00 Euro (ISBN: 3-890-77608-5) 013.) Another Dog's Guide von Murr, Kater; Verlag: Miau Publishing; Preis: 23.95 Dollar (ISBN liegt nicht vor.) 023.) Another Hitchhiker's Guide to the Galaxy von Adams, Douglas; Verlag: Spacetrotter Publi­shing; Preis: 15.00 Dollar (ISBN: 3-899-77617-4)

Attribute value easily fixes this:

<xsl:number format="001.) " value="fn:position()"/>

The result is orderly again:

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

So far, sequences were constructed using xsl:apply-templates. XSLT also offers loop instructions, which both construct and process sequences themselves.

   

back to top