1
0
mirror of https://github.com/alexandrev/xslt-lab.git synced 2026-09-18 19:43:16 +00:00
Files
xslt-lab/site/content/xslt/functions/xsl-non-matching-substring.md
T
alexandrev-tibco 53e90ef86e feat(blog): complete XSLT/XPath reference — 229 function pages
Full coverage of XSLT 1.0, 2.0 and 3.0 elements and XPath functions:
- 59 XSLT elements (xsl:stylesheet → xsl:use-accumulators)
- 170 XPath functions (1.0 node/string/numeric/boolean, 2.0 sequence/
  date/QName/string, 3.0 HOF/map/array/JSON/streaming)
Each page: description, parameters table, return value, 2 runnable
Saxon examples with input XML + stylesheet + output, notes, cross-links.
xsltCompletions.js: all 229 entries now have blogSlug for hover links.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-19 09:28:05 +02:00

3.5 KiB

title, description, date, version, versionLabel, category, syntax, tags
title description date version versionLabel category syntax tags
xsl:non-matching-substring Defines the template body applied to each substring that does not match the regex inside xsl:analyze-string. 2026-04-18T00:00:00Z 2.0 XSLT 2.0 element <xsl:non-matching-substring>
xslt
reference
xslt2

Description

xsl:non-matching-substring is a child element of xsl:analyze-string. Its body is instantiated once for each portion of the analyzed string that falls between matches — the "gaps" around the matched substrings, including any leading or trailing text.

The element has no attributes. Inside it, the context item (.) is the non-matching segment as a string. regex-group() is not meaningful here because no capture groups apply to unmatched text.

Omitting xsl:non-matching-substring effectively discards all non-matching text from the output, which is useful when you only want to extract the matched parts.

Parameters

xsl:non-matching-substring has no attributes. It contains a sequence constructor.

Return value

The nodes or atomic values produced by the sequence constructor, contributed to the output of the enclosing xsl:analyze-string.

Examples

Extracting only the matched parts (discarding non-matches)

Input XML:

<?xml version="1.0" encoding="UTF-8"?>
<log>Error at line 42: file not found. Warning at line 99: deprecated API.</log>

Stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/log">
    <lines>
      <xsl:analyze-string select="." regex="line \d+">
        <xsl:matching-substring>
          <ref><xsl:value-of select="."/></ref>
        </xsl:matching-substring>
        <!-- No xsl:non-matching-substring: surrounding text is discarded -->
      </xsl:analyze-string>
    </lines>
  </xsl:template>
</xsl:stylesheet>

Output:

<lines>
  <ref>line 42</ref>
  <ref>line 99</ref>
</lines>

Preserving non-matching text alongside highlights

Input XML:

<?xml version="1.0" encoding="UTF-8"?>
<note>Remember to call 555-1234 or 555-5678 if urgent.</note>

Stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html"/>

  <xsl:template match="/note">
    <p>
      <xsl:analyze-string select="." regex="\d{{3}}-\d{{4}}">
        <xsl:matching-substring>
          <strong><xsl:value-of select="."/></strong>
        </xsl:matching-substring>
        <xsl:non-matching-substring>
          <xsl:value-of select="."/>
        </xsl:non-matching-substring>
      </xsl:analyze-string>
    </p>
  </xsl:template>
</xsl:stylesheet>

Output:

<p>Remember to call <strong>555-1234</strong> or <strong>555-5678</strong> if urgent.</p>

Notes

  • xsl:non-matching-substring is only valid as a direct child of xsl:analyze-string.
  • If the regex matches the entire string, xsl:non-matching-substring is never instantiated.
  • Leading text before the first match and trailing text after the last match are also treated as non-matching substrings.
  • The context item is a string; node-related functions like name() or parent:: are not applicable here.

See also