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 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.
 
Old July 6th, 2009, 06:46 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
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?
The Following User Says Thank You to samjudson For This Useful Post:
chandra_perni (July 6th, 2009)
 
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
 
Old July 6th, 2009, 07:00 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
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?
 
Old July 6th, 2009, 07:13 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
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
The Following User Says Thank You to mhkay For This Useful Post:
chandra_perni (July 6th, 2009)
 
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.
 
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..





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





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