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

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
2.92Mb size Format: txt, pdf, ePub

It is possible to rewrite this whole construct as:

    select=“if (@discount)

             then @price - @discount

             else @price”/>

or if you really like brevity, as:

              select=“@price - (@discount, 0)[1]”/>

This illustrates that when you are calculating values, you often have a choice as to whether to do the work at the XPath level or at the XSLT level. My own preference with simple expressions such as this is to use the XPath approach, unless there is a need to use facilities that are only available at the XSLT level, such as the ability to create new nodes, or the more powerful instructions available in XSLT such as

,

, or

.

By far the most common use of

is the evaluation of the result of a stylesheet function, and there are examples of this under

on page 344.

The

instruction also enables template rules to return atomic values, or references to existing nodes. An example of the possibilities this opens up is shown in the section
Simulating Higher Order Functions
on page 250, which shows a generalized function for finding cycles in any XML data source, regardless of how the relationships are represented.

See Also


on page 344


on page 292


on page 433

xsl:sort

The

element is used to define a component of a sort key. It is used within an

,

,

, or

instruction to define the order in which this instruction processes its data.

Changes in 2.0

The

element may now be used within

and

as well as within

and

.

The value of the sort key may be calculated using an enclosed sequence constructor, as an alternative to using the
select
attribute.

In XSLT 1.0,

was always used to sort a set of nodes. In 2.0 it is generalized so that it can sort any sequence.

A
collation
attribute has been added to allow the collating sequence for strings to be specified by means of a URI, and a
stable
attribute has been added to control how duplicates are handled.

Sorting is now sensitive to the types of the items being sorted. For example, if the sort key values are numeric, they will be compared as numbers rather than as strings.

Format

  select? = 
expression

  lang? = { 
nmtoken
 }

  order? = { “ascending” | “descending”}

  collation? = { 
uri
 }

  case-order? = { “upper-first” | “lower-first”}

  data-type? = { “text” | “number” | 
qname-but-not-ncname
} >

  stable? = { “yes” | “no”}

  


Position


is always a child of

,

,

, or

. Any number of sort keys may be specified, in major-to-minor order.

When used in

,

, or

, any

elements must appear before the sequence constructor of the containing element.

When used in

, the

elements can come before or after any

elements.

Attributes

Name
Value
Meaning
select
optional
XPath Expression
Defines the sort key. The default is
.
, the context item itself.
order
optional
Attribute value template returning
ascending
or
descending
Defines whether the nodes are processed in ascending or descending order of this key. The default is
ascending
.
case-order
optional
Attribute value template returning
upper-first
or
lower-first
Defines whether upper-case letters are to be collated before or after lower-case letters. The default is language-dependent.
lang
optional
Attribute value template returning a language code
Defines the language whose collating conventions are to be used. The default depends on the processing environment.
data-type
optional
Attribute value template returning
text
,
number
or Lexical QName
Defines whether the values are to be collated alphabetically or numerically, or using a user-defined type. The default is
text
.

Other books

Napoleón en Chamartín by Benito Pérez Galdós
The Tin-Kin by Eleanor Thom
Renee Ryan by The Outlaw's Redemption
Supervising Sally by Oliver, Marina
Muchacho by Louanne Johnson
Making the Hook-Up by Cole Riley
06 - Eye of the Fortuneteller by R.L. Stine - (ebook by Undead)
Acosado by Kevin Hearne