This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: Result count in a for-each with an embedded if
- To: xsl at cheekymail dot net
- Subject: [xsl] Re: Result count in a for-each with an embedded if
- From: Dimitre Novatchev <dnovatchev at yahoo dot com>
- Date: Tue, 19 Jun 2001 10:23:25 -0700 (PDT)
- Cc: xsl-list at lists dot mulberrytech dot com
- Reply-To: xsl-list at lists dot mulberrytech dot com
Hi Tim,
This is quite straightforward:
xml source:
----------
<articles>
<article ID="article1">
<title>
A Title about frogs
</title>
<detail>
Some detail on frogs.
</detail>
</article>
<article ID="article2">
<title>
A Title about cats
</title>
<detail>
Some detail on cats.
</detail>
</article>
<article ID="article3">
<title>
A Title about cats and dogs
</title>
<detail>
Do dogs eat cats.
</detail>
</article>
</articles>
stylesheet:
----------
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:variable name="vLower" select="'abcdefghijklmnopqrstuvwxyz'"/>
<xsl:variable name="vUpper" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
<xsl:variable name="keyword" select="'Cat'"/>
<xsl:template match="articles">
<xsl:for-each select="article
[contains(translate(detail, $vLower, $vUpper),
translate($keyword, $vLower, $vUpper)
)
]">
<xsl:text>
</xsl:text><xsl:value-of select="normalize-space(title)" />
<xsl:if test="position() = last()">
<xsl:text>
</xsl:text>There were <xsl:value-of select="position()"/>
articles found.
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Result:
------
A Title about cats
A Title about cats and dogs
There were 2 articles found.
Cheers,
Dimitre Novatchev.
Tim wrote:
Hi,
Tring to do a count of the results of the following snip:
<xsl:template match="articles">
<xsl:for-each select="article">
<xsl:if test="contains(translate
(detail,'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ'),trans
late
($keyword,'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ'))">
<xsl:value-of select="normalize-space(title)"/>
</xsl:if>
</xsl:for-each>
There were XX articles found.
</xsl:template>
Need to return the XX value.
I can't find a way of putting the contains test into the for-each
select so I could use the position()(?).
Ideas would be appreciated.
__________________________________________________
Do You Yahoo!?
Spot the hottest trends in music, movies, and more.
http://buzz.yahoo.com/
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list