|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
November 23rd, 2006, 01:53 AM
|
Authorized User
|
|
Join Date: Dec 2005
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
XSL: template reuse
This is regarding a counter issue.
Is there a possiblity that - every time i call a template - the template should increment the value by one.
For instance...if there is a counter variable x, and a counter template named xxxtemplate....every time i call the xxxtemplate, the counter variable should get incremented by 1.
[Which means every time i get to fetch this x variable...it should get incremented by one]
Kindly help, Thanx in advance.
__________________
Ramesh
\"Always Look For Something NEW\"
|
November 23rd, 2006, 04:47 AM
|
|
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
As stated the answer is no, XSLT doesn't have mutable variables.
If you give an example of your problem then perhaps there will be another way of tackling it.
--
Joe ( Microsoft MVP - XML)
|
November 23rd, 2006, 05:27 AM
|
|
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
You can't update variables in a declarative language.
You can often achieve the effect your require using the position() function, or using an expression such as count(preceding-sibling::x).
In other cases you can use recursion: a template can call itself supplying a parameter such as
<xsl:with-param name="p" select="$p + 1"/>
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
November 23rd, 2006, 05:40 AM
|
Authorized User
|
|
Join Date: Dec 2005
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Joe,
Thanx for your help in this regard. Lemme try to brief you out a lil more.
A certain block of elements have to get repeated
- which depends on certain set of attributes/rather conditions.
So when a condition is true - there is a need to execute this block which has a OrderNumber tag
that needs to get incremented with each loop execution.
Thatz the reason - for posting my previous message - of having a global variable which would get
incremented everytime, i call the templte that has the set of elements.
For instance if the input xml has
<test condition1='Y' condition2='Y' condition3='Y'condition4='N'condition5='Y'/>
I would be checking for each condition status.
For all the tru ones i need to call a template which would have
<OrderItem>
<Action>Add</Action>
<OrderNumber>1</OrderNumber>
<OrderSubtype/>
<ContactLastName/>
<ContactTelephone/>
<ContactTitle/>
<ContactFirstName/>
</OrderItem>
Here the OrderNumber has to get incremented each time the template is called.
To make it very clear, if condition1 is Y, then the template is called once.
Hence, here - in the present stance - 4 conditions are Y...and hence there is a need to
call the above templete 4 times..The OrderNumber should abide by this too.
Hope you get my point.
thanx for your help.
|
November 23rd, 2006, 06:01 AM
|
|
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
You can do
<xsl:apply-templates select="@*[.='Y']"
<xsl:template match="@*">
....in here position() will give you the value you need ...
If you want the attributes processed in alphabetical order add an xsl:sort select="name()" as a child of xsl:apply-templates.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
November 23rd, 2006, 06:05 AM
|
Authorized User
|
|
Join Date: Dec 2005
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanx for the reply mike.
But how could we possibly get the position()?
thanx
|
November 23rd, 2006, 06:16 AM
|
|
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
position() is just an XPath function call, you can use it anywhere an XPath expression is allowed, e.g. in xsl:value-of.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
November 23rd, 2006, 06:19 AM
|
Authorized User
|
|
Join Date: Dec 2005
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
ok mike. Lemme try that.
Thanx a lot for your timely help.
Appreciate it!
|
November 23rd, 2006, 06:33 AM
|
Authorized User
|
|
Join Date: Dec 2005
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Mike/Joe,
One more issue here is that all the conditions that holds Y may not be required to execute the loop.
<test condition1='Y' condition2='Y' condition3='Y'condition4='N'condition5='Y'/>
Here if suppose i need only condition1/2/5 - only if these conditions hold Y, then i need to execute.
So i hope the template cant be a generic one and that we can't go for @*[.='Y']
Any comments on this?
thanx ,
|
November 23rd, 2006, 06:54 AM
|
|
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
Then you could use
apply-templates select="(@condition1, @condition2, @condition5)[.='Y']"
This would also give you a predictable order of processing the attributes.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|
|