title: Insert XSLT node-set B in another node-set A at detected position inside text node of node-set A
body: <p>I'm using XSLT to transform complex XML output of a content management system into XHTML. I'm using <code>&lt;xsl:apply-templates/&gt;</code> to get an XHTML fragment of whatever is described by XML input. That XML input comes with a very complex structure that may describe lots of different cases to be handled by several XSLT template elements. And that structure may change quite often in the future.</p>
  
  <p>Previously, the resulting fragment of that transformation was directly sent to XSLT output. Now the requirements have changed and I need to capture result, occasionally modify it to insert some other well-formed XHTML fragment at a certain position in value of the fragment.</p>
  
  <p>For the sake of demonstration, consider <code>&lt;xsl:apply-templates/&gt;</code> having created some opaque XHTML fragment captured in variable <strong>container</strong>.</p>
  
  <pre><code>&lt;xsl:variable name="container"&gt;
    &lt;xsl:apply-templates /&gt;
  &lt;/xsl:variable&gt;
  </code></pre>
  
  <p>Next there is a second XHTML fragment in variable <strong>snippet</strong>:</p>
  
  <pre><code>&lt;xsl:variable name="snippet"&gt;
    &lt;xsl:call-template name="get-snippet" /&gt;
  &lt;/xsl:variable&gt;
  </code></pre>
  
  <p>Requirement says to have node-set in <strong>$snippet</strong> to be inserted before any optionally contained period at end of value of <strong>$container</strong>. This ain't problematic unless XHTML fragments in both variables have to be kept as fragments. Thus one can't operate on string values of either variable.</p>
  
  <p>Is there any opportunity to achieve that requirement in XSLT without losing the power and flexibility of <code>&lt;xsl:apply-templates/&gt;</code> on retrieving XHTML fragment in <strong>$container</strong>?</p>
  
  <p>BTW: I already know about accessing the last text node in <code>$container</code> using:</p>
  
  <pre><code>node-set($container)//child::text[last()] 
  </code></pre>
  
  <p>But I missed to get something inserted in the mid of that text node and I consider XSLT failing to provide proper support for what I want to do.</p>