XML technologies / XSLT / Reference: XSLT 1.0 and 2.0 elements / text

(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 6.)

A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z

 

The instruction xsl:text creates a result text node. No normalization occurs inside it, so even text containing only whitespace characters is copied unchanged.

Classification

Instruction

Function group

Creating result document output

Introduction

XSLT 1.0

Position in the stylesheet and permitted contents:

The instruction xsl:text can occur anywhere in sequence constructors. Only text nodes are allowed inside, not elements or XSLT instructions. It may also be empty, producing no output.

Attributes:

Standard attributes apply, plus one optional element-specific attribute, disable-output-escaping, discouraged in XSLT 2.0.

disable-output-escaping

Value

"yes" | "no"

Usage

optional

Introduction

XSLT 1.0

Deprecated in XSLT 2.0. Attribute disable-output-escaping can suppress normal escaping of certain characters during text-node serialization.

Set its value to disable-output-escaping="yes". The other permitted value, also the default, is "no". It applies to output formats "xml", "xhtml" and "html", though discouraged in XSLT 2.0.

Purpose:

The instruction xsl:text creates a text node containing its content’s string value. Content is not normalized, though special characters are escaped (escaped) when required. Empty instructions return the empty sequence and produce no result node.

Other methods also create text nodes. For text computed by XPath, xsl:value-of is usually better. For literal text, simply insert it into the sequence constructor of a template rule. For more control, enclose it in an xsl:text instruction.

The instruction is required in two cases:

  • To create text containing only whitespace characters, including spaces, tabs, line feeds and carriage returns.

  • To create text whose content must not be escaped, also possible with xsl:value-of.

Forcing whitespace nodes:
The XSLT processor treats whitespace text inside template rules, such as breaks and spaces between element containers, as insignificant and does not copy it:

<!-- Leerzeichen zwischen Vor- und Nachname wird entfernt: -->
<name>
  <xsl:value-of select="vorname"/> <xsl:value-of select="nachname"/>
</name>

One possible result is:

<name>PeterMüller</name>

To ensure whitespace is copied, enclose it in an xsl:text instruction:

<!-- Leerzeichen wird geschützt: -->
<name>
  <xsl:value-of select="vorname"/>
  <xsl:text> </xsl:text>
  <xsl:value-of select="nachname"/>
</name>

The result is now as intended:

<name>Peter Müller</name>

Alternatively use string function fn:concat():

<name><xsl:value-of select="fn:concat(vorname,'  ',nachname)"/></name>

Suppressing escaping for format “xml”:
Normally the parser replaces characters represented in XML by predefined entities with their corresponding entity in the result, ensuring well-formed XML. For output format "xml", the characters are <, >, &, », and '. Others are not replaced.

Sometimes replacement is unwanted. Writing literal “C&A” into the result already causes difficulties.

First, character & cannot appear literally in the stylesheet: use entity &amp;. Trying

<xsl:text>C&amp;A</xsl:text>

produces “C&amp;A” rather than “C&A”, due to automatic escaping.

Disable escaping to obtain the literal character using attribute disable-output-escaping:

<xsl:text disable-output-escaping="yes">C&amp;A</xsl:text>

This gives “C&A”, though that string cannot belong to well-formed XML.

Technically, setting value "yes" marks every character of the result text node with Boolean flag disable-escaping, suppressing normal escaping for those characters.

Suppressing escaping for formats "html" and "xhtml":
Other characters depend on output format. For "html" and "xhtml", umlauts also use HTML entities. Character “Ä”, even when represented in the stylesheet by character entity &#xC4;, becomes HTML entity &Auml;.

To output literal “Ä”, use disable-output-escaping. Choose a suitable output encoding, here for example ISO-8859-1, to avoid surprises.

disable-output-escaping and format "text":
With output format "text", only text nodes are serialized. All characters appear literally, even those represented by entities or character references in the stylesheet.

Since text output never escapes characters, setting the disable-output-esca­ping attribute is irrelevant here, though harmless.

Examples:

Example 1 – Line break in xsl:text:

The xsl:text instruction copies whitespace text into the result. Alternatively, store it in a global variable for formatting serialized output. This example uses a line break:

<xsl:variable name="umbruch"><xsl:text>
</xsl:text></xsl:variable>

The break is now available as $umbruch throughout the stylesheet and can be output as needed:

<xsl:value-of select="'Ein'"/>
<xsl:value-of select="$break"/>
<xsl:value-of select="'Beispiel'"/>

gives this result source:

Ein Beispiel

Without the additional break, both text nodes from xsl:value-of would run together. (Note: For simplicity, string literals replace real XPath expressions here. This varies the first-name/surname example above.)

Example 2 – Creating ASP with xsl:text:

For result sections not well-formed as XML, such as PHP or ASP here, use xsl:text instructions with escaping disabled:

<xsl:text disable-output-escaping="yes">&lt;%@ Language="VBScript" %&gt;</xsl:text>

The result is as intended:

<%@ Language="VBScript" %>

Without disabling escaping, the result would be

&lt;%@ Language="VBScript" %&gt;

, which would not work as expected.

Element definition:

XSLT 1.0:

<!-- Category: instruction -->
<xsl:text 
  disable-output-escaping = "yes" | "no" >

  <!-- Content: #PCDATA -->
</xsl:text>

XSLT 2.0:

<!-- Category: instruction -->
<xsl:text
  [ disable-output-escaping ]? = "yes" | "no" >

  <!-- Content: #PCDATA -->
</xsl:text>

Note: In XSLT 2.0, attribute disable-output-escaping is deprecated, hence its brackets here.

Cross-references:

Constructors for the other node kinds are xsl:attribute, xsl:comment, xsl:element, xsl:processing-instruction and, since XSLT 2.0, xsl:namespace.

   

  

back to top