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

January 9th, 2009, 11:08 AM
|
Registered User
|
|
Join Date: Sep 2008
Location: , , India.
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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?
|

January 9th, 2009, 11:16 AM
|
Friend of Wrox
|
|
Join Date: Nov 2007
Location: Germany
Posts: 1,242
Thanks: 0
Thanked 244 Times in 243 Posts
|
|
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
|

January 9th, 2009, 11:28 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
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.
|

January 9th, 2009, 11:31 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
Martin's way is simpler, for version 1.0. I seem to have recursion on the brain at the moment 
|
Thread Tools |
|
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
|
|
|
|
 |