Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Mimicking a SELECT/CASE statement using XSL

Reply
Thread Tools

Mimicking a SELECT/CASE statement using XSL

 
 
Gadrin77
Guest
Posts: n/a
 
      05-03-2004
as a newbie to XSL, is it possible to mimic a SELECT/CASE statement
using
XSL?

I tried a quickie and I kept getting errors either using PARAM or
WITH-PARAM
in the wrong place or VARIABLE. I ended up using .createProcessor and
doing
all the work behind the scenes in my programming language, then using
the
..addParameter to pass the values.

I wanted to select data, then set variables using XSL:CHOOSE and
XSL:IF based
on the values found in the data, then further alter these farther down
in the XSL with math.

pseudo-code:

xml:
<main class="1">

XSL Logic...

if class == 1

xsl-var1 = 10
xsl-var2 = 20

else

xsl-var1 = 50
xsl-var2 = 60

endif

myNewNumber = "value-of xsl-var1 + value-of xsl-var2"

'convert myNewNumber to kilograms (was pounds)

myNewNumber = myNewNumber * 2.2

etc, etc.

I'm not sure the value of a XSL:VARIABLE can be redefined.

sorry for the fuzziness, any help is greatly appreciated.
 
Reply With Quote
 
 
 
 
Patrick TJ McPhee
Guest
Posts: n/a
 
      05-03-2004
In article <(E-Mail Removed) >,
Gadrin77 <(E-Mail Removed)> wrote:

% as a newbie to XSL, is it possible to mimic a SELECT/CASE statement
% using
% XSL?

Yes.

% I tried a quickie and I kept getting errors either using PARAM or
% WITH-PARAM
% in the wrong place or VARIABLE.

With is nothing to do with the former question. Yes, there's something
like a select (xsl:choose), and no, it doesn't change the scoping
rules for variables.

<xsl:variable name='xsl-var1'>
<xsl:choose>
<xsl:when test='$class = 1'><xsl:text>10</xsl:text></xsl:if>
<xsltherwise><xsl:text>20</xsl:text></xsl:if>
</xsl:choose>
</xsl:variable name='xsl-var1'>

% I'm not sure the value of a XSL:VARIABLE can be redefined.

It can't be. You can get the same effect using parameters and
recursive template calls. You could also use recursive calls to
solve your initial problem.

<xsl:choose>
<xsl:when test='$class = 1'>
<xsl:call-template name='guts'>
<xsl:with-param name='xsl-var1' select='10'/>
<xsl:with-param name='xsl-var2' select='11'/>
</xsl:call-template>
</xsl:when>
<xsltherwise>
<xsl:call-template name='guts'>
<xsl:with-param name='xsl-var1' select='20'/>
<xsl:with-param name='xsl-var2' select='21'/>
</xsl:call-template>
<'xsltherwise>
</xsl:choose>

then you'd use the parameters in a template called guts.
--

Patrick TJ McPhee
East York Canada
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Andy Fish
Guest
Posts: n/a
 
      05-05-2004
In addition to Patrick's comments, it's worth adding that XSLT follows
what's known as a "functional programming" paradigm, very different to
procedural programming (mainly in that variables can't be updated which at
first seems crazy to a traditional programmer)

If you aren't familiar with functional programming, I'd recommend you try
and get your head round these concepts so you can understand how to use
recursion in templates (Jeni Tennision's "beginning XSLT" is very good book
but I'm sure there are others)

Andy


"Gadrin77" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> as a newbie to XSL, is it possible to mimic a SELECT/CASE statement
> using
> XSL?
>
> I tried a quickie and I kept getting errors either using PARAM or
> WITH-PARAM
> in the wrong place or VARIABLE. I ended up using .createProcessor and
> doing
> all the work behind the scenes in my programming language, then using
> the
> .addParameter to pass the values.
>
> I wanted to select data, then set variables using XSL:CHOOSE and
> XSL:IF based
> on the values found in the data, then further alter these farther down
> in the XSL with math.
>
> pseudo-code:
>
> xml:
> <main class="1">
>
> XSL Logic...
>
> if class == 1
>
> xsl-var1 = 10
> xsl-var2 = 20
>
> else
>
> xsl-var1 = 50
> xsl-var2 = 60
>
> endif
>
> myNewNumber = "value-of xsl-var1 + value-of xsl-var2"
>
> 'convert myNewNumber to kilograms (was pounds)
>
> myNewNumber = myNewNumber * 2.2
>
> etc, etc.
>
> I'm not sure the value of a XSL:VARIABLE can be redefined.
>
> sorry for the fuzziness, any help is greatly appreciated.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
legality of mimicking the Windows look and feel jaredmac@gmail.com Java 14 12-13-2006 11:13 PM
java classes mimicking WinBatch Edward Java 1 07-20-2006 05:38 PM
Mimicking MS Access interface in .ASP.NET 2.0 kmeezy ASP .Net 0 05-07-2006 08:52 PM
Mimicking CAT or TYPE in C mario.demiguel@gmail.com C Programming 9 10-12-2005 10:31 AM
XSL Question tp xsl:for-each and xsl:variable schaf@2wire.ch XML 1 05-27-2005 09:25 PM



Advertisments