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

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

Testing for Vendor Extensions

The previous example also demonstrates a second way of using the function, namely to test for vendor or third-party extensions. If you know that a particular extension instruction is present in some implementations and not others, you can use the
element-available()
test to see whether it is present, and again use either

or

to handle the situation when it isn't. The example
split.xsl
used this technique to test whether the

instruction was available.

An alternative to using the
element-available()
function is to use the

mechanism described in Chapter 3, page 141. An

element allows you to define what processing should occur if its containing instruction isn't available. The two mechanisms are essentially equivalent. A possible limitation of

is that it can only be used within an element that permits element children: it could not be used, for example, within

as currently defined. However, all the new instructions in XSLT 2.0 have been designed so that

can be used as a child element.

See Also

function-available()
on page 792

system-property()
on page 890

type-available()
on page 899


in Chapter 6, page 316

empty

The
empty()
function returns true if and only if the argument is an empty sequence.

For example, the expression
empty(//a)
returns true if the context document contains no

elements.

Signature

Argument
Type
Meaning
sequence
item()*
The input sequence
Result
xs:boolean
true
if the input sequence is empty, otherwise
false

Effect

The function returns true if and only if the supplied sequence is empty.

Examples

Assume the source document:

Expression
Result
empty(/para)
false
empty(/para/a)
false
empty(/para/a/@style)
false
empty(/para/b)
true
empty(/para/a[2])
true

Usage

Note that
empty()
is used only to test whether the number of items in a sequence is zero. As the examples above illustrate, it is
not
used to test whether a node is empty, in the sense of an element that has no children, or an attribute whose string value is a zero-length string.

To test whether an element
$E
has no element or text node children (or comments or processing instructions), you can write
if (empty($E/node()) ...
.

To test whether the string value of a node
$N
is the zero-length string, you can write
if (string($N) eq “”) ...
.

Remember also that a test on any value in the condition of an
if
expression is done by taking the effective boolean value of the expression, as defined under the
boolean()
function on page 721. For example, if the expression is a path expression then the condition is true if the path expression selects one or more nodes; if it is a string, then the condition is true if the string is not zero-length. So, for example:

has the same effect as:

and similarly, if
@a
refers to a list-valued attribute, then:

is equivalent to:

See Also

boolean()
on page 721

exists()
on page 778

not()
on page 850

encode-for-uri

The
encode-for-uri()
function is used to apply percent-encoding to special characters in a string when the string is intended to be used as part of a URI.

For example,
concat(‘http://www.wikipedia.org/’, encode-for-uri(‘Gerhard Schröder’))
returns
http://www.wikipedia.org/Gerhard%20Schr%C3%B6der
.

Other books

Star Shack by Lila Castle
The Way Back Home by Freya North
Cravings by Laurell K. Hamilton, MaryJanice Davidson, Eileen Wilks, Rebecca York
Firewalker by Josephine Angelini
The One Percenters by John W. Podgursky
Every Second Counts by Lance Armstrong