[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Iterating over all the Ancestors

Subject: Re: Iterating over all the Ancestors
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Tue, 26 Nov 2002 14:04:31 -0500
xslt ancestor child
Bix (and everyone),

Reading my own post, I realized I might have been confusing. The for-each walks all the elements up to the root, but it doesn't "walk up the tree". Rather, it selects these elements into a node-set (which, as David C. will remind us, has no implicit order) and walks them. By default, in document order, so even if the axis goes "up" the tree, the iteration goes "down".

I wouldn't bother to mention it except that (especially given that ancestor and ancestor-or-self are among the "reverse" axes) it's sometimes a source of gotchas.

Cheers,
Wendell

At 01:47 PM 11/26/2002, I wrote:
Bix,

Walking up the tree is easy:

<xsl:for-each select="ancestor::*">

walks all the elements up to the root (but not including it: it's not an element). You can include the context node itself by using the ancestor-or-self:: axix.

So in your case

<xsl:template match="data">
<xsl:for-each select="ancestor-or-self::*">
<xsl:if test="ancestor::*">
<!-- if the ancestor element has an ancestor element, it's not
first: prepend it with some formatting -->
<xsl:text> -> </xsl:text>
</xsl:if>
<xsl:value-of select="text"/>
<!-- reports the value of the <text> element child, which is our label -->
</xsl:for-each>
</xsl:template>


By default, a for-each traverses in document order, so this will go top-down. (There are ways to tweak it if you want something different.)

I hope this helps,
Wendell

At 01:19 PM 11/26/2002, you wrote:
I have been trying to figure out a way of iterating over the parent nodes of a particular child all the way to the root of the tree.
Essentially, I'd like to display the path which I have taken to get to the current child, but I would like to do it in such a way that at a future point in time, I can modify my style sheet to include hyperlinks at each parent node. An example xml file would be:


<data><text>Title for web page</text>
   <data><text>Motivation</text>
       <data><text>White Pages</text></data>
       <data><text>References</text></data>
   </data>
   <data><text>Requirements</text>
       <data><text>Software</text></data>
       <data><text>Hardware</text></data>
   </data>
   <data><text>Design</text>
       <data><text>Interfaces</text>
           <data><text>Hardware-Software</text></data>
           <data><text>Legacy Software</text></data>
       </data>
       <data><text>Functions</text></data>
   </data>
   <data><text>Implementation</text></data>
   <data><text>Verification</text></data>
   <data><text>Documentation</text></data>
   <data><text>Archive</text></data>
</data>

Suppose that we are at the following X-Path:
   /<root data>/<Design data>/<Interfaces data>/<Hardware-Software data>

I would like to develop a template that essentially displays the path above in the following way:

Title for web page -> Design -> Interfaces -> Hardware-Software

At each point, if I added a <href> node within the xml tree as a child of the <data> element, then I would like to be able to hyperlink each individual node...

Thanks!
Bix


======================================================================
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


======================================================================
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.