473,322 Members | 1,241 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,322 software developers and data experts.

XSLT: Is the variable scope of <xsl:if> local?

With the XSLT 1.0 engine that I was forced to use, I have to parse old XML
scripts where the number (to be parsed and saved into $EPISODE_NUMBER_RAW)
that I want to parse is written with a comma seperator if it goes beyond 3
digits. i.e. the variable might be parsed as a string '1,324' or a number
56, depending on which XML file I have.

I was trying to get rid of the comma seperator by breaking the string into
two parts (that number will never go beyond 5 digits), like this:
<xsl:choose>
<xsl:when test="contains($EPISODE_NUMBER_RAW, ',')">
<xsl:variable name="EPISODE_NUMBER"
select="concat(substring-before($EPISODE_NUMBER_RAW,','),
substring-after($EPISODE_NUMBER_RAW, ',') )"/>
</xsl:when>

<xsl:otherwise>
<xsl:variable name="EPISODE_NUMBER" select="$EPISODE_NUMBER_RAW"/>
</xsl:otherwise>

</xsl:choose>

<xsl:value-of select="$EPISODE_NUMBER"/>
However, the XSLT throws an error saying that $EPISODE_NUMBER is not found.
Does <xsl:choosehas its own variable scope? If so, can anybody give me a
pointer on how to solve the problem?

Thanks a lot in advance.

Cheers,
Hoi
Feb 20 '08 #1
8 5613

Hoi Wong <wo*****@stanford.eduwrote in
<fp*********@news.Stanford.EDU>:
With the XSLT 1.0 engine that I was forced to use, I have
to parse old XML scripts where the number (to be parsed
and saved into $EPISODE_NUMBER_RAW) that I want to parse
is written with a comma seperator if it goes beyond 3
digits. i.e. the variable might be parsed as a string
'1,324' or a number 56, depending on which XML file I
have.

I was trying to get rid of the comma seperator by breaking
the string into two parts (that number will never go
beyond 5 digits), like this:
640K of memory will be enough for anybody.
<xsl:choose>
<xsl:when test="contains($EPISODE_NUMBER_RAW, ',')">
<xsl:variable name="EPISODE_NUMBER"
select="concat(substring-before($EPISODE_NUMBER_RAW,','),
substring-after($EPISODE_NUMBER_RAW, ',') )"/>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="EPISODE_NUMBER"
select="$EPISODE_NUMBER_RAW"/>
</xsl:otherwise>
</xsl:choose>

However, the XSLT throws an error saying that
$EPISODE_NUMBER is not found. Does <xsl:choosehas its
own variable scope? If so, can anybody give me a pointer
on how to solve the problem?
Works:

<xsl:variable name="number">
<xsl:choose>
<xsl:when test="contains($raw-number,',')">
<xsl:value-of select=
"
concat(substring-before($raw-number,','),
substring-after($raw-number,','))
"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$raw-number"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>

Better:

<xsl:variable name="number"
select="translate($raw-number,',','')"/>

Even better - stuff it into a named template and invoke
that. You never know when you might need this again.

--
When all you have is a transformation engine, everything
looks like a tree.
Feb 20 '08 #2
_All_ XSLT variable scopes are local.

--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Feb 20 '08 #3
translate() works perfectly! Thanks.

"Pavel Lepin" <p.*****@ctncorp.comwrote in message
news:fp**********@aioe.org...
>
Hoi Wong <wo*****@stanford.eduwrote in
<fp*********@news.Stanford.EDU>:
>With the XSLT 1.0 engine that I was forced to use, I have
to parse old XML scripts where the number (to be parsed
and saved into $EPISODE_NUMBER_RAW) that I want to parse
is written with a comma seperator if it goes beyond 3
digits. i.e. the variable might be parsed as a string
'1,324' or a number 56, depending on which XML file I
have.

I was trying to get rid of the comma seperator by breaking
the string into two parts (that number will never go
beyond 5 digits), like this:

640K of memory will be enough for anybody.
> <xsl:choose>
<xsl:when test="contains($EPISODE_NUMBER_RAW, ',')">
<xsl:variable name="EPISODE_NUMBER"
select="concat(substring-before($EPISODE_NUMBER_RAW,','),
substring-after($EPISODE_NUMBER_RAW, ',') )"/>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="EPISODE_NUMBER"
select="$EPISODE_NUMBER_RAW"/>
</xsl:otherwise>
</xsl:choose>

However, the XSLT throws an error saying that
$EPISODE_NUMBER is not found. Does <xsl:choosehas its
own variable scope? If so, can anybody give me a pointer
on how to solve the problem?

Works:

<xsl:variable name="number">
<xsl:choose>
<xsl:when test="contains($raw-number,',')">
<xsl:value-of select=
"
concat(substring-before($raw-number,','),
substring-after($raw-number,','))
"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$raw-number"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>

Better:

<xsl:variable name="number"
select="translate($raw-number,',','')"/>

Even better - stuff it into a named template and invoke
that. You never know when you might need this again.

--
When all you have is a transformation engine, everything
looks like a tree.

Feb 20 '08 #4

Joseph Kesselman <ke************@comcast.netwrote in
<47bc3401@kcnews01>:
_All_ XSLT variable scopes are local.
Ah, but what about xsl:variable children of xsl:stylesheet
element? Besides, I think it's not about lexical/dynamic
scoping, but about what a variable's scope is. This is
rarely if ever mentioned in tutorials, so you've got to
shuffle through the spec to get a precise definition, and
we all know how averse your J. Random Developer is to that.

Yes, the scope of a local variable is limited by the nearest
enclosing block in many of the modern languages. But XSLT
doesn't really have blocks; and, say, PHP does not adhere
to that convention. Compare:

pavel@debian:~/dev/php$ a scope.pl
use warnings; use strict;
sub tst {
my $foo = 1;
if ($foo) {
my $bar = 2;
}
print($foo . ' ' . $bar . "\n");
}
tst();
pavel@debian:~/dev/php$ perl scope.pl
Global symbol "$bar" requires explicit package name at
scope.pl line 7.
Execution of scope.pl aborted due to compilation errors.
pavel@debian:~/dev/php$ a scope.php
<?php error_reporting(E_ALL | E_STRICT);
function tst() {
$foo = 1;
if ($foo) {
$bar = 2;
}
print($foo . ' ' . $bar . "\n");
}
tst(); ?>
pavel@debian:~/dev/php$ php scope.php
1 2
pavel@debian:~/dev/php$

(Perl also has dynamic scoping, using which would produce
the same behaviour as observed in the PHP script above -
but the point is that in PHP, the scope of $bar is limited
by the body of the function it's defined in, not by the
nearest enclosing block.)

I've also had some trouble with this in my first days with
XSLT, so I can sort of sympathise with the OP.

--
When all you have is a transformation engine, everything
looks like a tree.
Feb 21 '08 #5
Pavel Lepin wrote:
>>_All_ XSLT variable scopes are local.
Ah, but what about xsl:variable children of xsl:stylesheet
element?
They're local to the stylesheet element and its content.
scoping, but about what a variable's scope is.
A variable's lexical scope in XSLT -- the area within which that
variable can be referenced by name -- is the xsl:variable element's
following siblings and their descendants, unless the same name is used
for another variable within that scope. Its dynamic scope is the
execution of the element which encloses the xsl:variable element.

Putting it another way: An XSLT variable is created at the xsl:variable
element, is directly visible only within the element that element
appears within.

--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Feb 21 '08 #6

Joseph Kesselman <ke************@comcast.netwrote in
<47bd9735$1@kcnews01>:
Pavel Lepin wrote:
>>>_All_ XSLT variable scopes are local.
Ah, but what about xsl:variable children of
xsl:stylesheet element?

They're local to the stylesheet element and its content.
The spec seems to disagree [11.4]:

A top-level variable-binding element declares a global
variable that is visible everywhere.

I believe this affects also affects imports and includes,
but gotta test that on a real processor or shuffle through
the spec some more to see if there's an explicit
description of those cases.

--
When all you have is a transformation engine, everything
looks like a tree.
Feb 21 '08 #7
I believe this affects also affects imports and includes,

.... I think that's right. One can quibble about the details of how this
interacts with import/include -- whether those "become part of the same
stylesheet element" in some reasonable sense -- but I don't think it's
worth debating.

In practical terms, it's a difference that rarely, if ever, makes a
difference.

--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Feb 21 '08 #8

Joseph Kesselman wrote:
>I believe this affects also affects imports and includes,

... I think that's right. One can quibble about the details of how this
interacts with import/include -- whether those "become part of the same
stylesheet element" in some reasonable sense -- but I don't think it's
worth debating.

In practical terms, it's a difference that rarely, if ever, makes a
difference.
I apologise for displaying the symptoms of my Compulsive Nitpicking Syndrome
in public yet again. :-)

--
"These men died for us... frequently!"
Feb 21 '08 #9

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

Similar topics

3
by: Lizard | last post by:
OK, total newbie here, so this may be a mind-numbingly dumb question AND I may be phrasing it badly. I have an xsl:template which looks like this: <xsl:template match="LoanRecord"> <hr>...
10
by: Tjerk Wolterink | last post by:
The following code does not work: <xsl:variable name="width" select="form:image-width"/> <xsl:if test="$width>$max_image_width"> <xsl:variable name="width" select="$max_image_width"/> </xsl:if>...
2
by: websls | last post by:
I tried to do this : <xsl:if test="ToutCompris"> some output </xsl:if> ToutCompris is a boolean element in my XML file My problem is the output is parse even when ToutCompris is false I...
5
by: Luke Vogel | last post by:
Hi all, Probably a really basic question, but I cant find an answer ... I have an xml file of books something like: <product> <isbn>0-735-61374-5</isbn> <title>Microsoft Visual Basic Step By...
3
by: tldisbro | last post by:
Hello All, I am trying to use the returned value of the <fo:page-number> element/function in my <xsl:if> test condition. But am unsuccessful in doing so. Is it possible to use it in this fashion...
5
by: ina | last post by:
Hello guys, My name is ina and I have a problem with a file xlst. I am newbie and sorry for this question, probably must be very simple. I have this xml file <?xml version="1.0"?>...
2
by: riceyeh | last post by:
Hi, What does <xsl:if test="not($values)"mean? What I do not understand is $values? Here, means array? And . = $value means current node is equal to the variable value? So the total meaning is...
1
by: cfli1688 | last post by:
I saw example of xsl:if and I want to use it. I know that &gt; is greater than. Is there one for equals to, I try &eq; and it does not recognize it. Where can I get a list for this? Also, I have...
4
by: mark4asp | last post by:
I'm getting a problem with this code and I think the offending linke is : <xsl:if test="$folder = 'Search'"> I want to test the value of the Folder element for a value of precisely "Search"...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.