title: generate-id() too slow for large document
body: <p>I have a large xml document containing annotated speech transcripts. Following is a short fragment.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;U&gt;
      &lt;A/&gt;
      &lt;C type="start" id="cb01s"/&gt;
      &lt;P/&gt;
      &lt;T&gt;a&lt;/T&gt;
      &lt;T&gt;woman&lt;/T&gt;
      &lt;P/&gt;
      &lt;T&gt;took&lt;/T&gt;
      &lt;T&gt;off&lt;/T&gt;
      &lt;T&gt;the&lt;/T&gt;
      &lt;T&gt;train&lt;/T&gt;
      &lt;C type="end" id="cb02e"/&gt;
      &lt;P/&gt;
      &lt;T&gt;but&lt;/T&gt;
      &lt;P/&gt;
      &lt;F/&gt;
      &lt;RT&gt;
          &lt;O&gt;
              &lt;C type="start" id="cb03s"/&gt;
              &lt;T&gt;her&lt;/T&gt;
              &lt;T&gt;bag&lt;/T&gt;
              &lt;P/&gt;
              &lt;T&gt;are&lt;/T&gt;
          &lt;/O&gt;
          &lt;P/&gt;
          &lt;E&gt;
              &lt;C type="start" id="cb04s"/&gt;
              &lt;T&gt;her&lt;/T&gt;
              &lt;T&gt;bag&lt;/T&gt;
              &lt;T&gt;are&lt;/T&gt;
          &lt;/E&gt;
      &lt;/RT&gt;
      &lt;P/&gt;
      &lt;T&gt;still&lt;/T&gt;
      &lt;P/&gt;
      &lt;T&gt;in&lt;/T&gt;
      &lt;T&gt;the&lt;/T&gt;
      &lt;T&gt;train&lt;/T&gt;
      &lt;C type="end" id="cb05e"/&gt;
      &lt;PC&gt;.&lt;/PC&gt;
  &lt;/U&gt;
  </code></pre>
  
  <p>The basic task I need to do is to get the number of <code>&lt;T&gt;</code> nodes between certain pairs of <code>&lt;C&gt;</code> nodes. I've used the following stylesheet fragment to do this (illustrating with one specific pair of <code>&lt;C&gt;</code> nodes).</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
      &lt;xsl:output method="text" encoding="UTF-8"/&gt;
  
      &lt;xsl:template match="U"&gt;
          &lt;xsl:variable name="start-node" select="descendant::C[@id = 'cb01s']"/&gt;
          &lt;xsl:variable name="end-node" select="descendant::C[@id = 'cb02e']"/&gt;
          &lt;xsl:text&gt;Result: &lt;/xsl:text&gt;
          &lt;xsl:value-of select="count($start-node/following::T[following::C[generate-id(.) = generate-id($end-node)]])"/&gt;
      &lt;/xsl:template&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>This works fine on such a short XML fragment as above and gives the correct result: <code>Result: 6</code>.</p>
  
  <p>However, the actual XML document contains tens of thousands of <code>&lt;C&gt;</code> nodes and even more <code>&lt;T&gt;</code> nodes. So when I try to run the stylesheet on it the result comes back <em>very</em> slowly. (It would probably take days to finish completely.) I suppose the problem must be that on each run of the <code>&lt;xsl:value-of...</code> line, the processor (Saxon) is checking all <code>&lt;T&gt;</code> nodes and generating id's for <code>&lt;C&gt;</code> nodes multiples times (i.e., exponentially) and that slows everything down.</p>
  
  <p>Is there a way to speed up the process while still using generate-id()? Or do I need to get the number of <code>&lt;T&gt;</code> nodes with some alternate approach?</p>