Hi, Dietmar,
Given an XML file such as:
<widgetpile>
<widgets>
<widget/>
<widget/>
<widget/>
</widgets>
<widgets>
<widget/>
</widgets>
</widgetpile>
You can write XSLT thus:
<xsl:template match="widgetpile">
<xsl:for-each select="widgets">
Number of Widgets in this group: <xsl:value-of
select="count(widget)"/>
</xsl:for-each>
</xsl:template>
When you are processing each widget, you can determine which one you are
processing with the position() function, thus:
<xsl:template match="widget">
Now processing widget number <xsl:value-of select="position()"/>
</xsl:template>
So, you might link it all together in a single stylesheet thus:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:template match="widgetpile">
<xsl:for-each select="widgets">
Number of Widgets in this group: <xsl:value-of
select="count(widget)"/>
</xsl:for-each>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="widgets">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="widget">
Now processing widget number <xsl:value-of select="position()"/>
</xsl:template>
</xsl:stylesheet>
Note that the strip-space instruction is present to prevent position()
from picking up the carriage returns after the <widget> elements in the
XML file. Otherwise, this transform claims to be processing widgets 2, 4,
and 6 rather than 1, 2, and 3.
HTH
Jay Bryant
Bryant Communication Services
(presently consulting at Synergistic Solution Technologies)
"Dietmar Klotz" <dklotz(_at_)cellular(_dot_)de>
12/09/2005 02:12 PM
Please respond to
xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
To
<xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com>
cc
Subject
[xsl] counting in for each
I am making a list with "for each" and if the list is 1 or 3 items it
should
change the state of display. And I would also like to change the Icon
depending on how many objects there are....so object 1 has icon 1...object
2
icon 2
So my question. Is it possible to count how many Items there are in a
node?
And do I know where I am in a for each?
Thanks to all!
Dietmar Klotz
--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--
--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--