title: XSLT 2.0: Filtering through nodes / text with analyze-string but needing to also grab XML tags
body: <p>I'm working through my first major XSLT project, and am a bit of a novice, so please be patient with my ignorance.</p>
  
  <p>Our group is working on a transform of existing XMLs to an entirely different tagging system. I have devised a system of processing the MathType callouts (signified by "${TEXT}" ) using Analyze-String, but I'm having difficulty determining what I should do with code like the ital tags (signified by the "I" tags), which need to be kept in the result code.</p>
  
  <p>I tried using copy-of in the non-matching-substring, but that appears to not work. Of course, value-of gets me everything except the ital tags.</p>
  
  <p>I realize the variable ($stemString) is superfluous at this point. I was going along that path thinking I might be able to come up with something that would allow copy-of to process, but so far, no luck.</p>
  
  <p>Sample Code:</p>
  
  <pre><code>&lt;stem&gt;What is the value of &lt;I&gt;f&lt;/I&gt;(&lt;I&gt;x&lt;/I&gt;) when ${##A112800eqn01:3}&lt;/stem&gt;
  </code></pre>
  
  <p>My current XSLT:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  </code></pre>
  
  <p>
      </p>
  
  <pre><code>&lt;xsl:template match="node()|@*"&gt;
      &lt;xsl:copy&gt;
          &lt;xsl:apply-templates select="node()|@*"/&gt;
      &lt;/xsl:copy&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="assessmentItem"&gt;
  
  &lt;!--SNIP--&gt;
  
      &lt;xsl:apply-templates select="stemArea/stem"/&gt;
  
  &lt;!--SNIP--&gt;
  
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="stem"&gt;
  
      &lt;xsl:variable name="stemString"&gt;
          &lt;xsl:copy-of select="./* | ./text()"/&gt;
      &lt;/xsl:variable&gt;
  
      &lt;xsl:choose&gt;
  
          &lt;!--Tests for empty stems that aren't art callouts--&gt;
          &lt;xsl:when test=". = '' and @type!='art'"&gt;&lt;/xsl:when&gt;
          &lt;xsl:when test=". = ' ' and @type!='art'"&gt;&lt;/xsl:when&gt;
  
          &lt;!--Test for art callouts--&gt;
          &lt;xsl:when test="@type='art'"&gt;&lt;p&gt;&lt;img alt="{@loc}" height="10" id="{@loc}" label="" longdesc="normal" src="{@loc}" width="10"/&gt;&lt;/p&gt;&lt;/xsl:when&gt;
  
          &lt;!--Test for boxed text--&gt;
          &lt;xsl:when test="@style='box' or @style='boxL'"&gt;&lt;p&gt;&lt;span label="Tag_7"&gt;
              &lt;xsl:copy-of select="./* | ./text()"&gt;&lt;/xsl:copy-of&gt;
          &lt;/span&gt;&lt;/p&gt;&lt;/xsl:when&gt;
  
          &lt;xsl:otherwise&gt;&lt;p&gt;
  
              &lt;!--Are MathType tokens present in stem?--&gt;
              &lt;xsl:analyze-string regex="(\$\{{.+\}})" select="$stemString"&gt;
  
                  &lt;!--If MathType tokens are in stem, do the following--&gt;
                  &lt;xsl:matching-substring&gt;
  
                      &lt;xsl:analyze-string regex="(\$\{{)(##.+[eqn|art]\d+)([^a-zA-Z0-9]?.*\}})" select="."&gt;
                          &lt;xsl:matching-substring&gt;
                              &lt;img alt="{regex-group(2)}" height="10" id="{regex-group(2)}" label="" longdesc="normal" src="{regex-group(2)}" width="10"/&gt;
                          &lt;/xsl:matching-substring&gt;
                          &lt;xsl:non-matching-substring&gt;
                              &lt;xsl:text&gt;ERROR&lt;/xsl:text&gt;
                          &lt;/xsl:non-matching-substring&gt;
                      &lt;/xsl:analyze-string&gt;
  
                  &lt;/xsl:matching-substring&gt;
  
                  &lt;!--No MathType tokens in string--&gt;
                  &lt;xsl:non-matching-substring&gt;
                      &lt;xsl:value-of select="."/&gt;
                  &lt;/xsl:non-matching-substring&gt;
  
              &lt;/xsl:analyze-string&gt;
          &lt;/p&gt;&lt;/xsl:otherwise&gt;
  
      &lt;/xsl:choose&gt;
  
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p></p>
  
  <p>Desired Output:</p>
  
  <pre><code>&lt;p&gt;What is the value of &lt;I&gt;f&lt;/I&gt;(&lt;I&gt;x&lt;/I&gt;) when &lt;img alt="##A112800eqn01" height="10" id="##A112800eqn01" label="" longdesc="normal" src="##A112800eqn01" width="10"/&gt;&lt;/p&gt;
  </code></pre>
  
  <p>What I'm getting:</p>
  
  <pre><code>&lt;p&gt;What is the value of f(x) when &lt;img alt="##A112800eqn01" height="10" id="##A112800eqn01" label="" longdesc="normal" src="##A112800eqn01" width="10"/&gt;&lt;/p&gt;
  </code></pre>
  
  <p>Anyone have any ideas for how to proceed?  </p>
  
  <p>@Martin Honnen: Thank you for the response. Your code solves the error. </p>
  
  <p>However, I have an additional issue. When there is more than one MathType callout in a stem, it causes an error. I am sure that the cause is my regex not capturing everything properly, but I have hammered on this for a while to no avail. Below I will illustrate the issue I'm having.</p>
  
  <p>Sample Code:</p>
  
  <pre><code>&lt;stem type="text"&gt;What is the value of &lt;I&gt;f&lt;/I&gt;(&lt;I&gt;x&lt;/I&gt;) when ${##A112800eqn01:3}, and ${##A112800eqn02:3} is 3.&lt;/stem&gt;
  </code></pre>
  
  <p>Desired Output:</p>
  
  <pre><code>&lt;p&gt;What is the value of &lt;I&gt;f&lt;/I&gt;(&lt;I&gt;x&lt;/I&gt;) when &lt;img alt="##A112800eqn01" height="10" id="##A112800eqn01" label="" longdesc="normal" src="##A112800eqn01" width="10"/&gt;, and &lt;img alt="##A112800eqn02" height="10" id="##A112800eqn02" label="" longdesc="normal" src="##A112800eqn02" width="10"/&gt; is 3.&lt;/p&gt;
  </code></pre>
  
  <p>What I'm getting:</p>
  
  <pre><code>&lt;p&gt;What is the value of &lt;I&gt;f&lt;/I&gt;(&lt;I&gt;x&lt;/I&gt;) when &lt;img alt="##A112800eqn01:3}, and ${##A112800eqn02" height="10" id="##A112800eqn01:3}, and ${##A112800eqn02" label="" longdesc="normal" src="##A112800eqn01:3}, and ${##A112800eqn02" width="10"/&gt; is 3.&lt;/p&gt;
  </code></pre>