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

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

Effect

When the function is called with no arguments, the effect is the same as supplying the argument
.
—that is, it returns the base URI of the context node. If there is no context node, an error is reported.

When the first argument is present, the function returns the base URI of the input node supplied. This is a property of the node, defined in the data model. If the node was created by parsing raw XML, then the base URI is typically the URI of the resource containing the raw XML used to create the node. If the input XML used external entities, or if it was assembled from multiple source documents using XInclude, then the base URI will identify the original external entity or source document. The base URI of a node may also be altered by using the
xml:base
attribute in the XML. This attribute is defined in the W3 C Recommendation
XML Base
(
www.w3.org/TR/XMLBase
).

The base URI of a node is typically used when resolving a relative URI contained in the value of that node. By definition, relative URIs refer to files (or to be more general, resources) relative to the base URI of the file containing the relative URI.

When nodes are not created directly by parsing raw XML, the concept of base URI is not so clear-cut. XSLT defines that a node in a temporary tree derives its base URI from the base URI of the stylesheet.

If the first argument is supplied, but its value is an empty sequence, the function returns an empty sequence. An empty sequence is also returned if the function is applied to a node that does not have a base URI (for example, a parentless attribute or text node), or if the base URI of the node is unknown.

Usage and Examples

The
base-uri()
function is useful mainly in conjunction with
resolve-uri()
: it gives you a base URI against which a relative URI can be resolved. For example, if you want to locate the document identified by a relative URI held in an
href
attribute in a source document, the following code should be used:

doc(resolve-uri(@href, base-uri(.)))

Where input documents are assembled from multiple external entities, the
base-uri()
function can also be useful for diagnostics, to report where an error or anomaly was found during processing. In one data cleansing application, I was able to create a report in which such anomalies were grouped according to the base URI of the element where they occured, and then sorted according to the document order of those elements, thus simplifying the job of manually correcting the errors.

See Also

doc()
on page 750

document()
on page 754

resolve-uri()
on page 867

static-base-uri()
on page 876

boolean

The
boolean()
function calculates the
effective boolean value
of the supplied argument.

Changes in 2.0

The function has been generalized in XPath 2.0, so it accommodates a wider range of possible arguments.

Signature

Argument
Type
Meaning
value
item()*
The value whose effective boolean value is required
Result
xs:boolean
The effective boolean value of the argument

Effect

Many XPath 2.0 values have an effective boolean value. The
boolean()
function is used to calculate the effective boolean value explicitly, but it is also calculated implicitly in a number of contexts where a boolean value is required: see the
Usage
section below. The rules are as follows:

  • If the argument is an empty sequence, then the effective boolean value is
    false
    .
  • If the argument is a sequence whose first item is a node, then the effective boolean value is
    true
    .
  • If the argument is a singleton
    xs:boolean
    value, the result is the value of the argument.
  • If the argument is a singleton numeric value, the result is
    false
    if the argument is NaN or zero; otherwise, it is
    true
    .
  • If the argument is a singleton instance of
    xs:string
    ,
    xs:anyURI
    , or
    xs:untypedAtomic
    , the result is
    false
    if the string is zero length; otherwise, it is
    true
    .
  • In all other cases, the function reports an error.

Examples

Assume the source document:


  

  


with the variable
$John
bound to the first

element, and
$Mary
bound to the second.

Expression
Result
boolean(//emp[@age=21])
false
boolean(//emp[@age=53])
true
boolean(number($John/@age))
true
boolean(number($Mary/@age))
false
boolean(count($John/*))
false
boolean(string($John/@surname))
false
boolean(string($John/@name))
true
boolean(“true”)
true
boolean(“false”)
true

Usage

In most cases conversion to an
xs:boolean
occurs automatically when the context requires it; it is only necessary to call the
boolean()
function explicitly in order to force a conversion. For example, these rules are invoked automatically when an expression is used as the condition in an
if
expression, in the
satisfies
clause of the
some
and
every
expressions, and for the operands of
and
and
or
. They are also invoked in XSLT stylesheets for expressions used in

and

instructions.

Other books

The One That Got Away by Megan Hussey
Creature in Ogopogo Lake by Gertrude Chandler Warner
Atlantis: Devil's Sea by Robert Doherty
Maverick Marshall by Nelson Nye
A Murder of Mages by Marshall Ryan Maresca
Faithful by Louise Bay
Twice in a Lifetime by Dorothy Garlock
Awaken by Rachel D'Aigle