Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 2nd, 2007, 02:25 PM
Registered User
 
Join Date: Feb 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to sdaspen
Default Search A Hierarchy Tree for Node where id=value

I have the following XML:

- <channel>
  <title>Dishwashers</title>
  <requireauth>0</requireauth>
  <id>977</id>
     - <channel>
       <title>TestChannel 01</title>
       <id>980</id>
       - <channel>
           <title>subchannel b</title>
           <id>984</id>
               - <channel>
                   <title>subchannel b</title>
                   <id>999</id>
                   <parentchannel>984</parentchannel>
                </channel>
           <parentchannel>980</parentchannel>
       </channel>
  <parentchannel>975</parentchannel>
</channel>

What I want to do via XSLT is I have a variable <xsl:variable name="ch_selected"/>. I want to traverse the entire tree looking for the parentchannelid of $ch_selected. Then find out what channel it belongs to an so on to the top of the tree.

So channel id = 999
find and set a variable for it's parentchannelid which is 984
Find and set a variable for 984's parentchannelid which is 980
Find and set a variable for 980's parentchannel which is 975

The channel nodes will only ever be 3 levels deep.
Channel id 975 is the top or highest root channnel.

And all the parentchannelid's found and identified need to be available to the entire xslt page.

I've tried this with nested for-each loops but it looks like my variables then are only local to the for-each or if loops.

Thanks,

Shawn

 
Old February 2nd, 2007, 02:28 PM
Registered User
 
Join Date: Feb 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to sdaspen
Default

Here is the XSLT I was writing when I realized my variable was not globally available:

<xsl:variable name="ch_selected"><xsl:value-of select="/root/emcid"/></xsl:variable>
        <xsl:for-each select="/root/channel">
            <xsl:if test="id = $ch_selected">
                ppid1=<xsl:value-of select="parentchannel"/>
            </xsl:if>

            <xsl:if test="channel">
                    <xsl:for-each select="channel">
                    <xsl:if test="id = $ch_selected">
                        ppid2=<xsl:value-of select="parentchannel"/>



                    </xsl:if>
                    <xsl:if test="channel">
                            <xsl:for-each select="channel">
                            <xsl:if test="id = $ch_selected">
                                ppid3=<xsl:value-of select="parentchannel"/>
                                <xsl:variable name="ch_at3"><xsl:value-of select="parentchannel"/></xsl:variable>

                            </xsl:if>
                            </xsl:for-each>shawn=<xsl:value-of select="$ch_at3"/>
                        </xsl:if>
                    </xsl:for-each>
                </xsl:if>

        </xsl:for-each>

 
Old February 2nd, 2007, 04:19 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Looks like you're trying to code this in a way that doesn't work for XSLT, but you haven't shown any code so it's hard to see where your misunderstanding lies.

Let us know what output you want to produce and we may be able to suggest the right design approach.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old February 2nd, 2007, 04:51 PM
Registered User
 
Join Date: Feb 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to sdaspen
Default

The ouput I would like is an xsl variable (or 2 or 3) set for the parentchannel of each channel node.
Each <channel> has an <id> and <parentchannel> elements.
<channel>
    <channel>
         <channel>

         </channel>
    </channel>

</channel>
I have a specific channel id provided via an <xsl:variable>. I need the parentchannel for that channel and all it's ancestors up to the root.



 
Old February 2nd, 2007, 06:34 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

An xsl:variable is not an output, it's purely a means to an end. In this case, it's probably not the right means to the end.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
traverse the whole tree without knowing the node dipsut XSLT 3 June 17th, 2011 07:49 AM
Not able to see the last tree view node suji C# 0 October 11th, 2008 12:34 AM
TREE / HIERARCHY pallone XSLT 3 June 13th, 2008 07:45 AM
insert object into tree node Xeon-Yk C# 1 November 13th, 2006 12:06 PM
using hyperlink controls as node with tree control connect2sandep ASP.NET 1.0 and 1.1 Professional 0 October 27th, 2005 02:00 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.