Friday, May 01, 2009

How To Count Elements In XSLt

In looking to count the number of instances of an XML element I was looking about for a piece of XSLT. Every example I came across had the xsl 'number' element within the 'for-each' element, which meant that the XSLT had to parse the element I was counting first. I didn't want to have a 'for-each' involved at all, but eventually I was in luck, and it's much easier that using a 'number' element:

<xsl:value-of select="count(elementname)" />

I was directed to this solution by experts-exchange: http://www.experts-exchange.com/Web/Web_Languages/XML/Q_21294193.html

However, if you do want to perform counting operations, this is an excellent starting point: http://www.w3schools.com/Xsl/el_number.asp

And for more general XSLt references:
I highly recommend D. Pawson's site as it is basically a massive list of examples and explanations for just about everything anyone would ever want to do in XSLt.

Also, of course, the http://www.developer.com/xml/article.php/3383961 article will tell you everything you need to know about traversing XML - especially useful if you are writing code which needs to build, navigate or parse XML.

No comments:

Post a Comment