[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Alternative for breaking out of for-each loop in XSLT


Subject: Re: [xsl] Alternative for breaking out of for-each loop in XSLT
From: David Carlisle <davidc@xxxxxxxxx>
Date: Fri, 15 Sep 2006 11:40:50 +0100

> I can't stop the iteration because XSLT doesn't
> support breaking out of for each loop.

It couldn't support "breaking out of a loop" as for-each doesn't imply
looping in that sense. It specifies which nodes should be processed, but
they can be processed in any order, or in parallel.

Once again you haven't really said what you are trying to do, although
the code you posted has many loops that clearly are not needed, for
example
          <xsl:for-each select="parameter">
                <xsl:if test="@name='value'">
                    <xsl:value-of select="@value"/>
                </xsl:if>
            </xsl:for-each>
why iterate over all the parameters if you just want the value
parameter? 
 <xsl:value-of select="parameter[@name='value']"/>


I think that what you want is:

given 'code' return the "value" 

so that is just 

<xsl:key name="StatusDesc" match="header" use="parameter[@name='code']/@value"/>

<xsl:template name="StatusDesc">
    <xsl:param name="statCode"/>
    <xsl:value-of select="$statCode"/>
    <xsl:for-each select="document('REC_STATUS.xml')">
      <xsl:value-of select="key('StatusDesc',$statCode)/parameter[@name='value']"/>
    </xsl:for-each>
</xsl:template>

as you see here you don't need any loops at all, the for-each is only
iterating over a single / node so isn't really a loop, it is just the
way of telling key() which document to look in. In XSLT2 key has a new
third argument to specify the document so you could simplify to

<xsl:template name="StatusDesc">
    <xsl:param name="statCode"/>
    <xsl:value-of select="$statCode,key('StatusDesc',$statCode,document('REC_STATUS.xml'))/parameter[@name='value']"/>
</xsl:template>

David


Current Thread
Keywords