my dog learned polymorphism*
The moose likes XML and Related Technologies and the fly likes retrieving ancestors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "retrieving ancestors" Watch "retrieving ancestors" New topic
Author

retrieving ancestors

Mohit Sinha
Ranch Hand

Joined: Nov 29, 2004
Posts: 125
Hi All

I have a scneario where i am retrieving all the ancestors for a particular node.

Here is my code
----
<xsl:for-each select="ancestor::*">
<xsl:value-of select="node_name"/>
<xsl:text>/</xsl:text>
</xsl:for-each>
----

Here though it retrieves all but not in the order I want. I mean the output is correct but fetch is in the opposite direction.
I want something like this
A
A-B
A-B-C
A-B-C-D

but what i am getting in return is the opposite
A-B-C-D
A-B-C
A-B
A

How can I get all the top parent listing first & then subsequent nested ones.

Also I would like to know how to get the root top most parent node for a particular node


Do respond


Regards
Nicolas Stern
Ranch Hand

Joined: Apr 26, 2002
Posts: 57
Using a xsl:sort instruction will do it.

<xsl:for-each select="ancestor::*">
<xsl:sort select="." order="ascending" />
<ancestor>
<xsl:value-of select="name()"/>
</ancestor>
</xsl:for-each>

HTH
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18907
    
    8

Also I would like to know how to get the root top most parent node for a particular node
The standard way to get the last element of an XSLT node-set is to apply "[position() = last()]" to it. And the standard way to get the first element of a node-set is to apply "[1]" to it.
Mohit Sinha
Ranch Hand

Joined: Nov 29, 2004
Posts: 125
Hi All

I tried with xsl:sort (ascending/descending) but did not help.
I will give a glimpse of the xml which I have

*********************************

<node cid="1" oid="2">
<node_name>India</node_name>
<node cid="11" oid="4">
<node_name>Maharashtra</node_name>

<node cid="22" oid="8">
<node_name>Mumbai</node_name>

<node cid="33" oid="8">
<node_name>Dadar</node_name>
</node>
*********************************

Now what I want to print is something like this
India
India/Maharashtra
India/Maharashtra/Mumbai
India/Maharashtra/Mumbai/Dadar


But no matter what i tried its just giving me the output in the reverse order. Here is the code

<xsl:for-each select="ancestor::*">
<xsl:value-of select="node_name"/>
</xsl:for-each>
I applied the sort as suggested above but did not help.

Do let me know what I am missing here.

Regards
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: retrieving ancestors