Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Variables in XSLT

Reply
Thread Tools

Variables in XSLT

 
 
Oleg Konovalov
Guest
Posts: n/a
 
      05-13-2005

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[i]>
<xsl:if test="var2 &gt; ../var3">...do something...
</xsl:if>
</xsl:for-each>)

Any help is very appreciated.

Thank you in advance,
Oleg


 
Reply With Quote
 
 
 
 
David Carlisle
Guest
Posts: n/a
 
      05-13-2005

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Put variables into member variables or function variables? tjumail@gmail.com C++ 9 03-23-2008 04:03 PM
Multiple XSLT Transforms using a Controller XSLT sneill@mxlogic.com XML 2 10-19-2005 11:00 AM
ANN: New low-cost XML Editor, XSLT Editor, XSLT Debugger, DTD/Schema Editor Stylus Studio Java 0 08-03-2004 03:53 PM
xslt alone or xslt/java for static site? ted XML 1 01-26-2004 10:41 AM
[XSLT]Passing values from Javascript to a XSLT variable Benjamin Hillsley XML 3 09-25-2003 04:50 AM



Advertisments