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

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

Usage

The
QName()
function is useful when you want to compare node names against a specified name, especially one determined at runtime. This is done by using it in conjunction with the
node-name()
function.

For example, suppose that you are dealing with source documents for which several variants of the schema exist, all using different namespace URIs, and suppose that the actual namespace URI to be used in a particular run is passed in as a parameter. This makes it difficult to use path expressions in the natural way, because path expressions can only refer to names that are fully known (that is, both the namespace URI and local name are known) at compile time.

You can define a set of global variables like this:

 

 

and so on.

It is then possible to use path expressions such as:

select=“*[node-name()=$address]/*[node-name()=$postalcode]”

to locate nodes.

See Also

local-name-from-QName()
on page 826

namespace-uri-from-QName()
on page 841

node-name()
on page 843

regex-group

This function is available in XSLT only
.

The
regex-group()
function returns a captured substring resulting from matching a regular expression using the

instruction.

Signature

Argument
Type
Meaning
group
xs:integer
Identifies the captured subgroup that is required. The
n
th captured subgroup provides the string that was matched by the part of the regular expression enclosed by the
n
th left parenthesis.
Result
xs:string
The string that was matched by the
n
th subexpression of the regular expression
.

Effect

When the

instruction is used to match a string against a regular expression (regex), its

child element is invoked once for each substring of the input string that matches the regular expression. The substring that matched the regex can be referred to within this element as
.
, because it becomes the context item, and for consistency with other regex languages it is also available as the value of
regex-group(0)
. Sometimes, however, you need to know not only what the substring that matched the regex was, but which parts of that substring matched particular parts of the regex. The group of characters that matches a particular parenthesized subexpression within the regex is referred to as a
captured group
; and the captured group that matches the
n
th parenthesized subexpression is accessible as the value of
regex-group(n)
.

The substrings matched by

are available during the execution of the sequence constructor within the

element, including any templates called from instructions within this sequence constructor, that is, the scope is dynamic rather than static.

Note that it is only the

instruction that makes captured groups available. They are not made available by the regex functions
matches()
,
replace()
, and
tokenize()
.

Usage and Examples

The
regex-group()
function is always used together with the

instruction, described in Chapter 6, page 230.

For example, suppose you are analyzing a comma-separated-values file containing lines like this.

423,“Barbara Smith”,“General Motors”,1996-03-12

Given this line as the content of variable
$
in, you might analyze it using the code:


”]*?)”)|([

,]+?),’>

  

    

      

      

    

  


The regex here has two alternatives. The first alternative,
(“([

”]*?)”)
, matches a string enclosed in quotes. The second alternative,
([

,]+?),
, matches a sequence of non-comma characters followed by a comma. If a string within quotes is matched, then the characters between the quotes are matched by the
[

‘’]*?
part of the regex. This appears after the second
(
in the regex, so the string that it matches is available as
regex-group(2)
. If a nonquoted string is matched, it is matched by the
[

,]+?
part, which appears after the third
(

Other books

The Persian Price by Evelyn Anthony
Jenna Starborn by Sharon Shinn
Seduce Me by Robyn DeHart
Where by Kit Reed
The Sundial by Shirley Jackson
A Bloodhound to Die for by Virginia Lanier
Jake Walker's Wife by Lough, Loree