Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
Other attributes (optional) | Attribute value template | Any XPath expressions occurring between curly braces in the value are evaluated, and the resulting string forms the value of an attribute copied to the result sequence. Attribute Value Templates are described on page 122. |
Several of the attributes take the form of whitespace-separated lists. This is simply a list of names (or prefixes) in which the various names are separated by any of the XML-defined whitespace characters: tab, carriage return, newline, or space. For example, you could write:
Here the names
blue
,
italic
, and
centered
must match the names of
Content
The content of a literal result element is a sequence constructor. It may thus contain XSLT instructions, extension elements, literal result elements, and/or text nodes.
Effect
A new element is created, whose expanded name (that is, local name and namespace URI) is the same as the name of the literal result element. In nearly all cases the prefix will also be the same. The sequence constructor contained in the literal result element is evaluated, and this sequence is used to form the content of the new element. The new element is then returned as the result of the instruction, to be combined with other items produced by sibling instructions in the stylesheet.
The way attributes and namespaces are handled is described in subsequent sections.
Usage
Consider a sequence constructor containing a single literal result element.
In this case a
If the literal result element has content, then the content must be another sequence constructor, and this sequence constructor is itself evaluated; any nodes generated in the result sequence in the course of this process will become children of the element created from the literal result element.
For example, if the sequence constructor is:
then when the
It is tempting to think of this as a sequence of three steps:
However, this is not a true picture of what is going on, and it is best not to think about it this way; otherwise, you will start wondering, for example, how to delay writing the end tag until some condition is encountered in the input.
The transformation process writes nodes to the result tree; it does not write tags to a text file. The
Figure 3-3
helps illustrate this.
If you do find yourself thinking about where you want tags to appear in the output, it is a good idea to draw a sketch showing the required shape of the result tree and then think about how to write the stylesheet to produce the required nodes on the tree. Because the element in the result tree will always be produced by evaluating one sequence constructor in the stylesheet, this amounts to asking “what condition in the input tree should cause this result element to be generated?”
For example, suppose you want to generate an HTML table with five columns, arranging the
The first template rule matches In XSLT 2.0 problems like this one can also be solved conveniently using the Attributes of a Literal Result Element If the literal result element has attributes, other than the special then the output will contain a copy of this whole structure. The outer
element, and five
elements corresponding to this
not
appear at the start of a new row, and does nothing, because these will already have been processed by the first template rule.
xsl
-prefixed ones in the list above, then these attributes too will be copied to the current output destination. So if the sequence constructor contains:
element is copied to the result tree as before, and this time its content consists of another literal result element, the
element, which is copied to the result tree as a child of the
element, along with its
src
attribute. This time both the stylesheet tree and the result tree take the form shown in
Figure 3-4
.
Other books