RSS Forums RSS

Adding New Element using XSLT

Please support our XML, XSLT and XPATH advertiser: Programming Forums
Reply
Posts: 3
Reputation: learningxslt is an unknown quantity at this point 
Solved Threads: 0
learningxslt learningxslt is offline Offline
Newbie Poster

Adding New Element using XSLT

  #1  
Jan 12th, 2009
Hi All,
I have a set of elements to be added to an existing XML document. I would like to write a XSLT tranformation for this. My requirement is that I need to check whether the element is already there in the XML file. Only if its non existent, I need to add that element. I have to repeat this step for a set of elements. Also, my XML file is namespace based.

Please advise.

Regards
Ramesh
AddThis Social Bookmark Button
Reply With Quote  
Posts: 78
Reputation: fpmurphy is an unknown quantity at this point 
Solved Threads: 4
fpmurphy fpmurphy is offline Offline
Junior Poster in Training

Re: Adding New Element using XSLT

  #2  
Jan 12th, 2009
Here is a simple example of adding an element. Suppose you have the folllowing XML document
<root>
   <list>
      <a>aaaa</a>
      <b>bbbb</b>
   </list>
</root>
and you want to add an extra element <c>. The following stylesheet is one way of adding this element.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" >
  <xsl:param name="ename">c</xsl:param>
  <xsl:param name="evalue">cccc</xsl:param>

  <xsl:output method="xml" encoding="utf-8"/>

  <xsl:template match="@*|node()">
     <xsl:copy>
       <xsl:apply-templates select="@*|node()" />
     </xsl:copy>
  </xsl:template>

  <xsl:template match="list">
     <xsl:copy>
         <xsl:apply-templates/>
          <xsl:if test="not(c)">
             <xsl:element name="{$ename}"><xsl:value-of select="$evalue"/></xsl:element>
          </xsl:if>
     </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
The output is
<root>
      <list>
      <a>aaaa</a>
      <b>bbbb</b>
   <c>cccc</c></list>
</root>
Reply With Quote  
Posts: 3
Reputation: learningxslt is an unknown quantity at this point 
Solved Threads: 0
learningxslt learningxslt is offline Offline
Newbie Poster

Re: Adding New Element using XSLT

  #3  
Jan 15th, 2009
Thanks for the help. I was trying the same thing with my examples. But it looks like only the identity transformation template is only invoked. The other template is not invoked. There is a namespace associated with the XML file. ( The schema that defines the XML has the targetnamespace). Is this why the other templates are not invoked? How to handle namespace based insertion.

Thanks
Reply With Quote  
Posts: 78
Reputation: fpmurphy is an unknown quantity at this point 
Solved Threads: 4
fpmurphy fpmurphy is offline Offline
Junior Poster in Training

Re: Adding New Element using XSLT

  #4  
Jan 16th, 2009
Yes, you have to specifically handle the extra namespace issues.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Other Threads in the XML, XSLT and XPATH Forum
Views: 1141 | Replies: 3 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 10:25 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC