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

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

Content

A text node. The element may also be empty. It must not contain other elements such as

.

Effect

Text appearing within a sequence constructor in the stylesheet is copied to the result sequence (in the form of a new text node), whether it is enclosed by

or not. The only direct effect of enclosing text in an

element is that the handling of whitespace is different. A whitespace node appearing in the stylesheet (that is, a text node that consists only of whitespace) is copied to the result tree only if :

  • it appears within an

    element, or
  • an enclosing element has the attribute
    xml:space=“preserve”
    , and this is not overridden by an inner enclosing element specifying
    xml:space=“default”

The
disable-output-escaping
attribute controls whether special characters such as
<
should be escaped (that is, converted to a character reference or entity reference such as
<
) if they appear in the text. The default value is
no
. This attribute is deprecated but is retained for backward compatibility with XSLT 1.0; for further information, see page 945 in Chapter 15.

Usage

There are two main reasons for using

, which are to control the output of whitespace, and to disable the escaping of special characters by the serializer. The
disable-output-escaping
attribute is deprecated in XSLT 2.0, but there are still cases where it is useful; for details, see Chapter 15. The following section describes the more orthodox use of

to control whitespace.

Whitespace Control

The most obvious case where

is useful is to force output of whitespace. An example is given in the XSLT specification. If you write:

 

the space between the first name and the last name will be lost, because it is part of a node that contains whitespace only (a single space character). To force a space to appear between the first name and the last name, write:


 


The arrangement on three lines here is purely for readability, but it does not affect the output, because the newline characters are now in whitespace-only nodes that will not be output.

If you find this long-winded, another way of achieving the same effect is to write:


The other aspect of the problem is to prevent the output of unwanted whitespace. Fortunately in HTML output extra whitespace usually doesn't matter, because the browser will ignore it. For XML or text output, however, avoiding unwanted whitespace can be important.

If you are suffering from excess whitespace in your output, the first thing to establish is whether it comes from the source document or from the stylesheet. If the whitespace is adjacent to text copied from the source document, then it probably comes from the source document, but if it is adjacent to text that appears in the stylesheet, then this is the most likely source. Check also that the unwanted whitespace isn't coming from the serializer, by setting

.

If the unwanted whitespace comes from the source document, consider using

to remove nodes consisting entirely of whitespace, or the
normalize-space()
function to remove leading and trailing spaces around visible text.

The

element can be used to suppress unwanted whitespace that originates in the stylesheet. For example, consider the following template:


   [  ]


The intention here is to output a stage direction enclosed in square brackets. But the text nodes containing the opening and closing square brackets also contain a newline character and several spaces, which will be written to the output destination along with the brackets themselves. To prevent this behavior, the simplest way is to wrap an

element around the text you want to output, thus:

Other books

Rhiannon by Vicki Grove
Extinction Machine by Jonathan Maberry
Something to Be Desired by Mcguane, Thomas
Goblins by Philip Reeve
La Vie en Bleu by Jody Klaire
For the Sake of All Living Things by John M. Del Vecchio
You Changed My Life by Abdel Sellou