[xsl] Counting indent level

Subject: [xsl] Counting indent level
From: Nicholas Orr <nick@xxxxxxxxxxx>
Date: Mon, 29 Sep 2008 11:22:43 +1000
G'day all,

I've got some XML that contains a list of items and groups, and each group could contain either more items or more groups like so :

<catalog>
	<item />
	<group>
		< group >
		< item />
		< item />
		< group >
			< group >
				< item />
etc

So groups can contain either items or more groups or nothing. Items only exist at the root (catalog) of this part of the tree or inside groups. I'm outputting a single list of all of the items and group currently using this :

<xsl:for-each select="item|descendant::group|descendant::group/item">

The reason for that is that a item node might also contain child nodes that are also called <item> nodes that I don't want. I only want a item node that has it's immediate parent a group node. (There may be an easier way to do that...)

How do I count the "indent" level - so how many parent "groups" each item or group has, right back up to the catalog node? Eg in the example above, it would be 0 for the first item and group, 1 for the next 4 etc.

I can change the for-each that I'm using, although it has to remain in the same order, so I can't process them out of order.

Is the easiest way to count back up the tree for each one, or is there a way to build that into the for-each? If I use count, how do I count the path back up to the catalog node?

Thanks in advance,
Nick

Current Thread