title: How to use last() inside xsl:matching-substring?
body: <p>Is there a way to determine if a <code>xsl:matching-substring</code> is the <code>last()</code> matching substring?</p>
  
  <p>Example data:</p>
  
  <pre><code>&lt;data&gt;
      &lt;value&gt;1 A 1 2 B 2 1 C 3&lt;/value&gt;
  &lt;/data&gt;
  </code></pre>
  
  <p>Example XSLT:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0"&gt;
      &lt;xsl:output media-type="xhtml" encoding="UTF-8" /&gt;
      &lt;xsl:template match="/"&gt;
          &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
              &lt;head&gt;
                  &lt;title&gt;xsl:matching-substring problem&lt;/title&gt;
              &lt;/head&gt;
              &lt;body&gt;
                  &lt;h1&gt;xsl:matching-substring problem&lt;/h1&gt;
                  &lt;xsl:apply-templates/&gt;
              &lt;/body&gt;
          &lt;/html&gt;
      &lt;/xsl:template&gt;
      &lt;xsl:template match="//value"&gt;
          &lt;xsl:element name="p" namespace="http://www.w3.org/1999/xhtml"&gt;
              &lt;xsl:analyze-string select="." regex="\p{{Nd}}\s\p{{Lu}}\s\p{{Nd}}" flags="i"&gt;
                  &lt;xsl:matching-substring&gt;
                      &lt;xsl:value-of select="."/&gt;
                      &lt;xsl:if test="not(last())"&gt;
                          &lt;xsl:element name="br" namespace="http://www.w3.org/1999/xhtml" /&gt;
                      &lt;/xsl:if /&gt;
                  &lt;/xsl:matching-substring&gt;
              &lt;/xsl:analyze-string&gt;
          &lt;/xsl:element&gt;
      &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>Result:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
     &lt;head&gt;
        &lt;meta http-equiv="Content-Type" content="xhtml; charset=UTF-8" /&gt;
        &lt;title&gt;xsl:matching-substring problem&lt;/title&gt;
     &lt;/head&gt;
     &lt;body&gt;
        &lt;h1&gt;xsl:matching-substring problem&lt;/h1&gt;
        &lt;p&gt;1 A 12 B 21 C 3&lt;/p&gt;
     &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>So, it seems <code>last()</code> is true also for the first and the second matching substring (in fact, <code>last()</code> seems to return the position of the last character of the matching substring).</p>
  
  <p>Without the <code>xsl:if</code>, i get:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
     &lt;head&gt;
        &lt;meta http-equiv="Content-Type" content="xhtml; charset=UTF-8" /&gt;
        &lt;title&gt;xsl:matching-substring problem&lt;/title&gt;
     &lt;/head&gt;
     &lt;body&gt;
        &lt;h1&gt;xsl:matching-substring problem&lt;/h1&gt;
        &lt;p&gt;1 A 1&lt;br /&gt;2 B 2&lt;br /&gt;1 C 3&lt;br /&gt;&lt;/p&gt;
     &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>But i would prefer <code>&lt;p&gt;1 A 1&lt;br /&gt;2 B 2&lt;br /&gt;1 C 3&lt;/p&gt;</code>. Is there a way to achieve this using XSLT 2.0? (Im using Saxon HE as xslt engine).</p>