Mimicking a SELECT/CASE statement using XSL

Discussion in 'XML' started by Gadrin77, May 3, 2004.

  1. Gadrin77

    Gadrin77 Guest

    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.
     
    Gadrin77, May 3, 2004
    #1
    1. Advertising

  2. In article <>,
    Gadrin77 <> 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>
    <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.
    --

    Patrick TJ McPhee
    East York Canada
     
    Patrick TJ McPhee, May 4, 2004
    #2
    1. Advertising

  3. Gadrin77

    Andy Fish Guest

    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" <> wrote in message
    news:...
    > 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.
     
    Andy Fish, May 5, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tee
    Replies:
    3
    Views:
    7,821
    Herfried K. Wagner [MVP]
    Jun 23, 2004
  2. kmeezy
    Replies:
    0
    Views:
    541
    kmeezy
    May 7, 2006
  3. Edward
    Replies:
    1
    Views:
    373
    Oliver Wong
    Jul 20, 2006
  4. Lakshmi Narayanan.R

    "Case 1 To 5" in Select case, giving Error!!. Why?

    Lakshmi Narayanan.R, Mar 2, 2005, in forum: ASP General
    Replies:
    10
    Views:
    354
    Bob Barrows [MVP]
    Mar 4, 2005
  5. aa
    Replies:
    14
    Views:
    331
Loading...

Share This Page