[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: counting in for each
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@xxxxxxxxxxx> 12/09/2005 02:12 PM Please respond to xsl-list@xxxxxxxxxxxxxxxxxxxxxx To <xsl-list@xxxxxxxxxxxxxxxxxxxxxx> cc Subject 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
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|