I have an xml like this one:
<?xml version="1.0" encoding="WINDOWS-1252" ?> <?xml-stylesheet type="text/xsl" href="test.xsl"?> <root> <something> <cd>a1</cd> <cd>a2</cd> </something> <another> <cd>b1</cd> <cd>b2</cd> <cd>b3</cd> <cd>b1</cd> <cd>b2</cd> <cd>b3</cd> <cd>b1</cd> <cd>b2</cd> </another> </root> I'm selecting all the cd nodes and trying to take the cds from 10 to 10 (1st, 11th, 21th...) this way:
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html><body> <xsl:apply-templates select="//cd" /> </body></html> </xsl:template> <xsl:template match="cd"/> <xsl:template match="cd[position() mod 10 = 1]"> <div> pos:<xsl:value-of select="position()"/>; value:<xsl:value-of select="."/> </div> </xsl:template> </xsl:stylesheet> The output:
pos:1; value:a1 pos:3; value:b3 And the expected output:
pos:1; value:a1 pos:11; value:b11 The problem is that position() in the match is the node number based on his parent. So the position() of "b1" is 1 and not 3. Is there a function to know the index of the current node?
Thanks in advance.
//EDIT to extend the example
cds?<xsl:variable name="all-cds" select="//cd" />and then loop through them<xsl:for-each select="$all-cds">and you will get their position independent of their parents.