Re: [xsl] Optimizing XSLT iteration

Subject: Re: [xsl] Optimizing XSLT iteration
From: Deborah Pickett <debbiep-list-xsl@xxxxxxxxxx>
Date: Mon, 08 Oct 2007 08:07:31 +1000
Sujata Gohad wrote:
>                             <xsl:for-each select="locus[position()=1]">
>                                 <xsl:text >M </xsl:text >
>                                 <xsl:value-of select="@ellipse_x"/ >
>                                 <xsl:text > </xsl:text >
>                                 <xsl:value-of select="@ellipse_y"/ >
>                             </xsl:for-each >
>                             <xsl:for-each select="locus[position()!=1]">
>                                 <xsl:text > L</xsl:text >
>                                 <xsl:value-of select="@ellipse_x"/ >
>                                 <xsl:text > </xsl:text >
>                                 <xsl:value-of select="@ellipse_y"/ >
>                             </xsl:for-each >

> Is there a way to faster iteration of the "locus" elements?

See if this is any quicker:

<xsl:for-each select="locus">
  <xsl:choose>
    <xsl:when test="position() = 1">
      <xsl:text>M </xsl:text>
    </xsl:when>
    <xsl:otherwise>
      <xsl:text> L </xsl:text>
    </xsl:otherwise>
  </xsl:choose>
  <xsl:value-of select="@ellipse_x"/ >
  <xsl:text > </xsl:text >
  <xsl:value-of select="@ellipse_y"/ >
</xsl:for-each>

Depending on your XSLT processor, your original code may be constructing
up to four node-sets: locus, the subset when position() = 1, locus
(again), the subset when position() != 1.

You're allowed to use position() in almost any expression, not just as
the predicate of a prior selection.  In my code, position() applies to
the position that each locus has in the most recent for-each, which is
what you want.

Current Thread