Re: [xsl] how to walk nodes

Subject: Re: [xsl] how to walk nodes
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Thu, 13 Sep 2001 15:28:00 -0400
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() &gt; 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@xxxxxxxxxxxxxxxx
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



Current Thread