Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
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:
See Also
encode-for-uri()
on page 771
iri-to-uri()
on page 811
escape-uri-attributes
option in
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:
Expression | Result |
exists(/para) | true |
exists(/para/a) | true |
exists(/para/a/@style) | true |
exists(/para/b) | false |
exists(/para/a[2]) | false |