XML technologies / XSLT / Introduction to XSLT 2.0 / Getting started with XSLT and XPath / A comparison: expression vs. instruction / Singletons and duplicate nodes
(Excerpt from "XSLT 2.0 & XPath 2.0" by Frank Bongers, Chapter 1.)
For a sequence, it does not matter whether its XPath expressions yield different or identical results. Duplicates are allowed. Here is a very simple example: an element containing a string:
<lachen>ha ha</lachen>First, as a simple exercise, we retrieve the element’s string value. The XPath expression consists solely of the element name:
<xsl:value-of select="lachen"/> produces: ha ha
Now a sequence is used instead of the expression. A sequence consisting of just one expression has the same effect as that expression alone:
<xsl:value-of select="(lachen)"/> produces: ha ha
Warning:
In XPath 1.0, the parentheses themselves cause an error. Otherwise, this illustrates that a sequence containing a single Item is identical to that item itself. Such a sequence is known as a singleton sequence.
Now the expression is simply used twice in succession:
<xsl:value-of select="(lachen, lachen)"/> produces: ha ha ha ha
This is where it becomes interesting. First, we see a general fact about sequences: a sequence may contain a node more than once. Strictly speaking, of course, a sequence never contains the node itself, but a pointer to it.
Remember:
The beginning and end of a literal sequence are marked by parentheses, and its items are separated by commas. Sequences cannot be nested. Items are never themselves sequences, but either atomic values or nodes; the same node may appear more than once in a sequence.
Copyright © Galileo Press, Bonn 2008
You may print the online version for your private use.
Otherwise, this chapter from "XSLT 2.0 & XPath 2.0 ― Das umfassende Handbuch" is subject to the same terms as the printed edition: the work and all its parts are protected by copyright. All rights are reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
Galileo Press, Rheinwerkallee 4, 53227 Bonn