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 October 15th, 2004, 09:39 AM
Registered User
Join Date: Oct 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Losing variables

Hi ,

Probably something really simple, but I'm stuck.

This is a stylesheet for calculating a breadcrumb trail in an external XML file.

The main template calls a template named "findbreadcrumb" that goes to work on the external file.

It works great, but:
once I want to output the Result, which is the variable $breadc, it comes up empty.

When I output the result IN the actual breadcrumb routine, it's there and correct: this is at the spot

Could anybody give me a hint why that correct variable is gone after I've left the template ?

The node for which I am calculating the breadcrumb is nr. 75 which is there and not empty.

Again: I've triple checked the routine and it works fine.
I'm just losing the variable...

Cheers and thanks in advance

Matthieu Brandt

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

    <xsl:variable name="varbreadc">
    <xsl:variable name="breadc">

    <xsl:variable name ="varmenuid" select="75">

    <xsl:template match="/">

         <xsl:call-template name="findbreadcrumb">
             <xsl:with-param name="menuid" select="$varmenuid"/>
             <xsl:with-param name="breadc" select="$varbreadc"/>

         Result: <xsl:value-of select="$breadc" />


     <xsl:template name="findbreadcrumb">
         <xsl:param name="menuid"/>
         <xsl:param name="breadc"/>

         <xsl:for-each select ="document('h:\XML\MenusWithPageCount.xml')/datarootmenu/MenusWithPageCount/Main_menu_nr[. = $menuid]">

                 <xsl:call-template name="bc">
                         <xsl:with-param name="breadc" select="parent::node()/Main_menu_name" />
                         <xsl:with-param name="parentnr" select ="parent::node()/Parent_menu_nr"/>



     <xsl:template name="bc">
           <xsl:param name="breadc" />
           <xsl:param name="parentnr"/>

                  <xsl:when test="$parentnr &gt; '0'">
                        <xsl:for-each select ="//MenusWithPageCount">
                          <xsl:if test ="Main_menu_nr = $parentnr" >
                                <xsl:call-template name="bc">
                              <xsl:with-param name="breadc" select= "concat (Main_menu_name,'---',$breadc)"/>
                             <xsl:with-param name="parentnr" select = "Parent_menu_nr"/>

         <xsl:when test="$parentnr = '0'">
             <!-- Finished with breadcrumb routine
                             Variable $breadc is correctly filled-->



Old October 15th, 2004, 10:33 AM
joefawcett's Avatar
Wrox Author
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts

Your variable is out of scope within the main template. There are a number of other points, the main being you would be much better off having the external document in a variable such as:
<xsl:param name = "menuData">h:\XML\MenusWithPageCount.xm</xsl:param>
<xsl:variable name="menuDoc" select="document($menuData)"/>
Then you pass the name of the file, for testing it will assume the default, change the for-each to:
<xsl:for-each select ="$datarootmenu/MenusWithPageCount/Main_menu_nr[. = $menuid]">
Why access the menu data like this, why not just pass the file to the processsor?


Joe (Co-author Beginning XML, 3rd edition)

P.S. Looks like your xml file has a different structure to before so show a small sample if you need more help.
Old October 16th, 2004, 01:59 AM
Registered User
Join Date: Oct 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts

Hi Joe

And thanks for your reply. I knew it was something with scopes....
But I'm still unsure how to repair it.

I will definitely study your remarks on the other points.
The reason I pass the file as an external document, is that I'm trying to figure out a way to work with two (or more XML) files.

The output is going to be one html page that has a bunch of stuff on it. This is the main XML file that the processor needs to handle.

That page will also need some other info from other XML files.
The breadcrumb being the most complicated one (for me).

Because the main xsl file will be big, I'm trying to program everything in neat little blocks and just import de xsl files seperately, have them work on any XML files they need.
But I'll need to pass some variables to those XSL templates and get some others back.

The xml file was indeed changed a bit:
this is what it looks like now :

<Main_menu_name>Principles and Tricks</Main_menu_name>


<Main_menu_name>Inversions on D</Main_menu_name>

<Main_menu_name>Bass walk down</Main_menu_name>


I guess I'm not used to working with 'scopes' etc.
And I used to be a computer programmer for a living (back in the 80's)
. Even worked on one of the parent::node() of internet.
XML and XSLT are exciting and just what the doctor ordered for the stuff I'm doing now: I'm writing a web based music book, throwing it into a database and then (hopefully) retrieving it in multiple formats.

Anyway: just like to tell you that I appreciate the help very much!

Cheers from the Netherlands,

Matthieu Brandt

Similar Threads
Thread Thread Starter Forum Replies Last Post
Losing a session variable Abbas Classic ASP Professional 2 March 21st, 2007 04:00 PM
Variables losing their values jrensink78 ASP.NET 1.0 and 1.1 Basics 3 April 13th, 2006 01:09 PM
Losing form variables (from posting) BunaZiua Dreamweaver (all versions) 1 February 23rd, 2004 04:46 PM
Losing session variables Justine Classic ASP Professional 3 January 16th, 2004 10:10 AM

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