Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
If the supplied value is a single node, the result is the string value of that node. The string value of a node is defined as follows:
If the supplied value is a single atomic value the result is the same as the result of casting the atomic value to a string. Every atomic value can be cast to a string—the rules are given in Chapter 11.
Note that taking the string value of a node is not the same as taking the typed value and converting it to a string. For example, the typed value might be a sequence of integers, but no conversion is defined from a sequence of integers to a string. In some cases a node has no typed value, notably in the case where the schema defines it as having element-only content (as distinct from mixed or empty content). Such an element has no typed value, but it still has a string value that is the concatenation of the desendant text nodes.
The type signature does not allow a sequence of more than one item to be supplied. However, if XPath 1.0 compatibility mode is enabled, any items in the sequence after the first are ignored.
Examples
Assume that the context node is the element:
Expression | Result |
string() | “17blue” |
string(first) | “17” |
string(second) | “blue” |
string(@example) | “yes” |
string(+47.20) | “47.2” |
string(2=2) | “true” |
string(*) | In 1.0 mode: “17” In 2.0 mode: error |
Usage
When converting atomic values to strings, there isn't really anything to choose between using the
string()
function and using the
xs:string()
constructor function.
When the argument is a node, the two functions behave differently. The
string()
function extracts the string value of the node, while
xs:string()
extracts the typed value and converts it to a string. In the absence of a schema, they do exactly the same thing. But with a schema, here are two cases where they can give different results:
See Also
boolean()
on page 721
number()
on page 851
Converting Atomic Values
on page 654 in Chapter 11
string-join
The
string-join()
function returns a string constructed by concatenating all the strings in a supplied sequence, with an optional separator between adjacent strings.
For example,
string-join((“a”,“b”,“c”), “|”)
returns
a|b|c
.
Signature
Argument | Type | Meaning |
sequence | xs:string* | The supplied sequence of strings. |
separator | xs:string | The separator to be used between adjacent strings. If no separator is required, supply a zero-length string for this argument. |
Result | xs:string | The result of concatenating the supplied strings and inserting separators . |
Effect
Each of the strings in the supplied sequence is appended to the result string, retaining the order in which the strings appear in the sequence. Each string except the last is followed by the requested
separator
string.
If the supplied sequence is empty, the result is always a zero-length string.
Examples
Expression | Result |
string-join((“a”, “b”, “c”), “, ”) | “a, b, c” |
string-join((“A”, “B”, “C”), “”) | “ABC” |
string-join(“Z”, “+”) | “Z” |
string-join((), “∼”) | “” |
Usage
The expression:
string-join(ancestor-or-self::*/name(), “/”)
will return a path such as:
book/chapter/section/title
Note that there is no implicit conversion of the items in the sequence to strings, even in XPath 1.0 compatibility mode. If the items are not strings, you need to convert them explicitly. For example, given a sequence of numbers, you can write:
string-join($seq/string(), “, ”)
The
string-join()
function is often a handy alternative to
concat()
, because you can in effect give it a sequence of sequences to output. For example:
string-join((“debits:”, $debits, “credits:”, $credits), “ ”)
might produce the string:
debits: 23.40 18.50 67.00 credits: 17.00 5.00 4.32
In XSLT, functionality very similar to the
string-join()
function is invoked implicitly by the
produces the result
. The
separator
attribute to allow user control over the choice of separator, while attribute value templates always use a single space.
See Also
concat()
on page 729
string-length
The
string-length()
function returns the number of characters in a string value.
For example, the expression
string-length(‘Beethoven’)
returns 9.
Changes in 2.0
None.
Signature
Argument | Type | Meaning |
value (optional) | xs:string ? | The string whose length is required. If the argument is an empty sequence, the result of the function is 0 (zero). |
Result | xs:integer | A number: the number of characters in the value of the argument . |
Effect
When the argument is omitted, the function gives the string length of the value obtained by applying the
string()
function to the context item. It's an error if there is no context item.
Characters are counted as instances of the XML
Char
production. This means that a Unicode surrogate pair (a pair of 16-bit values used to represent a Unicode character in the range
#x10000 to #x10FFFF
) is treated as a single character.
It is the number of characters in the string that matters, not the way they are written in the source document. A character written using a character reference such as
ÿ
or an entity reference such as
&
is still one character.