Re: [xsl] Problems with for-each

Subject: Re: [xsl] Problems with for-each
From: David Carlisle <davidc@xxxxxxxxx>
Date: Fri, 13 Jul 2001 10:13:21 +0100
<xsl:variable name="id">
  <xsl:value-of select="identifier" />
</xsl:variable>
<xsl:for-each select="//itemList/headerList/header">
  <xsl:if test="name = 'title'">
    <xsl:apply-templates select="//itemList/item/title[../identifier = $id]" 
/>
  </xsl:if>


The answer to your question was on this list yesterday in a different
thread, use a variable to save the node you want to get back to,
befofore going into the loop, but also note that the above looks very
inefficient.

firstly by starting with // you cause the entire document to be searched
its entire depth. If you know that your headers are anywhere more
specific starting with anything other than // is always a big win,
secondly you are selecting all these nodes and then only doing something 
if they have a child node, and then selcting something else.
if there is more than one item selected in the outer loop then every
iteration will produce the same output as the inner select also starts
with // so does not depend on the current node in the outer loop.
In particular in the outer loop you select only itemLists with 
headerList/header/name=title' but in the inner loop you ignore that and
select all lists wether or not they have a name 'title'.

It looks like you don't want any for-each at all, but simply want to
select those <title> which have an <identifier> sibling with string
value equal to the current node's <identifier> child and a <name> equal
to 'title'.

So it seems (untested) all the above lines could more simply be written

<xsl:apply-templates 
 select="//itemList[headerList/header/name='title']
          /item[identifier=current()/identifier]
          /title"
/>


and preferably changing the // to something more specific, depending on
your source structure.          

David

_____________________________________________________________________
This message has been checked for all known viruses by Star Internet
delivered through the MessageLabs Virus Scanning Service. For further
information visit http://www.star.net.uk/stats.asp or alternatively call
Star Internet for details on the Virus Scanning Service.

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread