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

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

The term “non-ASCII” here means any Unicode character outside the codepoint range x20 to x7E inclusive. Note in particular that a space character is
not
escaped.

Examples

Expression
Result
escape-html-uri( “http://mhk.me.uk/˜index.html”)
“http://mhk.me.uk/˜index.html”
escape-html-uri(“my doc.xml”)
“my doc.xml”
escape-html-uri(“Grüße.html”)
“Gr%C3%BC%C3%9Fe.html”

Usage

This function is designed for use by applications that generate HTML.

By default, when the HTML or XHTML serialization methods are used (see Chapter 15), all attributes that are defined in the HTML/XHTML specification as containing URIs will be escaped by the serializer, using the rules defined for this function. In principle, therefore, the necessary escaping is done automatically.

There are two situations where escaping sometimes needs to be done manually, that is, by means of explicit calls on this function:

  • When the output contains URIs in attributes that are not defined as URI attributes in the HTML and XHTML specifications. This can arise because there are many extensions of HTML in popular use, and because XHTML allows embedding of other XML vocabularies such as SVG and MathML that may themselves make use of URIs.
  • When the automatic action of the serializer has been disabled by setting the serialization property
    escape-uri-attributes
    to the value
    no
    . This is sometimes necessary to prevent unwanted escaping, especially of URI attributes that are interpreted locally within the browser. Although the HTML specification states that all URI attributes should be escaped, the reality is that in today's browsers this can sometimes cause problems; for example, when a “URI” is actually JavaScript code, or when a fragment identifier in a hyperlink contains a same-document reference to an anchor whose name contains non-ASCII characters. If automatic URI escaping in the serializer is causing such problems, it can be switched off, and the stylesheet can then perform manual escaping of selected URIs using the
    escape-html-uri()
    function.

See Also

encode-for-uri()
on page 771

iri-to-uri()
on page 811

escape-uri-attributes
option in

: Chapter 15

exactly-one

The
exactly-one()
function returns its argument unchanged, provided that it is a sequence containing exactly one item. In other cases, it reports an error.

Signature

Argument
Type
Meaning
value
item()*
The input value. Although the function signature says that any sequence of items is allowed, a runtime error will occur if the number of items is not exactly one.
Result
item()
The same as the supplied value, after checking to ensure that it contains a single item
.

Effect

The
exactly-one()
function returns its argument unchanged, provided that it is a sequence containing exactly one item. In other cases, it reports an error.

This function is useful with XPath processors that perform pessimistic static type checking, as described in Chapter 5. As such, it is unlikely to be needed in XSLT. Calling this function acts as a promise by the programmer that the argument will be a sequence containing exactly one item. This allows the expression to be used in contexts that require a single value (for example, the operands of the
is
operator) when the processor might otherwise have reported a static type error. The XPath expression is still type-safe, because the check that the sequence does indeed contain a single item will be done at runtime, just as it would with a processor that does not enforce static type checking.

Examples

Assume the source document:


with a schema that defines the
separator
attribute to be optional.

Expression
Result
string-join((“a”, “b”, “c”), /list/@separator)
Succeeds unless the processor is doing static type checking, in which case it gives a compile-time error because the second argument of
string-join()
must not be an empty sequence.
string-join((“a”, “b”, “c”), exactly-one (/list/@separator))
Succeeds whether the processor is doing static type checking or not, because the check that the typed value of
@separator
contains a single item is deferred until runtime.

Usage

This function is never needed unless you are using a processor that does static type checking.

However, you may still find it useful as a way of inserting runtime checks into your XPath expressions, and documenting the assumptions you are making about the input data.

See Also

one-or-more()
on page 853

zero-or-one()
on page 912

treat as
expression on page 678 in Chapter 11

exists

The
exists()
function returns true if and only if a supplied sequence contains at least one item.

Signature

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

Effect

The function returns true if and only if the supplied sequence contains at least one item.

Examples

Assume the source document:

See also .

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

Other books

Saturday Morning by Lauraine Snelling
Of Saints and Shadows (1994) by Christopher Golden
Dorothy Garlock by A Place Called Rainwater
Wise Follies by Grace Wynne-Jones
The Winter Sea by Morrissey, Di
A Wrinkle in Time Quintet by Madeleine L’Engle