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 Display Modes
  #1 (permalink)  
Old January 9th, 2009, 11:08 AM
Registered User
 
Join Date: Sep 2008
Location: , , India.
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to sandeepbhutani304 Send a message via Yahoo to sandeepbhutani304
Default XSLT: Need to concatenate strings in loop and hold them for later use

Hi,
I am making some loops using xsl:for-each.
Now I want to concatenate strings of each iteration to the previous iteration results.
And want to keep it in some variable for later use.

e.g.:
for each iteration suppose i get a,b,c. Now I want to put them in a variable as a_b_c.
How can I achieve this?
Reply With Quote
  #2 (permalink)  
Old January 9th, 2009, 11:16 AM
Friend of Wrox
Points: 6,663, Level: 34
Points: 6,663, Level: 34 Points: 6,663, Level: 34 Points: 6,663, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,239
Thanks: 0
Thanked 244 Times in 243 Posts
Default

xsl:for-each is not a loop.
If you want to concatenate the values selected in an xsl:for-each then simply do e.g.
Code:
<xsl:variable name="v">
<xsl:for-each select="foo">
  <xsl:value-of select="."/>
</xsl:for-each>
</xsl:variable>
Of course you don't need a variable if you simply want to add the values to the result tree, in that case doing e.g.
Code:
<xsl:for-each select="foo">
  <xsl:value-of select="."/>
</xsl:for-each>
suffices.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
Reply With Quote
  #3 (permalink)  
Old January 9th, 2009, 11:28 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

XSLT cannot loop, for-each processes each node found by the select attribute. It's also a functional language so what you want can't be done ina non-functional way such as repeated adding to a variable. The common way to carry out the task you require, especially using XSLT version 1.0, is to use recursion. Pass the nodes to a template that processes the first, and passes the rest on to itself. When there are no more nodes it returns.

If you're using version 2.0 you can probably use the xls:value-of element with the separator attribute set to '_' or the XPath string-join() function.
__________________
Joe
http://joe.fawcett.name/
Reply With Quote
  #4 (permalink)  
Old January 9th, 2009, 11:31 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

Martin's way is simpler, for version 1.0. I seem to have recursion on the brain at the moment
__________________
Joe
http://joe.fawcett.name/
Reply With Quote
Reply


Thread Tools
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
XSLT Loop? kwilliams XSLT 1 June 16th, 2008 07:04 PM
xslt when loop problem check XSLT 1 April 18th, 2008 09:47 AM
Need to concatenate values in a loop LeoMathew XSLT 6 February 14th, 2008 09:03 AM
FOR LOOP IN XSLT im_himanshu2004 XSLT 0 October 10th, 2007 03:27 PM
loop in xslt vazarshahy XSLT 6 June 7th, 2006 05:46 PM



All times are GMT -4. The time now is 12:36 PM.


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