XML technologies / XSLT / Reference: XSLT 1.0 and 2.0 elements / perform-sort
(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 6.)
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z
Instruction xsl:perform-sort sorts an input sequence using a collection of sorting rules xsl:sort and returns the sorted sequence.
Classification | Instruction |
Function group | Sequence sorting |
Introduced | XSLT 2.0 |
Position in the stylesheet and permitted contents:
Instruction xsl:perform-sort is allowed in any instruction with a sequence constructor and within Literal Result Elements.
It contains one or more instructions xsl:sort preceding all other content. If optional attribute select is set, only further instructions xsl:fallback may appear. Without attribute select, an arbitrary sequence constructor after the elements xsl:sort alternatively defines the sequence to sort.
Attributes:
Standard attributes apply. Instruction xsl:perform-sort also has optional attribute select.
select
Value | xpath-expression |
Usage | optional |
Introduced | XSLT 2.0 |
Attribute select defines the input sequence. If set, only elements xsl:sort and xsl:fallback may occur, with no sequence constructor producing another input. Violation is a static error (ERR XTSE1040); attribute select and a sequence constructor can never both be evaluated.
Note: Without attribute select, a sequence constructor after the xsl:sort- instructions must supply the input. Otherwise the empty sequence is assumed and returned, which is valid but unhelpful.
Usage:
Instruction xsl:perform-sort creates complex sorting operations, for example inside user-defined stylesheet functions, making them available from arbitrary XPath expressions.
It accepts any sequence and sorts it using its rules xsl:sort. Instruction xsl:sort order sets criterion precedence: first primary, then secondary, and so on.
The input comes either from attribute select or from a sequence constructor following the instructions xsl:sort. Thus, the xsl:sort instructions cannot be interrupted by other instructions or Literal Result Elements. Only after the last instruction xsl:sort may the constructor or any number of elements xsl:fallback follow.
The sequence constructor cannot compete with attribute select. If attribute select is used, xsl:perform-sort may contain no additional constructor, only required instructions xsl:sort and optional xsl:fallback instructions.
Examples:
Example 1 – xsl:perform-sort syntax:
<xsl:variable name="buchliste-nach-autor">
<xsl:perform-sort select="//buch">
<xsl:sort select="autor/nachname"/>
<xsl:sort select="autor/vorname"/>
</xsl:perform-sort>
<xsl:variable>
The instruction constructs a sequence of all elements <buch> in a document and stores it in a variable sorted by author name. A slightly more involved combination of xsl:for-each and xsl:sequence has the same effect:
<xsl:variable name="buchliste-nach-autor">
<xsl:for-each select="//buch">
<xsl:sort select="autor/nachname"/>
<xsl:sort select="autor/vorname"/>
<!-- Sortierte Liste zurückgeben: -->
<xsl:sequence select="."/>
</xsl:for-each>
<xsl:variable>
Example 2 – Sorting with a stylesheet function:
Source document (excerpt):
<?xml version="1.0" encoding="ISO-8859-1"?>
<buchliste>
<buch>
<titel>Besser PHP programmieren</titel>
<verlag>Galileo Computing</verlag>
<autor>
<vorname>Carsten</vorname>
<nachname> Möhrke</nachname>
</autor>
</buch>
<buch>
<titel>Cocoon 2 und Tomcat</titel>
<verlag>Galileo Computing</verlag>
<autor>
<vorname>Stephan</vorname>
<nachname>Niedermeier</nachname>
</autor>
</buch>
...
</buchliste>
Stylesheet:
<?xml version="1.0"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:mein="lokaler-funktionsnamensraum" exclude-result-prefixes="mein">
<!-- Die Stylesheetfunktion: -->
<xsl:function name="mein:buchliste-nach-autor">
<xsl:param name="eingabeliste"/>
<xsl:perform-sort select="$eingabeliste//buch">
<xsl:sort select="autor/nachname"/>
<xsl:sort select="autor/vorname"/>
</xsl:perform-sort>
</xsl:function>
<xsl:template match="/">
<buchliste-nach-autor>
<xsl:sequence select="mein:buchliste-nach-autor(document('buchliste.xml'))"/>
</buchliste-nach-autor>
</xsl:template>
</xsl:stylesheet>
One application of xsl:perform-sort is in stylesheet functions sorting content. This allows sorting directly from XPath expressions and immediate further processing of the returned sorted sequence.
Element definition:
XSLT 1.0:
Element unavailable in XSLT 1.0.
XSLT 2.0:
<!-- Category: instruction -->
<xsl:perform-sort
select? = expression
<!-- Content: (xsl:sort+, sequence-constructor?) -->
</xsl:perform-sort>
Copyright © Galileo Press, Bonn 2008
You may print the online version for your private use.
Otherwise, this chapter from “XSLT 2.0 & XPath 2.0 ― Das umfassende Handbuch” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
Galileo Press, Rheinwerkallee 4, 53227 Bonn