This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: how to walk nodes
- To: xsl-list at lists dot mulberrytech dot com
- Subject: Re: [xsl] how to walk nodes
- From: Wendell Piez <wapiez at mulberrytech dot com>
- Date: Thu, 13 Sep 2001 15:28:00 -0400
- Reply-To: xsl-list at lists dot mulberrytech dot com
Dmitri,
The easiest way is to do it just the way your Subject line says. To iterate
over a set of nodes, use xsl:for-each. Although often abused, for-each is
perfect for this kind of thing.
So:
<xsl:template match="some_node">
<xsl:for-each select="ancestor::component">
<!-- Walking through ancestors named 'component'.
By default, for-each traverses the nodes in document order -->
<xsl:if select="position() > 1">
<!-- add a '/' delimiter in front of all steps but the first one -->
<xsl:text>/</xsl:text>
</xsl:if>
<xsl:value-of select="@name"/>
</xsl:for-each>
</xsl:template>
Without xsl:for-each, you'd have to have a special set of templates that
walked up (instead of down) the tree, and somehow reversed the order of the
values reported. It could be done with a single recursive template. But
this solution is easier.
Enjoy,
Wendell
At 11:32 AM 9/13/01, you wrote:
>I'd like to build a template that gives me "path" of parent nodes
>for example:
>
>XML
><components>
> <component name="comp1">
> <some_node1>
> <component name="comp2">
> </component>
> </some_node1>
> <some_node2>
> <some_node3>
> <component name="comp3">
> <some_node>
> </component>
> </some_node3>
> </some_node2>
> </component>
></components>
>
>XSL
><xsl:template match="some_node">
> hier walk through parent nodes
></xsl:template>
>
>output:
>
>"comp1/comp3"
======================================================================
Wendell Piez mailto:wapiez@mulberrytech.com
Mulberry Technologies, Inc. http://www.mulberrytech.com
17 West Jefferson Street Direct Phone: 301/315-9635
Suite 207 Phone: 301/315-9631
Rockville, MD 20850 Fax: 301/315-8285
----------------------------------------------------------------------
Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list