p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


Go Back   p2p.wrox.com Forums > XML > XSLT
I forgot my password Register Now
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 p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.

Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 6th, 2009, 07:38 AM
Authorized User
Points: 51, Level: 1
Points: 51, Level: 1 Points: 51, Level: 1 Points: 51, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2009
Posts: 13
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old July 6th, 2009, 07:46 AM
samjudson's Avatar
Friend of Wrox
Points: 4,336, Level: 27
Points: 4,336, Level: 27 Points: 4,336, Level: 27 Points: 4,336, Level: 27
Activity: 40%
Activity: 40% Activity: 40% Activity: 40%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 1,340
Thanks: 0
Thanked 26 Times in 26 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 -/
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
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, 07:54 AM
Authorized User
Points: 51, Level: 1
Points: 51, Level: 1 Points: 51, Level: 1 Points: 51, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2009
Posts: 13
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #4 (permalink)  
Old July 6th, 2009, 08:00 AM
samjudson's Avatar
Friend of Wrox
Points: 4,336, Level: 27
Points: 4,336, Level: 27 Points: 4,336, Level: 27 Points: 4,336, Level: 27
Activity: 40%
Activity: 40% Activity: 40% Activity: 40%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 1,340
Thanks: 0
Thanked 26 Times in 26 Posts
Default

Same answer:

<Revision><Number><xsl:value-of select="$OtherSource[position()]"/></Number></Revision>
__________________
/- Sam Judson : Wrox Technical Editor -/
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #5 (permalink)  
Old July 6th, 2009, 08:13 AM
mhkay's Avatar
Wrox Author
Points: 12,642, Level: 48
Points: 12,642, Level: 48 Points: 12,642, Level: 48 Points: 12,642, Level: 48
Activity: 97%
Activity: 97% Activity: 97% Activity: 97%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 3,900
Thanks: 0
Thanked 80 Times in 78 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 08:13 AM. Reason: correction
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
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, 08:23 AM
Authorized User
Points: 51, Level: 1
Points: 51, Level: 1 Points: 51, Level: 1 Points: 51, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2009
Posts: 13
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #7 (permalink)  
Old July 6th, 2009, 08:39 AM
Authorized User
Points: 51, Level: 1
Points: 51, Level: 1 Points: 51, Level: 1 Points: 51, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2009
Posts: 13
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 08:43 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Incrementing the variable value Swetha XSLT 2 April 28th, 2008 05:43 PM
Incrementing member variable in Synchronized block vikkiefd Pro Java 0 March 24th, 2008 05:15 AM
Way to incrementing value of variable in xsl vikkiefd XSLT 14 March 12th, 2008 11:33 PM
XSL Loop and incrementing a variable 2007-03-29 XSLT 8 March 30th, 2007 11:41 AM
incrementing value of a variable akibaMaila VB.NET 2002/2003 Basics 4 July 5th, 2005 01:04 PM



All times are GMT -4. The time now is 11:32 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
© 2008 Wiley Publishing, Inc