title: Using Template Within XSLT Key Generation
body: <p>I have an XML document containing items, each having a tokenized ID string.</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;Test&gt;
  &lt;Items&gt;
      &lt;Item&gt;
          &lt;ID&gt;1_A_3&lt;/ID&gt;
          &lt;Name&gt;foo&lt;/Name&gt;
      &lt;/Item&gt;
      &lt;Item&gt;
          &lt;ID&gt;1_B_5&lt;/ID&gt;
          &lt;Name&gt;bar&lt;/Name&gt;
      &lt;/Item&gt;
      &lt;Item&gt;
          &lt;ID&gt;1_B_7&lt;/ID&gt;
          &lt;Name&gt;baz&lt;/Name&gt;
      &lt;/Item&gt;
  &lt;/Items&gt;
  &lt;/Test&gt;
  </code></pre>
  
  <p>I need to transform this into another XML document that groups the items according to the <em>middle</em> part of their ID string (the letter in the above example).</p>
  
  <pre><code>&lt;?xml version='1.0' ?&gt;
  &lt;GroupedItems&gt;
      &lt;Group id="A"&gt;
          &lt;Item&gt;foo&lt;/Item&gt;
      &lt;/Group&gt;
      &lt;Group id="B"&gt;
          &lt;Item&gt;bar&lt;/Item&gt;
          &lt;Item&gt;baz&lt;/Item&gt;
      &lt;/Group&gt;
  &lt;/GroupedItems&gt;
  </code></pre>
  
  <p>I am finding the groups using the keys functionality:</p>
  
  <pre><code>&lt;xsl:key name="uniqueGroupIDs" 
     match="Test/Items/Item" 
     use="substring-before(substring-after(ID,'_'),'_')"/&gt;
  
  &lt;xsl:for-each 
     select="Test/Items/Item[generate-id() = 
      generate-id(key('uniqueGroupIDs',
      substring-before(substring-after(ID,'_'),'_')))]"&gt;
  </code></pre>
  
  <p>Notice the code duplication of the substring calls in both places.   I already have a template that does the same thing:</p>
  
  <pre><code>&lt;xsl:template name="ExtractGroupID"&gt;
      &lt;xsl:param name="idString"/&gt;        
  &lt;xsl:value-of 
         select="substring-before(substring-after($idString, '_'),'_')"/&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p>Is there any way to use that template within the key statements, to avoid the duplication of code?</p>
  
  <p>In XSLT 2.0 I would just define a function to do it, but I'm stuck with XSLT 1.0 due to a tools limitation beyond my control.</p>