title: XSLT 1.0: restrict entries in a nodeset
body: <p>Being relatively new to XSLT I have what I hope is a simple question. I have some flat XML files, which can be pretty big (eg. 7MB) that I need to make 'more hierarchical'. For example, the flat XML might look like this:</p>
  
  <pre><code>&lt;D0011&gt;
      &lt;b/&gt;
      &lt;c/&gt;
      &lt;d/&gt;
      &lt;e/&gt;
      &lt;b/&gt;
      ....
      ....
  &lt;/D0011&gt;
  </code></pre>
  
  <p>and it should end up looking like this:</p>
  
  <pre><code>&lt;D0011&gt;
    &lt;b&gt;
      &lt;c/&gt;
      &lt;d/&gt;
      &lt;e/&gt;
    &lt;/b&gt;
    &lt;b&gt;
   ....
   ....
  &lt;/D0011&gt;
  </code></pre>
  
  <p>I have a working XSLT for this, and it essentially gets a nodeset of all the b elements and then uses the 'following-sibling' axis to get a nodeset of the nodes following the current b node (ie. following-sibling::*[position()=$nodePos]). Then recursion is used to add the siblings into the result tree until another b element is found (I have parameterised it of course, to make it more generic).</p>
  
  <p>I also have a solution that just sends the position in the XML of the next b node and selects the nodes after that one after the other (using recursion) via a *[position() = $nodePos] selection.</p>
  
  <p>The problem is that the time to execute the transformation increases unacceptably with the size of the XML file. Looking into it with XML Spy it seems that it is the 'following-sibling' and 'position()=' that take the time in the two respective methods.</p>
  
  <p>What I really need is a way of restricting the number of nodes in the above selections, so fewer comparisons are performed: every time the position is tested, every node in the nodeset is tested to see if its position is the right one. Is there a way to do that ? Any other suggestions ?</p>
  
  <p>Thanks,</p>
  
  <p>Mike</p>