the varable thing...

Discussion in 'XML' started by R, Mar 23, 2005.

  1. R

    R Guest

    Hello everybody.

    I hava a trouble with variables.

    Value of my variable depends on count() if count() returns a number
    less then 2
    my variable ($width) use default value: 2

    here is my XSLT code:

    <xsl:choose>
    <xsl:when test="count(../group[@style='inline']/*) > 2">
    <xsl:variable name="width">
    <xsl:value-of select="count(../group[@style='inline']/*)"/>
    </xsl:variable>
    </xsl:when>
    <xsl:eek:therwise>
    <xsl:variable name="width">2</xsl:variable>
    </xsl:eek:therwise>
    </xsl:choose>

    and later on:

    <tr>
    <th colspan="{$width}">....

    and when my PHP5 DOM Parser reaches this moment i receive error:

    Warning: runtime error: file
    /home/xh/workspace/ERS_new/stylesheets/utils/xform.xsl element th in
    Warning: unregistered variable width in

    if I replace $width with count(...) everything is OK - but I need a
    variable
    to handle it when for example count() < 2

    and by the way - one more question

    count(../group[@style='inline']/*) returns number of children of all
    group
    elements with attribute style.
    How can I select ony the maximal count of children of groups elements?

    thanks in advance for any help
    best regards
    R
    R, Mar 23, 2005
    #1
    1. Advertising

  2. R wrote:


    > I hava a trouble with variables.
    >
    > Value of my variable depends on count() if count() returns a number
    > less then 2
    > my variable ($width) use default value: 2
    >
    > here is my XSLT code:
    >
    > <xsl:choose>
    > <xsl:when test="count(../group[@style='inline']/*) > 2">
    > <xsl:variable name="width">
    > <xsl:value-of select="count(../group[@style='inline']/*)"/>
    > </xsl:variable>
    > </xsl:when>
    > <xsl:eek:therwise>
    > <xsl:variable name="width">2</xsl:variable>
    > </xsl:eek:therwise>
    > </xsl:choose>


    Those variables are then only known in the scope of the parent element,
    meaning not in any way "later on".
    So you probably need
    <xsl:variable name="width">
    <xsl:choose>
    ...
    </xsl:choose>
    <xsl:eek:therwise>...</xsl:eek:therwise>
    </xls:variable>



    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Mar 23, 2005
    #2
    1. Advertising

  3. (R) writes:

    > Hello everybody.
    >
    > I hava a trouble with variables.
    >


    The scope of a variable binding is the element in which the binding
    appears so her:

    >
    > <xsl:choose>
    > <xsl:when test="count(../group[@style='inline']/*) > 2">
    > <xsl:variable name="width">
    > <xsl:value-of select="count(../group[@style='inline']/*)"/>
    > </xsl:variable>
    > </xsl:when>


    The scope is the xsl:when and it's gone out of scope as soon as it's defined.

    > <xsl:eek:therwise>
    > <xsl:variable name="width">2</xsl:variable>
    > </xsl:eek:therwise>
    > </xsl:choose>
    >


    You should make your code match the english description of what you
    wanted to do: you didn't want to only conditionally declare a variable
    depending on whether some condition was true, you want to
    unconditionally declare the variable, with a value that depends on some
    condition, so place the condition in the content of the variable binding:

    <xsl:variable name="width">
    <xsl:choose>
    <xsl:when test="count(../group[@style='inline']/*) > 2">
    <xsl:value-of select="count(../group[@style='inline']/*)"/>

    </xsl:when>
    <xsl:eek:therwise>2</xsl:eek:therwise>
    </xsl:choose>
    </xsl:variable>

    Actually for the test you don't need to count them all and then compare
    that against 2, you just want to know if there is a third node, so

    <xsl:when test="(../group[@style='inline']/*)[3]">

    (most uses of count() in a test can be replaced in the same way)

    In this case I think your value is in fact 2 + the number of nodes in
    the source (not counting the first 2, if they exist) in which case you
    can define that directly as


    <xsl:variable name="width" select="2 + count((../group[@style='inline']/*)[position()&gt;2])">"/>

    David
    David Carlisle, Mar 23, 2005
    #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. Replies:
    0
    Views:
    6,679
  2. =?Utf-8?B?R2FyeQ==?=

    Help Session Varable Disappeared (asp.net 2.0)

    =?Utf-8?B?R2FyeQ==?=, Jul 5, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    423
    Cowboy \(Gregory A. Beamer\)
    Jul 5, 2006
  3. Ron_Adam
    Replies:
    3
    Views:
    521
    Ron_Adam
    Apr 1, 2005
  4. Graham Mattingley

    Test varable for numeric value ??

    Graham Mattingley, Oct 8, 2003, in forum: ASP General
    Replies:
    1
    Views:
    128
    Ken Schaefer
    Oct 8, 2003
  5. BOBss

    Retrieving Varable from ASP to ASP.

    BOBss, Jun 5, 2007, in forum: ASP General
    Replies:
    3
    Views:
    138
    Dave Anderson
    Jun 6, 2007
Loading...

Share This Page