This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: for-each -- strange multiplication effects


Hi Mario,

> When the predicate is true for one element that matches the node set
> (say "//x") specified by the select attribute of for-each it it
> repeated count(//x) times even when the predicate is false.
>
> <xsl:template match="test">
>         <xsl:for-each select="//test[@x]">
>                 this should only happen once!
>                 <xsl:value-of select="@x"/>
>         </xsl:for-each>
> </xsl:template>

In your template, you're matching 'test' elements.  When templates are
applied to the 'doc' element, then the built-in template is used, and
templates are applied to *all* the 'test' elements (4 of them).  Each
time the processor comes across one of these test elements (which it
does 4 times), it goes through all the 'test' elements with a 'x'
attribute (1 of them) and outputs the content.

To get the effect that you want, try the following:

<xsl:template match="doc">
   <xsl:for-each select="//test[@x]">
      this should only happen once!
      <xsl:value-of select="@x"/>
   </xsl:for-each>
</xsl:template>

This template will only be applied once, because there's only one
'doc' element in the source XML.

Alternatively, use:

<xsl:template match="test[@x]">
   this should only happen once!
   <xsl:value-of select="@x"/>
</xsl:template>

This template will only be applied once, because there's only one
'test' element with an 'x' attribute.

I hope that helps,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/



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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]