Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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
 
Old November 23rd, 2006, 01:53 AM
Authorized User
 
Join Date: Dec 2005
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to ramesh.kumarm
Default 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\"
 
Old November 23rd, 2006, 04:47 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

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)
 
Old November 23rd, 2006, 05:27 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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
 
Old November 23rd, 2006, 05:40 AM
Authorized User
 
Join Date: Dec 2005
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to ramesh.kumarm
Default

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.

 
Old November 23rd, 2006, 06:01 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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
 
Old November 23rd, 2006, 06:05 AM
Authorized User
 
Join Date: Dec 2005
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to ramesh.kumarm
Default

Thanx for the reply mike.
But how could we possibly get the position()?

thanx

 
Old November 23rd, 2006, 06:16 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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
 
Old November 23rd, 2006, 06:19 AM
Authorized User
 
Join Date: Dec 2005
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to ramesh.kumarm
Default

ok mike. Lemme try that.
Thanx a lot for your timely help.
Appreciate it!

 
Old November 23rd, 2006, 06:33 AM
Authorized User
 
Join Date: Dec 2005
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to ramesh.kumarm
Default

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 ,

 
Old November 23rd, 2006, 06:54 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
output xsl in predefined template newuser2525 XSLT 1 December 24th, 2007 08:30 AM
xsl:template problem mickhughes XSLT 1 August 16th, 2007 08:40 AM
xsl:template match 'overlapping' ? Kabe XSLT 1 February 25th, 2005 06:03 AM
xsl template concat problem nambati XSLT 5 January 31st, 2005 03:43 PM
help with xsl template match enT XSLT 9 September 24th, 2003 06:21 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.