XML technologies / XSLT / Introduction to XSLT 2.0 / Getting started with XSLT and XPath / String values and attribute values / The string value of an element node

(Excerpt from "XSLT 2.0 & XPath 2.0" by Frank Bongers, Chapter 1.)

With the document node as the context, the instruction

<xsl:value-of select="gruss"/>

for this structure

<gruss>
  <hallo>Hallo Welt! Dies ist XSLT.</hallo>
</gruss>

produced a result. It produced none, however, for

<hallo>
  <gruss>Hallo Welt! Dies ist XSLT.</gruss>
</hallo>

The latter case is clear: although an element <gruss> exists, it does not satisfy the requirement of the expression child::gruss that it be immediately below the context: the element in between is <hallo>.

In the first case, <gruss> is at the required position and the text node inside it is reached, even though that text node is not directly inside <gruss> but one level further in, inside the element <hallo>. The intervening element is evidently ignored.

Another example illustrates this:

<a>Noch<b> ein <c>anderes</c></b> Stringwertbeispiel.</a>

       ↓        ↓      ↓                      ↓

       Noch   ein     anderes         Stringwertbeispiel.

The string value of <a> consists of the sequence of all text nodes (Note: In effect, the document branch is flattened and the element tags are discarded.) These are combined into a single text node for output, producing the string »Noch ein anderes Stringwertbeispiel.« The string value of <b> would therefore be »ein anderes«, and that of <c> would be »anderes«.

Incidentally, an element’s string value includes only the text nodes it contains, not the values of any comment or attribute nodes. Those nodes do nevertheless have string values of their own.

The string value of the element <person> in the case of

<person nachname="Meier" vorname="Peter"/>

is therefore the empty string. Of course, XPath can still be used to access an attribute value.

   

back to top