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 September 1st, 2010, 01:40 PM
Registered User
 
Join Date: Sep 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Merge nodes using xslt

Hi,

I have

<subject>maths</subject>
<subject>history</subject>
<subject>science</subject>

I want to convert it to

<subject>maths : history : science</subject>

any help is appreciated.

thanks
 
Old September 1st, 2010, 01:43 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

With XSLT 2.0:
Code:
<xsl:template match="put-name-of-parent-of-those-subject-elements-here">
  <subject><xsl:value-of select="subject" separator=" : "/></subject>
</xsl:template>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old September 1st, 2010, 01:49 PM
Registered User
 
Join Date: Sep 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for solution,

buy my xml is alightly differnt actually my xml looks like this

Code:
<subject>maths</subject>
<subject>history</subject>
<subject>science</subject>
<name>himanshu</name>
I want to convert it to

Code:
<subject>maths : history : science</subject>
<name>himanshu</name>

Last edited by himanshu; September 1st, 2010 at 01:50 PM.. Reason: code dint come properly
 
Old September 1st, 2010, 01:52 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Please show more context, those elements you have shown do have a parent element I assume, please show that.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old September 1st, 2010, 03:21 PM
Registered User
 
Join Date: Sep 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Martin Honnen View Post
Please show more context, those elements you have shown do have a parent element I assume, please show that.

Code:
<student>
<subject>maths</subject>
<subject>history</subject>
<subject>science</subject>
<name>himanshu</name>

<address>
	<city>newyork</city>
	<state>newyork</state>
</address>
<student>
 
Old September 2nd, 2010, 06:40 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

XSLT 2.0:
Code:
<xsl:template match="student">
  <xsl:copy>
     <subject><xsl:value-of select="subject" separator=" : "/></subject>
     <xsl:copy-of select="name"/>
  </xsl:copy>
</xsl:template>
XSLT 1.0:
Code:
<xsl:template match="student">
  <xsl:copy>
     <subject>
        <xsl:for-each select="subject">
           <xsl:value-of select="."/>
           <xsl:if test="position() != last()">
              <xsl:text> : </xsl:text>
           </xsl:if>
         </xsl:for-each>
     </subject>
     <xsl:copy-of select="name"/>
  </xsl:copy>
</xsl:template>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog





Similar Threads
Thread Thread Starter Forum Replies Last Post
[XSLT 2.0] Merge XML [email protected] XSLT 5 April 1st, 2014 12:25 PM
XML Merge Records using XSLT navik_pathak XSLT 10 June 22nd, 2009 06:13 PM
XSLT 1.0: Looping through nodes kwilliams XSLT 4 December 1st, 2008 06:21 PM
XSLT for Variable Child Nodes jlagedo XSLT 2 September 10th, 2006 03:00 AM
Removing nodes with an XSLT Jza XSLT 2 April 19th, 2006 10:06 AM





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