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

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

XPath processors are required to maintain duration values to a precision of three decimal places (one millisecond). Some processors may maintain a finer precision than this, but it is optional.

Examples

Expression
Result
seconds-from-duration(xs:dayTimeDuration(“PT1 M30.5 S”))
30.5
seconds-from-duration(xs:duration(“P1 MT150 S”))
30
seconds-from-duration(xs:dayTimeDuration(“-PT0.0055 S”))
-0.0055
seconds-from-duration(xs:yearMonthDuration(“P1 M”))
0

See Also

days-from-duration
on page 745

hours-from-duration
on page 801

minutes-from-duration
on page 832

seconds-from-time

see
seconds-from-dateTime()
on page 873

starts-with

The
starts-with()
function tests whether one string starts with another string.

For example, the expression
starts-with(‘$17.30’
,
‘$’)
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
A collation URI
Result
xs:boolean
True if the containing string starts with the test string, otherwise false

Effect

If there is no
collation
argument, then the system tests to see whether the first
N
characters of the
input
string match the characters in the
test
string (where
N
is the length of the
test
string). If so, the result is
true
; otherwise, it is
false
. Characters match if they have the same Unicode value.

If the
test
string is empty, the result is always
true
. If the
input
string is empty, the result is
true
only if the
test
string is also empty. If the
test
string is longer than the
input
, the result is always
false
.

If either the
input
or the
test
argument is an empty sequence, it is treated in the same way as 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 from the static context.

Examples

Expression
Result
starts-with(“#note”, “#”)
true
starts-with(“yes”, “yes”)
true
starts-with(“YES”, “yes”)
false
starts-with(“yes”, “”)
true

Usage

For more sophisticated string matching, use the
matches()
function, which provides the ability to match against a regular expression. However, the
matches()
function does not give the ability to use a collation.

See Also

contains()
on page 730

ends-with()
on page 773

matches()
on page 808

string-length()
on page 880

static-base-uri

The
static-base-uri()
function returns the base URI from the static context of the XPath expression. In XSLT this will be the base URI of the element in the stylesheet containing the expression.

Signature

Type
Meaning
Result
xs:anyURI
The base URI from the static context of the XPath expression

Effect

The function returns the base URI from the static context. This is determined by the host language in which the expression appears. For an XPath expression used in an XSLT stylesheet, the base URI is the URI of the stylesheet module, unless this is modified with an
xml:base
attribute.

The base URI is used when resolving a relative URI contained in the expression, for example as an argument of the
doc()
or
document()
function.

It is possible that the base URI is unknown. This can happen in XSLT if the stylesheet is supplied as a DOM, or as a character string or input stream with no associated URI. In this case this function returns an empty sequence.

Usage

In XSLT 2.0 and XQuery 1.0 it is well defined how the base URI in the static context is established. If you invoke XPath expressions via an API from a programming language (for example, a Java or .NET API) then there may be no explicit way of setting the base URI, especially if the API was originally designed for XPath 1.0. In this case the
static-base-uri()
function is defined to return an empty sequence.

See Also

base-uri
() on page 719

doc()
on page 750

document()
on page 754

resolve-uri()
on page 867

string

The
string()
function converts its argument to a string. When the argument is a node, it extracts the string value of the node; when the argument is an atomic value, it converts the atomic value to a string in a similar way to the
xs:string()
constructor function.

For example, the expression
string(4.00)
returns the string
“4”
.

Changes in 2.0

The function has been generalized to take a wider range of types as its input.

In XPath 1.0, when a sequence containing several nodes was supplied, the
string()
function returned the string value of the first node, and ignored the rest. This behavior is retained in XPath 2.0 when running in 1.0 backward-compatibility mode; but in 2.0 mode, supplying more than one item in the argument is an error.

Signature

Argument
Type
Meaning
value
(optional)
item()?
The value to be converted. If the argument is omitted, it defaults to the context item.
Result
xs:string
The result of converting the argument to a string
.

Effect

Values of most types can be converted to a string.

If the function is called with no arguments, the effect is the same as supplying
.
(the context item) as the first argument.

If the supplied value is an empty sequence, the result is a zero-length string. (Don't confuse supplying an argument whose value is
()
with not supplying an argument—the effect is different.)

Other books

Breeders by Arno Joubert
Mr Lynch’s Holiday by Catherine O’Flynn
1 The Bank of the River by Michael Richan
Shanghai Sparrow by Gaie Sebold
The Blind Side by Patricia Wentworth
Tonio by Jonathan Reeder
Faking Sweet by J.C. Burke
One Time All I Wanted by Elizabeth, Nicolle