• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

retrieving ancestors

 
Mohit Sinha
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21116
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic