Re: [xsl] Counting indent level

Subject: Re: [xsl] Counting indent level
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Sun, 28 Sep 2008 21:38:51 -0400
At 2008-09-29 11:22 +1000, Nicholas Orr wrote:
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.

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.

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


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.


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?

I hope the above helps.


. . . . . . . . . . Ken

--
Upcoming XSLT/XSL-FO hands-on courses:      Wellington, NZ 2009-01
Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video
G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal

Current Thread