XML technologies / XSLT / Reference: XSLT 1.0 and 2.0 elements / value-of

(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:value-of copies the string value of the XPath expression in its select attribute into the result as a text node. Without select, XSLT 2.0 may instead use an optional sequence constructor’s string value.

Classification

Instruction

Function group

Copying string values to the result

Introduction

XSLT 1.0

Position in the stylesheet and permitted content:

The instruction xsl:value-of can occur wherever a sequence constructor needs text nodes.

In XSLT 2.0, xsl:value-of may contain a sequence constructor only when its select attribute is absent; otherwise it must be empty. In XSLT 1.0 it is always empty.

Attributes:

Standard attributes apply. In addition, xsl:value-of has three optional attributes: select (required in XSLT 1.0), disable-output-escaping (deprecated in XSLT 2.0) and separator (new in XSLT 2.0).

disable-output-escaping

Value

"yes" | "no"

Usage

optional

Introduction

XSLT 2.0

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

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

select

Value

xpath-expression

Usage

optional (in XSLT 1.0 required).

Introduction

XSLT 2.0

An XPath expression specifying nodes and/or atomic values, atomized where necessary. For one or more items, string values are joined by spaces unless attribute separator is set. An empty sequence returns the empty sequence. Backwards-compatible mode uses only the first item’s string value.

Without attribute select, XSLT 2.0 allows a sequence constructor whose string value is used. If both are absent, the empty sequence is returned. If select is present and the instruction is nonempty, this is a static error.

separator

Value

{string} (also as an AVT)

Usage

optional

Introduction

XSLT 2.0

The value is a string xs:string used as the separator. Setting attribute separator returns a text node joining all input items, or constructor results, with this separator. An empty input always returns the empty sequence, regardless of the separator.

A non-atomic item, such as an element, is atomized first. An empty separator joins item string values directly.

Default behavior:
Without attribute separator, three cases apply: backwards-compatible processing with select, normal mode with select, or normal mode with a sequence constructor.

  • Backwards-compatible behavior:
    Only the first item’s string value from select is returned; the rest are discarded. XSLT 1.0 behaves this way and has no separator attribute: only the first node’s string value is output.

  • Normal mode with select:
    The instruction behaves as if attribute separator had a single space (#x20) as value, producing a space-separated list of select item strings.

  • Normal mode with a sequence constructor:
    The instruction behaves as if attribute separator had an empty string, joining constructor item strings directly.

Purpose:

The instruction xsl:value-of creates a result text node from the XPath expression in attribute select. Its input can be any node or atomic-value sequence. An empty result produces no output.

Non-atomic input items are atomized; an element’s string value becomes text.

Defaults differ between XPath 1.0 and XPath 2.0:
For several atomized items, behavior depends on normal or backwards-compatible processing. The latter outputs only the first item’s string value, as XSLT 1.0 does by default. Force this in XSLT 2.0 with version attribute value "1.0".

Conversely, an attribute version with value "2.0" or greater disables backwards compatibility. The same applies if attribute version is not explicitly set on xsl:value-of but inherited from an ancestor such as xsl:styles­heet, unless overridden by a version attribute on an intervening instruction.

Normal XSLT 2.0 processing joins every atomized input string with a space. An explicit separator attribute supplies a different separator instead.

Examples:

Example 1a – extracting child-element string values:

XML document (excerpt):

...
<name>
  <vorname>Peter</vorname>
  <nachname>Meier</nachname>
</name>
...

Stylesheet (excerpt):

<xsl:template match="name">
  <p>
    <xsl:value-of select="vorname"/>
    <xsl:text> </xsl:text>
    <xsl:value-of select="nachname"/>
  </p>
</xsl:template>

produces (excerpt):

...
<p>Peter Meier</p>
...

In the current template rule, an element <name> is the current node and has two children, <vorname> and <nachname>. Instruction xsl:value-of extracts both string values, while instruction xsl:text forces a space between first and last name.

The returned string joins all descendant text nodes, including whitespace nodes between elements, which may introduce tabs and line breaks into the result.

Example 1b – extracting child-element string values:

With the same source but this stylesheet:

<xsl:template match="name">
  <p>
    <xsl:value-of select="."/>
  </p>
</xsl:template>

the possibly unexpected result is:

<p>
   Peter
   Meier
</p>

HTML may tolerate this, but source formatting can introduce unwanted spacing. Controlled extraction or normalization before output is preferable. Another solution is:

<xsl:value-of select="fn:normalize-space(.)"/>

Example 2a – select sequence without separator:

<p>
  <xsl:value-of select="(1,2,3,4,'Hallo','Welt!')"/>
</p>

produces:

<p>1 2 3 4 Hallo Welt!</p>

The four literal items are joined with spaces, the default separator ' '.

Example 2b – select sequence with separator:

<p>
  <xsl:value-of select="(1,2,3,4,'Hallo','Welt!')" separator=" + "/>
</p>

produces:

<p>1 + 2 + 3 + 4 + Hallo + Welt!</p>

The four literal items are joined with separator ' + ' between each pair.

Example 3a – with a sequence constructor:

<p>
  <xsl:value-of>
    <xsl:sequence select="(1, 2, 3, 4)"/>
    <foo>Hallo</foo>
    <bar>Welt!</bar>
  </xsl:value-of>
</p>

produces:

<p>1234HalloWelt!</p>

Literal result elements, XSLT instructions or both produce items that are atomized and joined as strings. Instruction xsl:value-of behaves as if attribute separator had value separator="". Whitespace-only text nodes disappear because their string value is empty.

Example 3b – with a sequence constructor and separator:

<p>
  <xsl:value-of separator=" ">
    <xsl:sequence select="(1, 2, 3, 4)"/>
    <foo>Hallo</foo>
    <bar>Welt!</bar>
  </xsl:value-of>
</p>

produces:

<p>1 2 3 4 Hallo Welt!</p>

The separator attribute must explicitly supply a space to avoid direct concatenation of constructor item strings. Whitespace-only text nodes still disappear.

Element definition:

XSLT 1.0:

<!-- Category: instruction -->
<xsl:value-of 
     select = string-expression 
     disable-output-escaping = "yes" | "no" 
/>

XSLT 2.0:

<!-- Category: instruction -->
<xsl:value-of
     select? = expression
     separator? = { string }
     [ disable-output-escaping ]? = "yes" | "no" >

     <!-- Content: sequence-constructor? -->
<xsl:value-of/>

Note: In XSLT 2.0, attribute disable-output-escaping is deprecated and is therefore shown in brackets.

   

  

back to top