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

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

Examples

Expression
Result
substring-after(“my.xml”, “.”)
“xml”
substring-after(“my.xml”, “m”)
“y.xml”
substring-after(“my.xml”, “xml”)
“”
substring-after(“my.xml”, “#”)
“”
substring-after(“”, “#”)
“”
substring-after(“my.xml”, “”)
“my.xml”

Usage

The
substring-after()
function was often used in XPath 1.0 to analyze a string that contains delimiter characters. For example, when the string is a whitespace-separated list of tokens, the first token can be obtained using:

substring-before($s, ‘ ’)

and the rest of the string using

substring-after($s, ‘ ’)

With XPath 2.0, this can be done more robustly using the
tokenize()
function. However, there are still many cases where it is more convenient to use
substring-after()
. For example, to extract the local part of a lexical QName, you can write:

substring-after($qname, ‘:’)

XSLT Example

The following example shows a recursive template that takes a whitespace-separated list as input, and outputs each token separated by an empty


element.


    

    

       select=“concat(normalize-space($list),‘ ’)”/>

    

    

    

    

       

       

          

       

    

 

See Also

contains()
on page 730

substring()
on page 883

substring-before()
in the next section

substring-before

The
substring-before()
function returns that part of a string value that occurs before the first occurrence of some specified substring.

For example, the value of
substring-before(‘print=yes’,
‘=’)
is the string
print
.

Changes in 2.0

An optional
collation
argument has been added.

Signature

Argument
Type
Meaning
value
xs:string?
The containing string
test
xs:string?
The test string
collation
(optional)
xs:string
Identifies the collation to be used for comparing strings
Result
xs:string
A string containing those characters that precede the first occurrence of the test substring within the containing string

Effect

If the containing string (
value
) does not contain the
test
substring, the function returns a zero-length string. Note that this could also mean that the
value
starts with the
test
string; the two cases can be distinguished by calling the
starts-with()
function.

If the
value
does contain the
test
substring, the function returns a string made up of all the characters that appear in the
value
before the first occurrence of the
test
substring.

If either of the strings is an empty sequence or a zero-length string, the function returns a zero-length string.

If a collation is specified, this collation is used to test whether the strings match. See the description of the
contains()
function on page 730 for an account of how substring matching works with a collation. If the collation argument is omitted, the function uses the default collation.

Things get complicated if the collation classifies characters such as space or hyphen as ignorable for sorting purposes. If hyphen is ignorable, then
substring-before(“a-b-c”, “-b”)
returns
a-
. That's because the match chosen for
-b
is the minimal matching substring, which is
b
.

Other books

White Ghost by Steven Gore
A Dangerous Age by Ellen Gilchrist
Sweet by Skye Warren
The Duchess Hunt by Jennifer Haymore
Vampire Rising by Larry Benjamin
Please Release Me by Rhoda Baxter
Special Forces Savior by Janie Crouch
Natural Born Angel by Speer, Scott