Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | 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 July 6th, 2009, 06:38 AM
Authorized User
 
Join Date: May 2009
Posts: 15
Thanks: 6
Thanked 0 Times in 0 Posts
Default Incrementing a variable in XSL

Input XML:
-----------
<OUT>
<OUT_ITEM>
<ITEM_ID>151844</ITEM_ID>
</OUT_ITEM>
<OUT_ITEM>
<ITEM_ID>151845</ITEM_ID>
</OUT_ITEM>
<OUT_ITEM>
<ITEM_ID>151846</ITEM_ID>
</OUT_ITEM>
</OUT>

Expected Output:
----------------
<?xml version = '1.0' encoding = 'UTF-8'?>
<ILResponse>
<DA>
<Revision>
<Number>1</Number>
</Revision>
</DA>
<DA>
<Revision>
<Number>2</Number>
</Revision>
</DA>
<DA>
<Revision>
<Number>3</Number>
</Revision>
</DA>
</ILResponse>

XSL tried code:
---------------
<xsl:template match="/">
<ILResponse>
<xsl:for-each select="OUT/OUT_ITEM">
<DA>
<xsl:variable name="ItemIndex" select="0"/>
<xsl:variable name="ItemIndex1" select="$ItemIndex+1"/>
<Revision>
<Number>
<xsl:value-of select="$ItemIndex1"/>
</Number>
</Revision>
</DA>
</xsl:for-each>
</ILResponse>
</xsl:template>

I want to increment the variable for the no of times the OUT_ITEM are there in the input XML. My goal is to maintain the index value. please suggest. Found that every time the initial value of the variable is returned.
Reply With Quote
  #2 (permalink)  
Old July 6th, 2009, 06:46 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You cannot update variables in XSLT, therefore you cannot increment a variable in the way you are asking to.

In the scenario you mention above I suspect you can just use the position() function which will return the position of the current item in matching the template.

<xsl:value-of select="position()"/>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
Reply With Quote
The Following User Says Thank You to samjudson For This Useful Post:
chandra_perni (July 6th, 2009)
  #3 (permalink)  
Old July 6th, 2009, 06:54 AM
Authorized User
 
Join Date: May 2009
Posts: 15
Thanks: 6
Thanked 0 Times in 0 Posts
Default

Hi samjudson thanks for the reply. No actual my requirement is different. This is a sample template which i posted for letting my requirement. I want to update the variable each time which is not possible in xslt. Actually i will be getting the xsl from other xml file where i want to traverse based on the index value Ex: <Revision><Number><xsl:value-of select="$OtherSource[1]"/></Number></Revision>. Let me try it through XPath. Thank you
Reply With Quote
  #4 (permalink)  
Old July 6th, 2009, 07:00 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Same answer:

<Revision><Number><xsl:value-of select="$OtherSource[position()]"/></Number></Revision>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
Reply With Quote
  #5 (permalink)  
Old July 6th, 2009, 07:13 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>Same answer:

<Revision><Number><xsl:value-of select="$OtherSource[position()]"/></Number></Revision>

Umm, no: it needs to be

<Revision><Number><xsl:variable name="pos" select="position()"/><xsl:value-of select="$OtherSource[$pos]"/></Number></Revision>

because position() changes its value inside a predicate.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference

Last edited by mhkay; July 6th, 2009 at 07:13 AM.. Reason: correction
Reply With Quote
The Following User Says Thank You to mhkay For This Useful Post:
chandra_perni (July 6th, 2009)
  #6 (permalink)  
Old July 6th, 2009, 07:23 AM
Authorized User
 
Join Date: May 2009
Posts: 15
Thanks: 6
Thanked 0 Times in 0 Posts
Default

Hi samjudson thank you. The position() function works in this use case i have given but not in my actual case. Thank you. I am able to achieve using the XPath. It always returns 1 in my actual case.
Reply With Quote
  #7 (permalink)  
Old July 6th, 2009, 07:39 AM
Authorized User
 
Join Date: May 2009
Posts: 15
Thanks: 6
Thanked 0 Times in 0 Posts
Default

Thanks Michael yes you are right if we initialize through a variable and then pass it works.

Last edited by chandra_perni; July 6th, 2009 at 07:43 AM..
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
Incrementing the variable value Swetha XSLT 2 April 28th, 2008 04:43 PM
Incrementing member variable in Synchronized block vikkiefd Pro Java 0 March 24th, 2008 04:15 AM
Way to incrementing value of variable in xsl vikkiefd XSLT 14 March 12th, 2008 10:33 PM
XSL Loop and incrementing a variable 2007-03-29 XSLT 8 March 30th, 2007 10:41 AM
incrementing value of a variable akibaMaila VB.NET 2002/2003 Basics 4 July 5th, 2005 12:04 PM



All times are GMT -4. The time now is 04:47 AM.


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