title: How to Create an Element from Two Surrounding Elements?
body: <p>I am stuck with an XML to XML transformation using XSLT 2.0 where I need to transform this:</p>
  
  <pre><code>&lt;p&gt;some mixed content &lt;x h=""&gt;START:attr="value"&lt;/x&gt; more mixed content &lt;x h=""&gt;END&lt;/x&gt; other mixed content&lt;/p&gt;
  </code></pre>
  
  <p>To this:</p>
  
  <pre><code>&lt;p&gt;some mixed content &lt;ph attr="value"&gt; more mixed content &lt;/ph&gt; other mixed content&lt;/p&gt;
  </code></pre>
  
  <p>So basically I'd like to replace <code>&lt;x h=""&gt;START:attr="value"&lt;/x&gt;</code> with <code>&lt;ph attr="value"&gt;</code></p>
  
  <p>and <code>&lt;x h=""&gt;END&lt;/x&gt;</code> with <code>&lt;/ph&gt;</code> and process the rest as usual.</p>
  
  <p>Does anyone know if that's possible? </p>
  
  <p>My main issue is that I cannot figure out how to find the  element with value END and then tell the XSLT processor (I use saxon) to process the content between the first occurence of  and the second occurence of  and finally write the end element . I am familiar with how to create an element (including attributes).</p>
  
  <p>I have a specific template to match the start element START:attr="value". Since the XML document I process contains many other elements I'd prefer a recursive solution, so continue the processing of the found content between START and END by using other existing templates.</p>
  
  <p><strong>Sample XML</strong> 
  (note that I don't know in advance if the parent will be a p element)</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;p&gt; my sample text &lt;b&gt;mixed&lt;/b&gt; more
    &lt;x h=""&gt;START:attr="value"&lt;/x&gt;
    This is mixed content &lt;i&gt;REALLY&lt;/i&gt;, process it normally
    &lt;x h=""&gt;END&lt;/x&gt;
  &lt;/p&gt;
  </code></pre>
  
  <p><strong>My Stylesheet</strong></p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="2.0"&gt;
  
  &lt;xsl:output method="xml" indent="yes"/&gt;
  
  &lt;xsl:template match="x[@h][starts-with(., 'START:')]"&gt;
      &lt;ph&gt;
  
         &lt;xsl:for-each-group select="../*" group-starting-with="x[@h][. = 'START:']"&gt;
              &lt;xsl:for-each-group select="current-group()" group-ending-with="x[@h][. = 'END']"&gt;
  
                 &lt;xsl:apply-templates select="@*|node()|text()"/&gt;
  
              &lt;/xsl:for-each-group&gt;
         &lt;/xsl:for-each-group&gt;    
      &lt;/ph&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="x[@h][starts-with(., 'END')]"/&gt;
  
  &lt;xsl:template match="node()|@*"&gt;
      &lt;xsl:copy copy-namespaces="no"&gt;
          &lt;xsl:apply-templates select="node()|@*" /&gt; 
      &lt;/xsl:copy&gt;
  &lt;/xsl:template&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p><strong>Result</strong></p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;p&gt; my sample text &lt;b&gt;mixed&lt;/b&gt; more
    &lt;ph&gt;mixed&lt;/ph&gt;
    This is mixed content &lt;i&gt;REALLY&lt;/i&gt;, process it normally
  
  &lt;/p&gt;
  </code></pre>
  
  <p>I cannot figure out how to put the complete content between START and END within the  tags. Any ideas?</p>