Name

[2.0] <xsl:matching-substring>

Defines what to do when a string matches a regular expression. The regular expression is defined on the regex attribute of the <xsl:analyze-string> element that contains the <xsl:matching-substring> element.

Category

Instruction (this is effectively part of the <xsl:analyze-string> element).

Required Attributes

None.

Optional Attributes

None.

Content

A sequence constructor.

Appears in

The <xsl:analyze-string> element.

Defined in

XSLT section 15, “Regular Expressions.”

Example

Here is an example of <xsl:analyze-string> that uses a regular expression to convert U.S. and Canadian telephone numbers of the form 999-999-9999 to the form +1 (999) 999-9999:

<?xml version="1.0" encoding="utf-8"?>
<!-- analyze-string1.xsl -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="2.0">

  <xsl:output method="text"/>

  <xsl:template match="/">
    <xsl:for-each select="phonelist/phonenumber">
      <xsl:analyze-string select="." 
        regex="([0-9]{{3}})-([0-9]{{3}})-([0-9]{{4}})">
        <xsl:matching-substring>
          <xsl:text>&#xA;+1 (</xsl:text>
          <xsl:value-of select="regex-group(1)"/>
          <xsl:text>) </xsl:text>
          <xsl:value-of select="regex-group(2)"/>
          <xsl:text>-</xsl:text>
          <xsl:value-of select="regex-group(3)"/>
        </xsl:matching-substring>
      </xsl:analyze-string>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>

Our stylesheet uses the regular expression ([0-9]{{3}})-([0-9]{{3}})-([0-9]{{4}}), which creates three regex-groups. (Each set of parentheses represents a regex-group.) When we use ...

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.