[XPath] Anchors
In XML Schema, regular expressions are anchored; in other
words, the regular expression is assumed to start at the beginning of
the text and end at the end of the text. That means the expression
abc
matches only the three-character
string abc
. If there are any extra
characters before or after the letters abc
, XML Schema does not consider the string a
match.
The regular expression language in XPath 2.0 doesn’t work that way. When we use any of the regular
expression functions or elements, a string matches if it contains the
regular expression anywhere inside it. In other
words, using XPath’s regular expression language, both abc
and I know my
abc's
match the expression abc
. XPath provides the traditional anchor
operators used in other regular expression languages. The caret
(^
) matches the start of the string,
while the dollar sign ($
) matches the
end of the string. If multiline mode is on (-m
), the caret matches the start of the string
and any character immediately following a newline. Similarly, in
multiline mode, the dollar sign matches the end of the string and any
character immediately before a newline.
Here’s an example that illustrates how the anchors work:
<?xml version="1.0" encoding="utf-8"?> <!-- anchors.xsl --> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:template match="/"> <xsl:text>matches('abcdefghij', 'cde'): </xsl:text> <xsl:value-of select="if (matches('abcdefghij', 'cde')) then 'It''s ...
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.