Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 23rd, 2006, 01:53 AM
Authorized User
 
Join Date: Dec 2005
Location: , , .
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\"
Reply With Quote
  #2 (permalink)  
Old November 23rd, 2006, 04:47 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
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)
Reply With Quote
  #3 (permalink)  
Old November 23rd, 2006, 05:27 AM
mhkay's Avatar
Wrox Author
Points: 18,438, Level: 59
Points: 18,438, Level: 59 Points: 18,438, Level: 59 Points: 18,438, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,954
Thanks: 0
Thanked 290 Times in 285 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
Reply With Quote
  #4 (permalink)  
Old November 23rd, 2006, 05:40 AM
Authorized User
 
Join Date: Dec 2005
Location: , , .
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.

Reply With Quote
  #5 (permalink)  
Old November 23rd, 2006, 06:01 AM
mhkay's Avatar
Wrox Author
Points: 18,438, Level: 59
Points: 18,438, Level: 59 Points: 18,438, Level: 59 Points: 18,438, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,954
Thanks: 0
Thanked 290 Times in 285 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
Reply With Quote
  #6 (permalink)  
Old November 23rd, 2006, 06:05 AM
Authorized User
 
Join Date: Dec 2005
Location: , , .
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

Reply With Quote
  #7 (permalink)  
Old November 23rd, 2006, 06:16 AM
mhkay's Avatar
Wrox Author
Points: 18,438, Level: 59
Points: 18,438, Level: 59 Points: 18,438, Level: 59 Points: 18,438, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,954
Thanks: 0
Thanked 290 Times in 285 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
Reply With Quote
  #8 (permalink)  
Old November 23rd, 2006, 06:19 AM
Authorized User
 
Join Date: Dec 2005
Location: , , .
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!

Reply With Quote
  #9 (permalink)  
Old November 23rd, 2006, 06:33 AM
Authorized User
 
Join Date: Dec 2005
Location: , , .
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 ,

Reply With Quote
  #10 (permalink)  
Old November 23rd, 2006, 06:54 AM
mhkay's Avatar
Wrox Author
Points: 18,438, Level: 59
Points: 18,438, Level: 59 Points: 18,438, Level: 59 Points: 18,438, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,954
Thanks: 0
Thanked 290 Times in 285 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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 12:13 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.