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

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

A
$0
in the replacement string refers to the entire matched string.

If the replacement string contains a variable that hasn't been matched, perhaps because the relevant parenthesized subexpression was in a branch that wasn't used, then a zero-length string is substituted for the variable. If the subexpression was matched more than once, then it's the last one that is used.

If the regex does not match the input string, the
replace()
function will return the input string unchanged. If this is not the effect you are looking for, use the
matches()
function first to see if there is a match.

If the regex is one that matches a zero-length string, that is, if
matches(“”, $regex)
is true, the system reports an error. An example of such a regex is
a*
. Although various interpretations of such a construct are possible, the Working Group decided that the results were too confusing and decided not to allow it.

Examples

Expression
Result
replace(“banana”, “a”, “o”)
“bonono”
replace(“banana”, “(ana|na)”, “[$1]”)
“b[ana][na]”
replace(“banana”, “(an)+”, “**”)
“b**a”
replace(“banana”, “(an)+?”, “**”)
“b****a”
replace(“facetiously”, “[aeiouy]”, “[$0]”)
“f[a]c[e]t[i][o][u]sl[y]”

Usage

The
replace()
function provides a much-needed string replacement capability for XPath. In XPath 1.0 it was possible to do simple one-for-one character replacement using the
translate()
function, but anything more complex required the use of cumbersome recursive templates in XSLT.

One limitation of the
replace()
function, however, is that the result is always a string: this function cannot be used directly for so-called
up-conversion
applications where the aim is to generate markup within the string (a typical example of such a conversion is the requirement to replace newlines in a string by empty


elements). For such applications, the XSLT

instruction is more powerful.

See Also


in Chapter 6, page 230

matches()
on page 828

tokenize()
on page 894

translate()
on page 897

Regular Expressions, Chapter 14

resolve-QName

The
resolve-QName()
function returns a value of type
xs:QName
(that is, an expanded QName consisting of a namespace URI and a local name), taking as input a lexical QName (a string in the form
prefix:local-name
or simply
local-name
), by resolving the prefix used in the lexical QName against the in-scope namespaces of a given element node.

Signature

Argument
Type
Meaning
lexical-qname
xs:string?
The lexical QName whose prefix is to be resolved. It must conform to the syntax of a QName as defined in the XML Namespaces specification (which is the same as the lexical space for an
xs:QName
defined in XML Schema).
element
element()
An element node whose in-scope namespaces are to be used to resolve the namespace prefix used in the lexical QName.
Result
xs:QName?
The expanded
xs:QName
, containing the namespace URI corresponding to the prefix supplied in the lexical QName
.

Effect

If the first argument is an empty sequence, the function returns an empty sequence.

The local-name part of the resulting
xs:QName
value will always be the same as the local-name part of the supplied lexical QName: that is, the part after the colon, if there is a colon, or the whole string otherwise. The constructed
xs:QName
will also retain any prefix that was present in the supplied lexical QName.

If the lexical QName has no prefix, then the default namespace for the given element will be used. More precisely, the system looks for an unnamed namespace node of the given element. If it finds one, then the namespace URI component of the result is taken from the string value of this namespace node. If there is no unnamed namespace node, then the namespace URI component of the resulting
xs:QName
value will be null.

Other books

Off Limits by Delilah Wilde
The Heretic's Apprentice by Ellis Peters
Then We Die by James Craig
Lady Doctor Wyre by Joely Sue Burkhart
The Red Eagles by David Downing
Marrying Off Mother by Gerald Durrell