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

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

in the regex, and is therefore available as
regex-group(3)
. Rather than work out whether group 2 or group 3 was matched, the XSLT code simply outputs both: the one that was not matched will be a zero-length string, so it is simpler to copy it to the output than to write the conditional code to find out whether it was actually matched.

A full stylesheet containing this example is shown under the
unparsed-text()
function (page 907).

See Also


on page 230 in Chapter 6

remove

The
remove()
function returns a sequence that contains all the items in an input sequence except the one at a specified position.

Signature

Argument
Type
Meaning
sequence
item()*
The input sequence
position
xs:integer
The position of the item to be removed
Result
item()*
A sequence containing all the items in the input sequence except the item at the specified position

Effect

The effect is the same as the expression:

$sequence[position() ne $position]

This means that if the
position
parameter is less than one or greater than the number of items in the input sequence, the input sequence is returned unchanged.

Examples

Expression
Result
remove((1 to 5), 4)
1, 2, 3, 5
remove((1 to 5), 10)
1, 2, 3, 4, 5
remove((), 1)
()

Usage

A common requirement, especially in recursive functions, is to get the
tail
of a sequence, that is, all items except the first. There are several ways of doing this in XPath 2.0, all equivalent. Take your pick:

  • $sequence[position() ne 1]
  • subsequence($sequence, 2)
  • remove($sequence, 1)

There's no intrinsic reason why any of these should perform better or worse than the others—it all depends on the implementation. In Saxon, all three generate the same runtime code.

See Also

insert-before()
on page 810

subsequence()
on page 882

replace

The
replace()
function constructs an output string from an input string by replacing all occurrences of substrings that match a supplied regular expression with a given replacement string. The replacement string may include references to captured groups within the input string.

Signature

Argument
Type
Meaning
input
xs:string?
The input string. If an empty sequence is supplied, an empty sequence is returned.
regex
xs:string
The regular expression, written according to the rules given in Chapter 14.
replacement
xs:string
The replacement string.
flags
(optional)
xs:string
One or more letters indicating options on how the matching is to be performed. If this argument is omitted, the effect is the same as supplying a zero-length string, which defaults all the option settings.
Result
xs:string?
The string produced by replacing substrings of the input string that match the regular expression
.

Effect

The rules for the syntax of regular expressions and for the flags argument are given in Chapter 14.

The input string is processed from left to right, looking for substrings that match the regular expression supplied in the regex argument. Characters that don't participate in a match are copied unchanged to the output string. When a substring is found that does match the regex, the substring is not copied to the output, but the replacement string is copied instead. The search then resumes at the character position following the matched substring. For example, the result of
replace(“banana”, “a”, “A”)
is
bAnAnA
.

It can happen that two substrings starting at the same position both match the regex. There are two ways this situation can arise.

Firstly, it happens when part of the regex is looking for repeated occurrences of a substring. For example, if the regex is
(an)*a
then immediately after the
b
of
banana
, there are three possible matches, the matched substrings being
a
,
ana
, and
anana
. The rule here is that
*
is a greedy quantifier: it matches as long a substring as it can. So the result of the expression
replace(“banana”, “(an)*a”, “#”)

Other books

Wolf in Shadow-eARC by John Lambshead
Strength and Honor by R.M. Meluch
Embracing the Fall by Lainey Reese
Trouble in Texas by Katie Lane
Death of the Party by Carolyn Hart