XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
Sushant PrabhuSubject: retreiving ancestors
Author: Sushant Prabhu
Date: 16 Nov 2006 04:01 AM
Hi All

I have a scenario 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.



Do respond


Regards

Postnext
James DurningSubject: retreiving ancestors
Author: James Durning
Date: 16 Nov 2006 12:02 PM
would a sort work?
eg.
<xsl:sort select="0-position()"/>

Postnext
Sushant PrabhuSubject: retreiving ancestors
Author: Sushant Prabhu
Date: 16 Nov 2006 06:33 PM
Hi All

I tried with xsl:sort (ascending/descending) but did not help.
<xsl:for-each select="ancestor::*">
<xsl:sort select="." order="ascending" />

<xsl:value-of select="node_name"/>

</xsl:for-each>



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

Postnext
James DurningSubject: retreiving ancestors
Author: James Durning
Date: 17 Nov 2006 09:41 AM
Originally Posted: 17 Nov 2006 09:37 AM
The outer loop surrounding that is important.
How do you get to that point?
Eg, this is one possible solution, but probably does not use the same method you use.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:for-each select="//node[not node]">
<xsl:for-each select="ancestor-or-self::*">
<xsl:for-each select="ancestor::*">
<xsl:value-of select="node_name"/>
<xsl:text>/</xsl:text>
</xsl:for-each>
<xsl:value-of select="node_name"/><xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Postnext
Sushant PrabhuSubject: retreiving ancestors
Author: Sushant Prabhu
Date: 19 Nov 2006 07:53 PM
Hi James

I tried that solution but it did not help.

Find the code below which I am using

----------------------------------------------------
<xsl:template match="node">
<xsl:apply-templates select="node"/>
<xsl:for-each select="ancestor::*">
<xsl:value-of select="node_name"/>

<xsl:if test = "position() != '1'">
<xsl:text>/</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:template>
----------------------------------------------------

Do post your suggestions

Thanks & Regards


Postnext
James DurningSubject: retreiving ancestors
Author: James Durning
Date: 21 Nov 2006 10:34 AM
Move the apply-templates after the rest. You want the children's output after your own output.
<xsl:template match="node">
<xsl:for-each ....
</xsl:for-each>

<xsl:apply-templates select="node"/>
</xsl:template>

Posttop
Sushant PrabhuSubject: retreiving ancestors
Author: Sushant Prabhu
Date: 23 Nov 2006 07:30 PM
Hi James

Thanks a million

That worked gr8

Regards,

 
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.