Mimicking a SELECT/CASE statement using XSL

G

Gadrin77

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.
 
P

Patrick TJ McPhee

% 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>
<xsl:eek:therwise><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>
<xsl:eek:therwise>
<xsl:call-template name='guts'>
<xsl:with-param name='xsl-var1' select='20'/>
<xsl:with-param name='xsl-var2' select='21'/>
</xsl:call-template>
<'xsl:eek:therwise>
</xsl:choose>

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

Andy Fish

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top