473,408 Members | 1,735 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

XSL -- Global Variables

I'm learning XML and XSL at the moment, but I still can't get my head
around the concept of non-updatable variables. I know we can use
recursion to cycle through a data structure and get the sum (say) of a
list of numbers, or a concatination of a set of strings (again,
example), but if I wanted to, for example, keep that value available
for another template later on to use, I can't see how I could do that.

Let me give a better example. I have written and template which is
called "FindMin" that cycles through a tree that finds the minimum. I
have checked the code and, when it it does go through all of the
subtrees and finds the minimum. However, it will only find the minimum
of each of the subtrees, since I cannot keep a global track of the
smallest value, since when I propagate up the tree, I will lose that
value. How could I rectify this?

Here's the code I've written. It may be a bit crude, but that's
because I've been messing around with it. Min and Path are declared as
an empty string (Min) and the root node of the tree in question at
first call.

<xsl:template name="FindMin">
<xsl:param name="Min"/>
<xsl:param name="Path"/>
<xsl:for-each select="./*">
<xsl:choose>
<xsl:when test="@value &lt; $Min">
<!-- Call template with updated min and path -->
<xsl:call-template name="FindMin">
<xsl:with-param name="Min" select="@value"/>
<xsl:with-param name="Path" select="."/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<!-- Call template with updated path only -->
<xsl:call-template name="FindMin">
<xsl:with-param name="Min" select="$Min"/>
<xsl:with-param name="Path" select="."/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
The tree I am using for this test is:

<node value="10">
<node value="15"/>
<node value="5">
<node value="7">
<node value="9"/>
<node value="8"/>
</node>
<node value="11"/>
</node>
</node>

Any ideas any one? I'm sure you've had this question before, but most
answers I've checked seem to use Microsoft's XML variation, which I'm
trying to avoid.

Regards

Johnny Ooi
Jul 20 '05 #1
6 4722
On 2 Nov 2004 12:20:41 -0800, Loopy <jj****@yahoo.com> wrote:
I'm learning XML and XSL at the moment, but I still can't get my head
around the concept of non-updatable variables. I know we can use
recursion to cycle through a data structure and get the sum (say) of a
list of numbers, or a concatination of a set of strings (again,
example), but if I wanted to, for example, keep that value available
for another template later on to use, I can't see how I could do that.

Let me give a better example. I have written and template which is
called "FindMin" that cycles through a tree that finds the minimum. I
have checked the code and, when it it does go through all of the
subtrees and finds the minimum. However, it will only find the minimum
of each of the subtrees, since I cannot keep a global track of the
smallest value, since when I propagate up the tree, I will lose that
value. How could I rectify this?

Here's the code I've written. It may be a bit crude, but that's
because I've been messing around with it. Min and Path are declared as
an empty string (Min) and the root node of the tree in question at
first call.


Hi,

Variables or parameters are indeed non-updateble in XSLT 1.0 and AFAIK there's nothing you can do about. But often, you can get around it by carefully overthinking the problem and redesigning the solution. Encapsulating 'xsl:call-template' with a variable might bring a solution in some cases.

I know that finding a solution to your specific example problem is not the reason for your post, but I'll put my solution here nonetheless:

<xsl:template name="FindMin">
<xsl:param name="Min">9999</xsl:param>
<xsl:choose>
<xsl:when test="count(//*[@value &lt; $Min]) &gt; 0">
<xsl:call-template name="FindMin">
<xsl:with-param name="Min" select="//*[@value &lt; $Min]/@value"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$Min"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
regards,

--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
Ceterum censeo XML omnibus esse utendum
Jul 20 '05 #2

Thanks for the code, it does the job. Two questions. First, if I wanted
to pass this to another template, I would place a call to the template
in the <xsl:otherwise> tag, correct? Second, could you step through the
code for me so I understand how the execution flows through. I'm using
the XSLT engine provided by Altova and that doesn't exactly provide a
friendly debug environment. :P

Johnny

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #3
Hi,
Thanks for the code, it does the job. Two questions. First, if I wanted
to pass this to another template, I would place a call to the template
in the <xsl:otherwise> tag, correct?
You could, But I wouldn't do that.
I would use:

<xsl:variable name="Minimum">
<xsl:call-template name="FindMin"/>
</xsl:variable>

<xsl:call-template name="do-something">
<xsl:with-param name="parameter" select="$Minimum"/>
</xsl:call-template>

Or, if the minimum's only purpose is being fed to another template ,I'd prefer to use:

<xsl:call-template name="do-something">
<xsl:with-param name="parameter">
<xsl:call-template name="FindMin"/>
</xsl:with-param>
</xsl:call-template>

Second, could you step through the
code for me so I understand how the execution flows through. I'm using
the XSLT engine provided by Altova and that doesn't exactly provide a
friendly debug environment. :P


<xsl:template name="FindMin">
<!-- if the param 'Min' is empty, give it some 'value' attribute (It doesn't matter from which node it comes) -->
<xsl:param name="Min"><xsl:value-of select="//@value"/></xsl:param>
<xsl:choose>
<xsl:when test="count(//*[@value &lt; $Min]) &gt; 0">
<!-- if the current node has any descendants with a 'value' attribute smaller than the current minimum -->
<!-- call this template again with a 'Min' parameter that is equal to the first 'value' attribute smaller than the current Minimum -->
<xsl:call-template name="FindMin">
<!-- the current node remains the same: we don't plunge into the tree -->
<xsl:with-param name="Min" select="//*[@value &lt; $Min]/@value"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<!-- if the tree has no such descendents (i.e. the Minimum is found), display the Minimum -->
<!-- the template has done it's job and all recursively called templates above will close silently-->
<xsl:value-of select="$Min"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

Btw, I don't have any debug environment.

Hope this helps.
regards,
--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
Ceterum censeo XML omnibus esse utendum
Jul 20 '05 #4
Wow, so I didn't need to worry about doing all those recursive calls, I
could have used the "//" selector instead. So this selects all
decendents at all levels, is that right?


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #5
> Wow, so I didn't need to worry about doing all those recursive calls, I
could have used the "//" selector instead. So this selects all
decendents at all levels, is that right?


That's coorect. ('//' is short for '/descendant-or-self::node()/')

But one could argue if this Xpath approach is better than using recursive calls.

regards,
--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
Ceterum censeo XML omnibus esse utendum
Jul 20 '05 #6

Thanks for that, I'm going to go play with that and see what I can make
of it.

Johnny

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Matt | last post by:
Greetings, What are people's thoughts on global variables in C++? Why are we taught not to use them in programming? Is it true that if you are running two copies of the C program one copy can...
4
by: Andrew V. Romero | last post by:
I have been working on a function which makes it easier for me to pull variables from the URL. So far I have: <script language="JavaScript"> var variablesInUrl; var vArray = new Array(); ...
12
by: David WOO | last post by:
Hi, I am a newbie on C++, I need to define some global variables which should be accessible to most classes. In the mean time, I don't won't the global variables be modified freely at most of...
2
by: Bryan Parkoff | last post by:
….I would like to know which is the best optimization to use global variable or global struct. I always tell C/C++ Compiler to turn on optimization. ….I use underscore between first name and...
17
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm...
33
by: MLH | last post by:
I've read some posts indicating that having tons of GV's in an Access app is a bad idea. Personally, I love GVs and I use them (possibly abuse them) all the time for everything imaginable - have...
9
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang...
5
by: Sandman | last post by:
I dont think I understand them. I've read the section on scope in the manual inside out. I'm running PHP 5.2.0 Here is the code I'm working on: //include_me.php <?php $MYVAR = array(); global...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.