|
|
 |
| 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.
|
 |

July 6th, 2009, 07:38 AM
|
|
Authorized User
|
|
Join Date: May 2009
Posts: 13
Thanks: 6
Thanked 0 Times in 0 Posts
|
|
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.
|

July 6th, 2009, 07:46 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 1,340
Thanks: 0
Thanked 26 Times in 26 Posts
|
|
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 -/
|
|
The Following User Says Thank You to samjudson For This Useful Post:
|
|

July 6th, 2009, 07:54 AM
|
|
Authorized User
|
|
Join Date: May 2009
Posts: 13
Thanks: 6
Thanked 0 Times in 0 Posts
|
|
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
|

July 6th, 2009, 08:00 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 1,340
Thanks: 0
Thanked 26 Times in 26 Posts
|
|
Same answer:
<Revision><Number><xsl:value-of select="$OtherSource[position()]"/></Number></Revision>
__________________
/- Sam Judson : Wrox Technical Editor -/
|

July 6th, 2009, 08:13 AM
|
 |
Wrox Author
Points: 12,642, Level: 48 |
|
|
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 3,900
Thanks: 0
Thanked 80 Times in 78 Posts
|
|
>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
|
|
The Following User Says Thank You to mhkay For This Useful Post:
|
|

July 6th, 2009, 08:23 AM
|
|
Authorized User
|
|
Join Date: May 2009
Posts: 13
Thanks: 6
Thanked 0 Times in 0 Posts
|
|
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.
|

July 6th, 2009, 08:39 AM
|
|
Authorized User
|
|
Join Date: May 2009
Posts: 13
Thanks: 6
Thanked 0 Times in 0 Posts
|
|
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.
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |