Results 1 to 2 of 2
Thread: XSL Element counting
Jul 11, 2004, 11:07 #1
- Join Date
- Jul 2001
- 0 Post(s)
- 0 Thread(s)
XSL Element counting
Does anybody know how to achive something like an iterator in XSL. For example:
i have an XML document which has inside n-elements - equations. I have previously generated images that contain theese equations and have the name eqN where N is their number of appearance. The first equation has the name eq1.gif, the next one eq2.gif and so on. What I want to do now, using XSL is to repeat this count. I know that during execution of XSL you cannot modify the values of variables so doing something like $count++ is not a good idea :-). I think this could be done by counting all predecessors but I do not know how I can do this.
Jul 11, 2004, 12:04 #2
Are you looping through nodes to generate these images? If so, you can use the XPath position() function to find the "index" of the element in the node set. Here's a quick example that may apply to what you're doing.
<?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet href="pos.xsl" type="text/xsl"?> <root> <eq>1+1=2</eq> <eq>1+2=3</eq> <eq>1+3=4</eq> <eq>1+4=5</eq> <eq>1+5=6</eq> </root>
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="html" version="4.01" encoding="utf-8" /> <xsl:template match="/"> <html> <head> <title>position test</title> </head> <body> <xsl:for-each select="/root/eq"> <div> <!--output the equation--> <xsl:value-of select="." /> <!--output an image based on position of element in node set--> <img> <xsl:attribute name="src"> eq<xsl:value-of select="position()" />.gif </xsl:attribute> </img> </div> </xsl:for-each> </body> </html> </xsl:template> </xsl:stylesheet>