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

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

Built-in template rules now pass parameters through unchanged.

Format

  select? = expression

  mode? = token>

  


Position


is an instruction, and it is always used within a sequence constructor.

Attributes

Name
Value
Meaning
select
optional
XPath Expression
The sequence of nodes to be processed. If omitted, all children of the context node are processed.
mode
optional
Lexical QName or
#current
The processing mode. Template rules used to process the selected nodes must have a matching mode. If omitted, the default (unnamed) mode is used. The value
#current
indicates that the current mode should be used.

Content

Zero or more

elements and zero or more

elements, in any order.

Effect

The

element selects a sequence of nodes in the input tree and processes each of them individually by finding a matching template rule for that node. The sequence of nodes is determined by the
select
attribute; the order in which they are processed is determined by the

elements (if present); and the parameters passed to the template rules are determined by the

elements (if present). The behavior is explained in detail in the following sections.

The select Attribute

If the
select
attribute is present, the
expression
defines the nodes that will be processed. This must be an XPath expression that returns a sequence of (zero or more) nodes. For example,
select=“*”/>
selects the element nodes that are children of the context node. Writing
select=“@width+3”/>
would cause a type error, because the value of the expression is a number, not a sequence of nodes.

The expression may select nodes relative to the context node (the node currently being processed), as in the example above. Alternatively, it may make an absolute selection from the root node (for example
select=“//item”/>
), or it may simply select the nodes by reference to a variable initialized earlier (for example
)
.

If the
select
attribute is omitted, the nodes processed will be the children of the context node: that is, the elements, text nodes, comments, and processing instructions that occur directly within the context node. It's then an error if the context item isn't a node. Text nodes that consist only of whitespace will be processed along with the others, unless they have been stripped from the tree; for details, see

on page 465. In the XDM tree model (described in Chapter 2) attribute nodes and namespace nodes are
not
regarded as children of the containing element, so they are not processed: If you want to process attribute nodes, you must include an explicit
select
attribute; for example,
select=“@*”/>
. However, it is more usual to get the attribute values directly using the

instruction, described on page 495.

For each node in the selected sequence, in turn, one template rule is selected and the sequence constructor contained in its body is evaluated. In general, there may be a different template rule for each selected node. Within this sequence constructor, this node becomes the new context node, so it can be referred to using the XPath expression
.
.

The called template can also discover the relative position of this node within the list of nodes selected for processing: specifically, it can use the
position()
function to give the position of that node in the list of nodes being processed (the first node processed has
position()=1
, and so on), and the
last()
function to give the number of nodes in the list being processed. These two functions are described in detail in Chapter 13. They enable the called template to output sequence numbers for the nodes as they are processed, or to take different action for the first and the last nodes, or perhaps to use different background colors for odd-numbered and even-numbered nodes.

Sorting

If there are no child

instructions, the selected items are processed in the order of the sequence produced by evaluating the
select
expression. If the
select
expression is a path expression, the nodes will be in
document order
. In the normal case where the nodes all come from the same input document this means they will be processed in the order they are encountered in the original source document; for example, an element node is processed before its children. Attribute nodes belonging to the same element, however, may be processed in any order, because the order of attributes in XML is not considered significant. If there are nodes from several different documents in the sequence, which can happen when you use the
document()
function (described in Chapter 13, page 754), the relative order of nodes from different documents is not defined, though it is consistent if the same set of nodes is processed more than once.

The direction of the axis used to select the nodes is irrelevant. (The direction of different axes is described in Chapter 9.) For example,
select=“preceding-sibling::*”
will process the preceding siblings of the current node in document order (starting with the first sibling) even though the preceding-sibling axis is in reverse document order. The axis direction affects only the meaning of any positional qualifiers used within the select expression. For example,
select=“preceding-sibling::*[1]”
will select the first preceding sibling element in the direction of the axis, which is the element immediately before the current node, if there is one.

Other books

Next to Me by AnnaLisa Grant
Durbar by Singh, Tavleen
Andersen, Kurt by True Believers
Aberystwyth Mon Amour by Pryce, Malcolm
Unknown by Unknown
The Cutting Crew by Steve Mosby