Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
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
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
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
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
<
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
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
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