Subject: Re: [xsl] Counting indent level From: Nicholas Orr <nick@xxxxxxxxxxx> Date: Mon, 29 Sep 2008 12:51:14 +1000 |
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">
I expect you would have a more easily maintained stylesheet if you had:
<xsl:template match="catalog"> <xsl:apply-templates select="*"/> </xsl:template> <xsl:template match="group"> <!--report on the group--> <xsl:apply-templates select="item"/> </xsl:template> <xsl:template match="item"> <!--report on the item--> </xsl:template>
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.
Every node (except the root node above the document element) has only a single parent node, so I'm not sure what you mean by "immediate parent". Unless you meant to say "immediate ancestor" which is the parent.
Above you also state you want an item whose parent is catalogue and not just those whose parent is group. If I'm wrong, then you'll have to change the above to the following to not process items that are not a child of a group:
<xsl:template match="catalog"> <xsl:apply-templates select="*"/> </xsl:template>
(There may be an easier way to do that...)
See above ... only items that are immediate children of the catalogue or a group are processed.
I am assuming that a group is never a descendant of an item as the code above will not process the descendants of an item, whereas your posited for-each statement would.
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.
The code I give you above will process groups and items in document order.
item group group item item etc
<xsl:value-of select="count(ancestor::group)"/>
Cheers, Nick
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] Counting indent level, G. Ken Holman | Thread | Re: [xsl] Counting indent level, Jonas Mellin |
Re: [xsl] Counting indent level, G. Ken Holman | Date | Re: [xsl] Counting indent level, Jonas Mellin |
Month |