title: Can matched regex string from replace() be used in the replacement inside concat()? XSLT 2 or 3
body: <p>I have an example xml file where a <code>&lt;document&gt;</code> node contains child nodes of one <code>&lt;docText&gt;</code> and zero, one or two <code>&lt;researchNote&gt;</code>. When the text string [fn:1] occurs I want to replace that with a <code>&lt;span&gt;</code> containing the 1st instance of <code>&lt;researchNote&gt;</code>, if [fn:2] I want to replace with the second instance of <code>&lt;researchNote&gt;</code>. I get this to work for first instance using <code>replace()</code> when I either do not include the predicate or statically include the predicate as [1] or [2]. When I try to use the matched string using $1 from the regular expression from the integer matched in the <code>replace()</code> I get an error. I would like to find a way in the following XML and XSLT to reference the integer.</p>
  
  <p>This is my XML</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project&gt;
  &lt;document id="doc1"&gt;
        &lt;docText&gt;This is a test of an inline footnote reference[fn:1]. This is a second[fn:2] footnote.&lt;/docText&gt;
        &lt;researchNote&gt;First footnote.&lt;/researchNote&gt;
        &lt;researchNote&gt;Second footnote.&lt;/researchNote&gt;
  &lt;/document&gt;
  &lt;document id="doc2"&gt;
        &lt;docText&gt;This is a test of an inline footnote reference[fn:1].&lt;/docText&gt;
        &lt;researchNote&gt;First footnote.&lt;/researchNote&gt;
  &lt;/document&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>This is my XSL file. I can use either XSLT 3.0 or 2.0</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xsl:stylesheet version="3.0" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
      exclude-result-prefixes="xsl"&gt;
  
      &lt;xsl:output method="html" html-version="5.0" encoding="utf-8"  indent="yes"/&gt;
  &lt;xsl:template match="/"&gt;
      &lt;html&gt;
      &lt;head&gt;&lt;title&gt;Test&lt;/title&gt;&lt;/head&gt;
      &lt;body&gt;&lt;xsl:apply-templates select="project/document/docText"/&gt;&lt;/body&gt;
      &lt;/html&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="docText"&gt;
      &lt;p&gt;
      &lt;xsl:variable name="string1" select="replace(.,'\[fn:(\d)\]', concat('&amp;lt;span class=&amp;quot;fn&amp;quot; id=&amp;quot;',concat(ancestor::document/@id,'-fn'),'&amp;quot;&amp;gt; (',ancestor::document/researchNote[1],')&amp;lt;/span&amp;gt;'))"/&gt;
      &lt;xsl:value-of select="$string1" disable-output-escaping="yes" /&gt;
      &lt;/p&gt;
      &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>This would be part of the desired output</p>
  
  <pre><code>&lt;p&gt;This is a test of an inline footnote reference&lt;span class="fn" id="doc1-fn"&gt; (First footnote.)&lt;/span&gt;. This is a second&lt;span class="fn" id="doc1-fn"&gt; (Second footnote.)&lt;/span&gt; footnote.&lt;/p&gt;
  </code></pre>
  
  <p>I want to use the number matched in <code>\[fn:(\d)\]</code> eg. <code>$1</code>, which in this case would be 1 or 2, in the predicate of <code>ancestor::document/researchNote[]</code> like this <code>ancestor::document/researchNote[$1]</code>. That use gives an error. So, is it possible to do what I want in the replace() function or in a similar manner.</p>
  
  <p>Thanks, Michael</p>