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 23rd, 2009, 11:43 AM
Authorized User
 
Join Date: Sep 2009
Posts: 14
Thanks: 6
Thanked 0 Times in 0 Posts
Question Append element to existing set

I have the following input xml and want to insert a new <ADDON> container with new elements inside. The input xml may have one or many <ADDON> containers already, in which case I want to add another <ADDON> container some static data. Can you assist?


Input XML:
Code:
<ROOT>
<MANYOTHERELEMENTS>
<PARENTOFADDON>
<ADDON>
<Aelement>ExisingData</Aelement>
</ADDON>
</PARENTOFADDON>
</MANYOTHERELEMENTS>
</ROOT>
Desired output xml:
Code:
<ROOT>
<MANYOTHERELEMENTS>
<PARENTOFADDON>
<ADDON>
<Aelement>ExisingData</Aelement>
</ADDON>
<ADDON>
<Aelement>MyData</Aelement>
</ADDON>
</PARENTOFADDON>
</MANYOTHERELEMENTS>
</ROOT>
 
Old September 23rd, 2009, 12:05 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

You can simply use the identity transformation template
Code:
<xsl:template match="@* | node()">
  <xsl:copy>
     <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>
plus one template
Code:
<xsl:template match="PARENTOFADDON">
  <xsl:copy>
     <xsl:apply-templates select="@ | node()"/>
     <ADDON>
         <Aelement>MyData</Aelement>
     </ADDON>
   </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
Problem adding element to the previous element dani1 XSLT 5 September 10th, 2008 01:38 AM
Append XML1 as an element in XML2 pruthvi888 XML 3 March 7th, 2007 04:03 AM
Adding existing and non-existing attributes spencer.clark XSLT 5 July 27th, 2005 04:02 PM
adding of element and assigning to one element sushovandatta XSLT 2 November 16th, 2004 07:04 PM
javascript - add element to existing xml? willgotoo XML 4 October 8th, 2004 09:33 AM





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