XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition (234 page)

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
13.89Mb size Format: txt, pdf, ePub
collation
optional
Attribute value template returning collation URI
The collation URI identifies how strings are to be compared with each other.
stable
optional
Attribute value template returning
yes
or
no
This attribute is allowed only on the first

element; if set to
no
, it indicates that there is no requirement to retain the original order of items that have equal values for all the sort keys.

Except for
select
, all these attributes can be written as attribute value templates. The context item, context position, and context size for evaluating these attribute value templates are the same as the context for evaluating the
select
attribute of the containing instruction (that is

,

,

, or

).

Content

The element may contain a sequence constructor. This is used to compute the sort key value, as an alternative to using the
select
attribute. The two are mutually exclusive: if the
select
attribute is present, the element must be empty, and if it is not empty, the
select
attribute must be omitted. If neither is present, the default is
select=“.”
.

Effect

The list of

elements appearing for example within an

or

element determines the order in which the selected items are processed. The items are sorted first by the first sort key; any group of items that have duplicate values for the first sort key are then sorted by the second sort key, and so on.

It's useful to start by establishing some clear terminology. There is a tendency to use the phrase
sort key
with several different meanings, often in the same sentence, so to avoid confusion I'll try to stick to the more precise terms that are used in the XSLT specification itself.

  • A collection of

    elements, which together define all the criteria for performing a sort, is called a
    sort key specification
    .
  • A single

    element within the sort key specification is called a
    sort key component
    . Often, of course, there will only be one component in a sort key specification.
  • The result of evaluating a sort key component for one of the items to be sorted is called a
    sort key value
    .

So if you are sorting by last name and then first name, the sort key specification is “last name, then first name;” the sort key components are “last name” and “first name,” and the sort key values are strings such as
“Kay”
and
“Michael”
.

It's also useful to be clear about how we describe the sorting process:

  • The sequence that provides the input to the sort operation is called the
    initial sequence
    . In XSLT 1.0 the initial sequence was always a set of nodes in document order, but in 2.0 it can be any sequence of items (nodes or atomic values) in any order.
  • The sequence that is produced as the output of the sort operation is called (naturally enough) the
    sorted sequence
    .

The overall rules for the sorting operation are fairly intuitive, but it's worth stating them for completeness:

  • Given two items A and B in the initial sequence, their relative positions in the sorted sequence are determined by evaluating all their sort key values, one for each component in the sort key specification.
  • The relative positions of A and B depend on the first pair of sort key values that is different for A and B; the second pair of sort key values needs to be considered only if the first sort key values for the two items are equal, and so on. For example, if you are sorting by last name and then first name, the system will only need to consider the first name for two individuals who have the same last name. I will explain later exactly what it means for one sort key value to be considered equal to another.
  • Considering only this pair of sort key values, A comes before B in the sorted sequence if A's value for this sort key component is less than B's value, unless
    order=“descending”
    is specified for this sort key component, in which case it comes after B. I will explain later what it means for one sort key value to be less than another.
  • If all the sort key values for A and B are the same, then A and B appear in the sorted sequence in the same relative positions that they had in the initial sequence (the technical term for this is that the sort is
    stable
    ). However, if
    stable=“no”
    is specified on the first

    element, this requirement is waived, and the system can return duplicates in any order.

Other books

Footsteps in the Sky by Greg Keyes
Lance by Elle Thorne
Bizarre History by Joe Rhatigan
Matt Reilly Stories by Flyboy707