[XPath] Reluctant Quantifiers
By default, a regular expression or subexpression matches
the longest possible string. The quantifiers +
, *
, and
?
match as many characters as
possible. Adding a question mark to the end of a quantifier causes the
expression or subexpression to match the shortest
possible string. The quantifiers are modified as follows:
a+?
Matches
a
oncea??
Matches
a
, either once or not at all (works only in thematches()
function; more on this later in this section)a*?
Matches
a
, zero times or once (works only in thematches()
function; more on this later in this section)a{x}?
Matches
a
, exactlyx
times (in this case the reluctant qualifier doesn’t change anything)a{x,}?
Matches
a
exactlyx
times (the comma is irrelevant—a reluctant quantifier matches only the minimum length)a{x,y}?
Matches
a
exactlyx
times (the second number,y
, is irrelevant—a reluctant quantifier matches only the minimum length)
As an example, we’ll use the replace()
function. We’ll put square brackets around each match in the
original string. The normal quantifier and the reluctant quantifier work
differently, as we’ll see. Here’s the stylesheet:
<?xml version="1.0" encoding="utf-8"?> <!-- reluctant.xsl --> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:template match="/"> <xsl:text>Original string: 'Call me at 19195551212'
</xsl:text> <xsl:text> replace($x, '([0-9]+)', '[$1]'):
 </xsl:text> <xsl:value-of select="replace('Call ...
Get XSLT, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.