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

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

When a sequence constructor is evaluated, the result is, in general, a sequence of items. Many instructions such as

,

, and

create new nodes; a couple of the new instructions introduced in XSLT 2.0 such as

and

can also return references to existing nodes in a source document. It is also possible for the sequence to contain atomic values: these can be produced using the

and

instructions.

Most likely, the sequence returned by a sequence constructor will be used to build the content of an element node in the result tree. In the example shown in
Figure 2-9
:

  • The sequence constructors contained in the

    elements always (if they are evaluated at all) produce a sequence of three nodes: two empty


    elements and a text node.
  • The sequence constructor contained in the

    instruction returns a sequence that is the concatenation of the nodes returned by the first

    instruction, then the nodes returned by the

    instruction, and finally the nodes returned by the second

    instruction. The result of the template is this sequence of nodes.

Suppose that the template rule in our example is invoked using a construct such as the following:


    


The

instruction results in the sequence of nodes produced by evaluating the selected template rule. In fact, this instruction can select several

elements, and the template rule is invoked once for each one. The resulting nodes are all concatenated into a single sequence. Because the

instruction is the only instruction in the sequence constructor contained by the


element, the final sequence delivered to the

element contains the results of expanding the template rule for each of the selected

elements in the source document.

The


element is a literal result element, which when evaluated constructs a new element node. The name of this node will be

, and the content will be formed from the sequence produced by evaluating the sequence constructor contained within the literal result element in the stylesheet. In our example, this sequence contains elements and text nodes, and these are copied to form the content of the new

element.

This is by far the most common scenario: instructions are evaluated to produce a sequence of nodes, and the nodes are copied to form the contents of a result tree. However, in XSLT 2.0 it is also possible to process the sequence in other ways. In particular:

  • A generated sequence can be captured in a variable. For example, the following variable will have a value that is a sequence of element nodes. These elements are not attached to a tree; they have no parent, and they are not siblings of each other:


     January

     February

     March


  • You can then refer to the third element in this sequence as
    $months[3]
    . But don't try doing
    $months/month
    ; the
    $months
    variable holds a sequence of

    elements, and the path expression
    $months/month
    (which is short for
    $months/child::month
    ) tries to find children of these elements that are named

    . It won't find any.

This kind of variable is constructed when the
as
attribute is present to define the required type of the items in the sequence. If the
as
attribute were omitted, the

elements would be added to a temporary document, and the variable
$months
would refer to the document node of this tree. For more information, see the section “Temporary Documents” on page 85.

Other books

Coming Home by Leslie Kelly
Sleepwalker by Michael Cadnum
Twisted Perfection by Abbi Glines
Astonish Me by Maggie Shipstead
Tracie Peterson by Entangled
All I Want for Christmas by Linda Reilly
Anonymous Sources by Mary Louise Kelly
Magic Graves by Ilona Andrews, Jeaniene Frost

© FullEnglishBooks 2015 - 2024    Contact for me [email protected]