Variables in XSLT

Discussion in 'XML' started by Oleg Konovalov, May 13, 2005.

  1. Hi,

    I am new to XSLT, trying to significantly modify somebody
    else's XSL. That is not 2.0.

    I need to create min & max variable(s)
    to be used in many templates and sub-templates
    based on certain conditions (not always).

    It can't be calculated at the beginning of the file
    (depends on other variables),
    so seems like it has to be made local variable(s).

    Since it's not easy to calculate (20-50 lines of code each)
    probably makes sense to make a template out of it.

    What I don't know is:
    1) how to make template to return a value, something like [that's not XSLT
    2.0]:
    <xsl:variable min><call-template name="calculate-min"/></xsl:variable>

    2) how to declare a global variable and then assign it much later in the
    code;

    3) Also, it there a way to compare a node value with a previous
    value within a for-each loop like (to find a max value of the nodes):
    <xsl:for-each mystruct/mystuct1>
    <xsl:if test="position() &gt; 1 and
    var2 &gt; var2(position()-1)">...do something...
    </xsl:if>
    </xsl:for-each>
    Would that work ?

    4) Can I get access to a value from another branch
    from within for-each (where var2 and var3 are "brothers")?
    Like:
    <xsl:for-each mystruct/mystruct1>
    <xsl:if test="var2 &gt; ../var3">...do something...
    </xsl:if>
    </xsl:for-each>)

    Any help is very appreciated.

    Thank you in advance,
    Oleg
    Oleg Konovalov, May 13, 2005
    #1
    1. Advertising

  2. Since it's not easy to calculate (20-50 lines of code each)
    probably makes sense to make a template out of it.


    Do you need a variable at all? It sounds like you just need a template.


    > 1) how to make template to return a value, something like [that's not XSLT
    > 2.0]:
    > <xsl:variable min><call-template name="calculate-min"/></xsl:variable>


    IN XSLT1 if you use xsl:variable with non-empty content (and no select
    attribute) then it always creates a result tree fragment. However the
    string value of this result tree fragment can be used as a number or
    other atomic value in XPath contexts so long as it has the right syntax
    for a number. If you use teh select attribute then a variable can be
    bound directly to a number (but then you can't call templates).

    > 2) how to declare a global variable and then assign it much later in the
    > code;


    In declarative languages variables are bound to values not memory
    locations so there is no notion of declaring a variable without giving
    it a value, and no notion of changing a value of a variable.
    In different scopes you can have local variables that happen to have the
    same name, but these variables are unrelated, the program would work teh
    same way if you changed the program to use different variable names in
    different scopes.

    > 3) Also, it there a way to compare a node value with a previous
    > value within a for-each loop like (to find a max value of the nodes):
    > <xsl:for-each mystruct/mystuct1>


    No. If you are iterating over an unsorted set of nodes you can get back
    to the previous node using the preceding or preceding-sibling-axis
    but in general you need to use a recursive template here to pass on teh
    current max value, or simpler but a bit less efficient use a for-each
    with xsl:sort and then just use the last (or first) item in teh sorted
    list as the maximum.

    > 4) Can I get access to a value from another branch


    The example you gave was correct xslt so yes, you have read acccess via
    XPath to the whole input tree (or trees).

    David
    David Carlisle, May 13, 2005
    #2
    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. Stylus Studio
    Replies:
    0
    Views:
    636
    Stylus Studio
    Aug 3, 2004
  2. Benjamin Hillsley
    Replies:
    3
    Views:
    1,658
    Dimitre Novatchev
    Sep 25, 2003
  3. ted
    Replies:
    1
    Views:
    610
    Laurens
    Jan 26, 2004
  4. Replies:
    2
    Views:
    707
    Henry S. Thompson
    Oct 19, 2005
  5. Replies:
    9
    Views:
    927
Loading...

Share This Page