Sorting a nodeset before passing to xsl:for-each

Posted by Zack Mulgrew on Stack Overflow See other posts from Stack Overflow or by Zack Mulgrew
Published on 2010-04-07T20:30:14Z Indexed on 2010/04/07 20:33 UTC
Read the original article Hit count: 388

Filed under:
|
|

I have a situation where loop through a sorted nodeset and apply a template on each of the nodes:

<div id="contractscontainer">
  <xsl:for-each select="document">
    <xsl:sort select="content[@name='ClientName']/text()" />
    <xsl:apply-templates select="." mode="client-contract" />
  </xsl:for-each>
</div>

I want to do something special with the "first" 5 nodes in the node set and render them nested element. The problem is that they need to be in the same order as if they were sorted (as they are in the loop).

I had planned on doing this by using two xsl:for-each elements, each with the correct nodes selected from the set. I can't do this, however, because they need to be sorted before I can select the "first" 5.

Example:

<div id="contractscontainer">
  <div class="first-five">
    <xsl:for-each select="document[position() < 6]">
      <xsl:sort select="content[@name='ClientName']/text()" />
      <xsl:apply-templates select="." mode="client-contract" />
    </xsl:for-each>
  </div>
  <div class="rest-of-them">
    <xsl:for-each select="document[position() > 5]">
      <xsl:sort select="content[@name='ClientName']/text()" />
      <xsl:apply-templates select="." mode="client-contract" />
    </xsl:for-each>
  </div>
</div>

I don't think this will work because I'm selecting the nodes by position before sorting them, but I can't use xsl:sort outside of the xsl:for-each.

Am I approaching this incorrectly?

© Stack Overflow or respective owner

Related posts about xslt

Related posts about xsl