XML technologies / XSLT / Reference: XSLT 1.0 and 2.0 elements / matching-substring
(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 6.)
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z
Sub-instruction xsl:matching-substring processes, within a sequence generated by xsl:analyze-string, the input substrings matching the pattern.
Classification | Sub-instruction |
Function group | Test string values |
Introduced | XSLT 2.0 |
Position in the stylesheet and permitted contents:
Sub-instruction xsl:matching-substring is the first child in the template body of xsl:analyze-string.
Its contents are a sequence constructor processing the substring from the sequence generated by xsl:analyze-String. The body may contain text, arbitrary Literal Result Elements and XSLT instructions.
Attributes:
For xsl:matching-substring, standard attributes apply; no element-specific attributes exist.
Purpose:
The instruction xsl:matching-substring processes substrings for which the regular expression in xsl:analyze-string produces a match (match). It runs once for every match, in the order determined by the output sequence of xsl:analyze-string. At most one sub-instruction xsl:matching-substring may appear in xsl:analyze-string, followed by at most one xsl:non-matching-substring and any number of xsl:fallback elements.
Closing tag </xsl:matching-substring> is mandatory. Empty-element syntax is forbidden even with an empty body. In that case the element may instead be omitted entirely from xsl:analyze-string.
Subpatterns and function regex-group():
If parentheses define subpatterns in the regular expression of xsl:analyze-string, their matching substrings are stored in temporary variables and can be extracted with regex-group(), whose signature is:
regex-group($group-number as xs:integer) as xs:string
Integer arguments identify the subpattern. For example, regex-group(2) returns the string in the second subpattern’s temporary variable.
For regular expression "(\d\d)\.(\d\d)", matching two digit pairs separated by a dot, argument 1 returns the first pair and argument 2 the second.
An argument with no corresponding subpattern, here for example 3, returns an empty string, as does an existing subpattern containing an empty string.
Outside an xsl:matching-substring instruction, the function always returns an empty sequence.
Example – regex-group() in xsl:matching-substring:
Source document:
<?xml version="1.0" encoding="UTF-8"?>
<beispiel>
Ein ganz beliebiger String,
der die Zeichenfolge 47.11 enthält.
</beispiel>
Stylesheet:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"version="2.0">
<xsl:template match="/">
<ergebnis>
<xsl:analyze-string select="beispiel" regex="(\d\d)\.(\d\d)">
<xsl:matching-substring>
<zahl1>Zahl 1: <xsl:value-of select="regex-group(1)"/></zahl1>
<zahl2>Zahl 2: <xsl:value-of select="regex-group(2)"/></zahl2>
</xsl:matching-substring>
<xsl:non-matching-substring>
<rest><xsl:value-of select="."/></rest>
</xsl:non-matching-substring>
</xsl:analyze-string>
</ergebnis>
</xsl:template>
</xsl:stylesheet>
Two subpatterns in the regular expression of a found matching-substring should be output separately.
Result:
<?xml version="1.0" encoding="UTF-8"?>
<ergebnis>
<rest> Ein ganz beliebiger String, der die Zeichenfolge </rest>
<zahl1>Zahl 1: 47</zahl1>
<zahl2>Zahl 2: 11</zahl2>
<rest> enthält.</rest>
</ergebnis>
The two match parts are output separately; the dot between the digit pairs is discarded.
Element definition:
XSLT 1.0:
Element unavailable in XSLT 1.0.
XSLT 2.0:
<!-- Category: sub instruction -->
<xsl:matching-substring>
<!-- Content: sequence-constructor -->
</xsl:matching-substring>
Copyright © Galileo Press, Bonn 2008
You may print the online version for your private use.
Otherwise, this chapter from “XSLT 2.0 & XPath 2.0 ― Das umfassende Handbuch” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
Galileo Press, Rheinwerkallee 4, 53227 Bonn