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

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

then the same stylesheet can contain a named template to display a numbered message as follows:


   

   

      

       select=“document(‘’)/*/user:data/message[@nr=$message-nr]”/>

   


The

element evaluates the XPath expression in its
select
attribute as a string, and writes the value to the result tree. In this case the XPath expression is a path expression starting with
document(‘’)
, which selects the root node of the stylesheet module, followed by
*
, which selects its first child (the

element), followed by
user:data
, which selects the

element, followed by
message[@nr = $message-nr]
, which selects the

element whose
nr
attribute is equal to the value of the
$message-nr
parameter in the stylesheet.

The advantage of this technique is that it gathers all the messages together in one place, for ease of maintenance. The technique can also be readily extended to use different sets of messages, depending on the user's preferred language.

With XSLT 2.0 this technique is no longer necessary, because it becomes more convenient to define fixed data as part of a global variable definition. Instead of writing:


   Source document is empty

   Invalid date

   Sales value is not numeric


you can write:


   Source document is empty

   Invalid date

   Sales value is not numeric


and instead of:


you can write:


The XSLT 1.0 technique still works, and you may want to continue using it when you write stylesheets that are to be portable between 1.0 and 2.0 processors.

Instructions

We saw in the previous chapter that a stylesheet is evaluated by a process that involves identifying template rules, evaluating the instructions contained in a template rule to produce nodes, and then adding the nodes to a result tree. This section explores in more detail the instructions that can be evaluated to produce nodes in the result tree.

The content of an

declaration, and of various other XSLT elements, is known as a
sequence constructor
. Element nodes within a sequence constructor are one of three kinds: XSLT instructions, extension elements, and literal result elements. I'll describe these in the next three sections.

XSLT Instructions

An XSLT instruction is one of the following elements.



























No other element in the XSLT namespace may appear directly in a sequence constructor. Other XSLT elements, for example

,

, and

, are not regarded as instructions, because they cannot appear directly in a sequence constructor—they may appear only in very specific contexts. The

element is anomalous as it can appear as a child of an

element, but it is constrained to appear before other elements, and is therefore not considered to be part of the sequence constructor. So, it is not classified as an instruction. The same is true of an

element appearing within

or

.

The following table gives a brief introduction to the effect of each XSLT instruction.

Instruction
Effect

Applies a regular expression to a string, causing subsidiary instructions to be evaluated for each matching and nonmatching substring.

Searches imported stylesheets for another template rule to apply to the context node.

Selects a sequence of nodes and for each of these nodes identifies the template rule to be used to process that node, invokes the template rule, and returns the results.

Constructs an attribute node.

Invokes a named template and returns its result.

Chooses one of a number of instructions to evaluate, based on boolean conditions.

Constructs a comment node.

Copies the context node. This is a shallow copy; the content of the new node is determined by the contained instructions.

Returns a deep copy of selected nodes or atomic values.

Constructs a document node.

Constructs an element node.

Defines fallback behavior to use if a particular instruction is not available.

Invokes the contained instructions once for each item in a sequence of items.

Selects a sequence of items and divides these into groups according to specified criteria; invokes the contained instructions once for each group of items.

Evaluates the contained instructions if and only if a specified condition is true.

Outputs a message to a system-defined destination.

Constructs a namespace node.

Selects another template rule that applies to the context node and invokes it.

Generates a sequence number for the context node and formats it for output.

Constructs a processing instruction node.

Selects a sequence of items and sorts them according to specified criteria.

Constructs a document node to act as the root of a result tree and optionally serializes it to a specified output destination.

Produces a sequence of nodes and/or atomic values.

Constructs a text node from literal text in the stylesheet, preserving whitespace.

Constructs a text node.

Defines a local variable whose value can be accessed from other instructions within its scope.

Other books

The World in Half by Cristina Henriquez
Playing With Fire by Gail Anderson-Dargatz
Little Boy Blue by Edward Bunker
Marketplace by Laura Antoniou
Eye Sleuth by Hazel Dawkins
A Wanton's Thief by Titania Ladley
Destiny's Chance by Cara Bristol