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 January 21st, 2010, 05:15 AM
Friend of Wrox
 
Join Date: Jan 2007
Posts: 115
Thanks: 2
Thanked 0 Times in 0 Posts
Question Create & update global var

Good day,

I created a variable, But when i want to use it at another place to check the value of the variable. I get the error: "variable created outside of scope."

Code:
<xsl:variable name="VenExpand" select="Attributes/Attribute[ @name='visible']" />

How do i create a Global var on top & then anywhere give it a value, so that i can use it throughout.

Regards
 
Old January 21st, 2010, 05:24 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You can never 'change' the value of a variable in XSLT, as it is a functional language.

You can declare a global variable fine by defining it outside of any template and then refer to it inside any template, you just can't 'update' its value.

Try explaining the problem you are trying to solve, rather than how you are trying to solve it and we may be able to offer an alternative.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old January 21st, 2010, 05:36 AM
Friend of Wrox
 
Join Date: Jan 2007
Posts: 115
Thanks: 2
Thanked 0 Times in 0 Posts
Question

Hi, I would like to display a table based on whether an object is visible.

The object is being loaded on the foreach & the table is outside the foreach.
I can set the var when i'm in the foreach. But after the foreach when i want to check if i have to show the table the var is outisde it's scope.

Code:
<xsl:for-each ..
   <xsl:call-templatename="CreateObject">
         <xsl:if test="@name='VE10IntVendorFields'">
            <xsl:variable name="VenEx" select="Attributes/Attribute[ @name='visible']" />
          ....
</xsl:for-each>
 
<xsl:if test="$VenEx = 'True'"> 
  <table>
I hope this help... Please help
 
Old January 21st, 2010, 05:45 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You'll just have to work out how to refer to that item in your XML without being inside the for-each loop.

I don't know what your input XML looks like and you missed the select attribute for the for-each instruction so this is just a guess, but something like this:

Code:
<xsl:variable name="VenEx" select="Object[@name='VE10IntVendorFields']/Attributes/Attribute[@name='visible']"/>

<xsl:if test="$VenEx='True'">
...
</xsl:if>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old January 21st, 2010, 06:20 AM
Friend of Wrox
 
Join Date: Jan 2007
Posts: 115
Thanks: 2
Thanked 0 Times in 0 Posts
Question

I'm editing the xslt file & it's a 3rd party application (site) searched & can't find the xml files.

Please help, i still get the error: "VarEn is either not defined or outside of scope"

Code:
<!-- First do all the ungrouped objects -->
<xsl:for-eachselect="Activity/ObjectGroup[@type='default']/Object">
<xsl:sortselect="@index"data-type="number" />
<xsl:variablename="index"select="@index" />
<xsl:variablename="nextNode"select="../Object[ @index > $index ][1]" />

<xsl:variablename="VenEx"select="Object[@name='VE10IntVendorFields']/Attributes/Attribute[@name='visible']"/>
 
</xsl:for-each>

 
 
<xsl:iftest="$VenEx = 'True'"> 
  <table>
 
Old January 21st, 2010, 06:30 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Your <xsl:variable> instruction CANNOT go INSIDE the <xsl:for-each> instruction, otherwise it will not exist once you get out of it.

Imagine in a C# program if you wrote the following:

Code:
function Method1()
{
   int i = 10;
   Method2();
}

function Method2()
{
   Console.WriteLine("i = " + i); // This will not compile - i does not exist in Method2().
}
The same is true in XSLT - if you declare the variable inside the <xsl:for-each> instruction it doesn't exist anywhere apart from inside that instruction.

Code:
<xsl:for-each>
...
</xsl:for-each>

<xsl:variable name="VenEx" select="Activity/ObjectGroup[@type='default']/Object[@name='VE10IntVendorFields']/Attributes/Attribute[@name='visible']"/>

<xsl:if test="$VenEx = 'True'">
   <table>
    ...
    </table>
</xsl:if>
Also, and we have mentioned this more than once now - please try to paste your code so that it retains the spaces in your XML markup - trying to read or test your code is hard enough as it is without the extra work.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
ismailc (January 21st, 2010)
 
Old January 21st, 2010, 08:12 AM
Friend of Wrox
 
Join Date: Jan 2007
Posts: 115
Thanks: 2
Thanked 0 Times in 0 Posts
Question

Thank You for the explenation - it makes sense

I dont get any errors now but I cant get my var value i don't thinks it sets a value, I dont see the table :(

the var i define here has a vlue of "true"
Code:
<xsl:for-eachselect="Activity/ObjectGroup[@type='default']/Object">
<xsl:sortselect="@index"data-type="number" />
<xsl:variablename="index"select="@index" />
<xsl:variablename="nextNode"select="../Object[ @index > $index ][1]" />
<xsl:variablename="nextCol"select="$nextNode/@columnNo" />
<xsl:variablename="vis"select="string(Attributes/Attribute[ @name='visible'])" />
<xsl:variablename="visNextCol"select="string($nextNode/Attributes/Attribute[ @name='visible'])" />
<xsl:call-templatename="CreateObject"><xsl:with-paramname="Object"select="." /></xsl:call-template>
 
<xsl:if test="@name='VE1077Doc'">
<xsl:variablename="VenExpand"select="Attributes/Attribute[ @name='visible']" />
<xsl:value-ofselect="$VenExpand" />
</xsl:if>
 
</xsl:for-each>


Here i can't seem to set the value or check the value of var
Code:
<xsl:variablename="VenEx"select="Object[@name='VE1077Doc']/Attributes/Attribute[ @name='visible']"/>
<xsl:value-ofselect="$VenEx" />
<xsl:if test="$VenEx = 'True'"> 
   <table>

Last edited by ismailc; January 21st, 2010 at 08:22 AM..
 
Old January 21st, 2010, 08:20 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Without seeing your input XML its almost impossible to answer, seeing as how you are doing some complicated sorting and stuff inside the for-each loop.

If you don't know what XML this XSLT file is processing then can I suggest you ask someone who does know, because without an example XML to process you're not going to get very far.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old January 21st, 2010, 08:34 AM
Friend of Wrox
 
Join Date: Jan 2007
Posts: 115
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Thank You for helping
 
Old January 21st, 2010, 03:26 PM
Friend of Wrox
 
Join Date: Jan 2007
Posts: 115
Thanks: 2
Thanked 0 Times in 0 Posts
Wink

Thank You Samjudson

I finally got it going, it took me a while i tried to read the xslt file's code & played a bit.
Working on your code & a lot of trying this & that I got it.

Code:
 
<xsl:variablename="VenEx2"select="Activity/ObjectGroup[@type='default']/Object[@name='VE1077Doc']/Attributes/Attribute[@name='visible']" />

<xsl:value-ofselect="$VenEx2" />





Similar Threads
Thread Thread Starter Forum Replies Last Post
Pass value to second page (create & pass var) ismailc ASP.NET 2.0 Basics 8 April 24th, 2010 07:03 AM
Update databse from global dayash_r_prasad Classic ASP Databases 0 August 3rd, 2006 04:21 AM
How do I use a global var as an output file name pmcquirk SQL Server DTS 2 May 5th, 2006 03:34 AM
UPDATE in Global.asa hoffmann Classic ASP Databases 5 November 14th, 2003 10:54 AM





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