This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: for-each -- strange multiplication effects
- To: mario dot jeckle at daimlerchrysler dot com
- Subject: Re: [xsl] for-each -- strange multiplication effects
- From: Jeni Tennison <mail at jenitennison dot com>
- Date: Tue, 16 Jan 2001 13:57:43 +0000
- CC: xsl-list at lists dot mulberrytech dot com, elke dot e dot meier at daimlerchrysler dot com
- Organization: Jeni Tennison Consulting Ltd
- References: <0057440003355118000002L482*@MHS>
- Reply-To: xsl-list at lists dot mulberrytech dot com
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