XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition (532 page)

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
13.57Mb size Format: txt, pdf, ePub

Usage in XSLT

The
concat()
function is often a convenient alternative to using multiple

elements to construct an output string. For example, the following expression creates a text node containing a concatenation of three strings:


This is equivalent to:


  

   

  


(Note the need for the outer

instruction—without this, the result would be a sequence of three text nodes rather than a single text node.) However, with XSLT 2.0 it is even simpler to write:


Another situation where
concat()
is useful is in defining a key, including look-up keys (

), sort keys (

), and grouping keys (

). XSLT keys cannot be multipart values, but you can get round this restriction by concatenating the parts of the key with an appropriate separator. For example:

                          use=“concat(first-name, ‘ ’, last-name)”/>

This key can then be used to retrieve the person (or persons) with a given name using an expression such as:


See Also

contains()
in the following section

string-join()
on page 879

substring()
on page 883

contains

The
contains()
function tests whether one string contains another as a substring. For example, the expression
contains(‘Santorini’,‘ant’)
returns true.

Changes in 2.0

An optional
collation
argument has been added.

Signature

Argument
Type
Meaning
input
xs:string?
The containing string
test
xs:string?
The test string
collation
(optional)
xs:string
The collation to be used
Result
xs:boolean
true if the containing string has a substring that is equal to the test string, otherwise false

Effect

If the Unicode codepoint collation is used (this is the default), then the result is
true
if the first string (
input)
contains a consecutive sequence of characters where each character has the same Unicode value as the corresponding character of the second string (
test
).

If
test
is zero-length, the result is always
true
.

If
input
is zero-length, the result is
false
except when
test
is also zero-length.

If either of the first two arguments is an empty sequence, the effect is the same as if it were a zero-length string.

If no collation is specified, then the default collation from the static context is used. When a collation is used, it is used to break both of the strings into a sequence of collation units, and the function returns
true
if the collation units generated for
test
form a subsequence of the collation units generated for
input
.

Because this function compares substrings, rather than just performing an equality match or ordering on two strings as a whole, it imposes particular constraints on the way the collation works—it only makes sense to use a collation that considers the string character by character. For a function such as
compare()
, it would be quite viable to use a collation that sorts
January
before
February
, or
5 Oak Street
before
10 Maple Drive
. But a collation that does this isn't also going to be able to look for substrings of characters in a meaningful way.

Other books

Rumbo al cosmos by Javier Casado
Soul Broker by Tina Pollick
No Biz Like Show Biz by Nancy Krulik
Dyscountopia by Niccolo Grovinci
Down the Rabbit Hole by Monica Corwin
Lord Harry's Folly by Catherine Coulter
The Ophelia Prophecy by Sharon Lynn Fisher