Variables in XSLT

O

Oleg Konovalov

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
 
D

David Carlisle

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
 

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

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top