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

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
9.45Mb size Format: txt, pdf, ePub
ads
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

elements elsewhere in the stylesheet.

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.

Product code

In this case a

element will be written to the result tree with a child text node whose content is
Product code
. When the result tree is output to an XML or HTML file, it will regenerate the text as it appeared in the stylesheet—or something equivalent. There is no guarantee that it will be character-for-character identical, for example the processor may add or remove whitespace within the tags, or it may represent characters using character or entity references.

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

element is evaluated, its content will also be evaluated. The content in this case is a sequence constructor consisting of a single XSLT instruction, and the effect is that this instruction is evaluated to create a text node that will be a child of the

element in the result tree. The instruction

outputs the typed value of the current node in the source tree, converted to a string. So if this value is
$83.99
, the result would be as follows:

$83.99

It is tempting to think of this as a sequence of three steps:

  • The

    start tag causes a

    start tag to be written to the output.
  • The

    element is evaluated, and the result (
    $83.99
    ) is written to the output.
  • The

    end tag causes a

    end tag to be written to the output.

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

element in the stylesheet causes a

element to be written to the result tree. You cannot write half a node to the tree—the start and end tags are not written as separate operations. The

and

tags are generated only when the result tree is serialized as XML or HTML.

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

elements from the source XML five to a row. Then the condition in the source XML that causes an output row to be generated is an

element whose position is 1, 6, 11, and so on. The logic can be written:



  

    

  




The first template rule matches

elements that should appear at the start of a new row; it outputs the

element, and five

elements corresponding to this

and its four following siblings. The second rule matches

elements that should
not
appear at the start of a new row, and does nothing, because these will already have been processed by the first template rule.

In XSLT 2.0 problems like this one can also be solved conveniently using the

instruction, which is described with examples in Chapter 6 on page 326. However, for simple cases like this, the approach shown above works just as well.

Attributes of a Literal Result Element

If the literal result element has attributes, other than the special
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:


then the output will contain a copy of this whole structure. The outer

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
.

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

Other books

WaltzofSeduction by Natasha Blackthorne
The Peppered Moth by Margaret Drabble
Pocahontas by Joseph Bruchac
Hooked by Stef Ann Holm
Pasadena by David Ebershoff
The Chastity Collection by Daniels, Daiza