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

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

Content

If the
select
attribute is present, the element must be empty. Otherwise, it may contain a sequence constructor.

Effect

The most common use of

is to select a node, and output the value of that node as a string. For example,

outputs the string value of the
dept
attribute of the context node. In the general case, however, the rules are more complicated.

The
select
expression or the contained
sequence-constructor
is evaluated. Adjacent text nodes in the resulting sequence are merged, and zero-length text nodes are removed. The sequence is then atomized, which causes any nodes in the sequence to be replaced by their typed values.

If the atomized sequence is empty, the result of the

instruction is a text node containing a zero-length string. Text nodes are allowed to be zero-length so long as they have no parent. But if you try to use a zero-length text node to form the content of an element, the text node disappears in the process.

If the atomized sequence contains a single value, this is converted to a string (by applying the XPath 2.0 casting rules). A new text node is constructed with this string as its value, and the text node is returned as the result of the

instruction (which usually means it will be written to a result tree). If the sequence contains more than one item, then the effect depends on whether backward-compatibility mode is enabled. This depends on the nearest
version
or
xsl:version
attribute found on a containing element, known as the effective version. If the effective version is less than
2.0
, then backward-compatibility mode is in force. However, if there is a
separator
attribute, then the instruction behaves according to the XSLT 2.0 rules regardless of the effective version.

Under the backward-compatibility rules, any item after the first in the sequence is discarded, and the instruction behaves as if the sequence only contained one item.

Under the 2.0 rules, each value in the atomized sequence is converted to a string by applying the XPath casting rules, and these strings are concatenated, with the chosen separator inserted between adjacent strings. A new text node is constructed that contains this string as its value, and the text node is returned as the result of the instruction. The default separator (under the 2.0 rules) is a single space when the
select
attribute is used, or a zero-length string when a sequence constructor is used. This means that


will output:

but:

will output:

The deprecated
disable-output-escaping
attribute has the same effect as it has with

. Special characters such as
<
in the string value of the select expression will be escaped just as if they occurred in literal text, and the
disable-output-escaping
attribute can be used to suppress this in the same way. For details, see Chapter 15 on page 945.

Usage

The

element is the most common way of writing text to a result tree.

Because the instruction is so common, it is often used unthinkingly when other constructs might be more appropriate. For example, it is very common to encounter code such as:


    


when it would be much more appropriate to write:


The difference is that the first variable creates a new temporary tree containing a document node and a text node, with the text node being a copy of data selected from the input document; the second variable is simply a reference to the selected node in the source. So the second construct is not only shorter and more readable, it is also likely to be much more efficient. The optimizer can't simply treat the two constructs as equivalent, because they are not. For example, if
a/b/c/d
selects nothing, then
empty($x)
will return false in the first case, true in the second.

Similarly, when a function returns a string result, people sometimes write code like this:


  

  


Here
upper-case($p)
returns a string. The

instruction creates a text node with this string as its value. Because the function is declared to return
xs:string
, the text node is then atomized to extract the original string. The system would have to do a lot less work if these multiple conversions were avoided, which can be achieved simply by replacing

with

.

Other books

The Summer Guest by Alison Anderson
The Trouble With Coco Monroe by MacKenzie, C. C.
Maritime Murder by Steve Vernon
The Day to Remember by Jessica Wood
Finding Eliza by Stephanie Pitcher Fishman
Heartsong Cottage by Emily March
The Masquerade by Rebecca Berto
Galaxy Blues by Allen Steele