Adding New Element using XSLT

Please support our XML, XSLT and XPATH advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2009
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

 
0
  #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
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 101
Reputation: fpmurphy is an unknown quantity at this point 
Solved Threads: 5
fpmurphy fpmurphy is offline Offline
Junior Poster

Re: Adding New Element using XSLT

 
0
  #2
Jan 12th, 2009
Here is a simple example of adding an element. Suppose you have the folllowing XML document
XML, XSLT and XPATH Syntax (Toggle Plain Text)
  1. <root>
  2. <list>
  3. <a>aaaa</a>
  4. <b>bbbb</b>
  5. </list>
  6. </root>
and you want to add an extra element <c>. The following stylesheet is one way of adding this element.
XML, XSLT and XPATH Syntax (Toggle Plain Text)
  1. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" >
  2. <xsl:param name="ename">c</xsl:param>
  3. <xsl:param name="evalue">cccc</xsl:param>
  4.  
  5. <xsl:output method="xml" encoding="utf-8"/>
  6.  
  7. <xsl:template match="@*|node()">
  8. <xsl:copy>
  9. <xsl:apply-templates select="@*|node()" />
  10. </xsl:copy>
  11. </xsl:template>
  12.  
  13. <xsl:template match="list">
  14. <xsl:copy>
  15. <xsl:apply-templates/>
  16. <xsl:if test="not(c)">
  17. <xsl:element name="{$ename}"><xsl:value-of select="$evalue"/></xsl:element>
  18. </xsl:if>
  19. </xsl:copy>
  20. </xsl:template>
  21.  
  22. </xsl:stylesheet>
The output is
XML, XSLT and XPATH Syntax (Toggle Plain Text)
  1. <root>
  2. <list>
  3. <a>aaaa</a>
  4. <b>bbbb</b>
  5. <c>cccc</c></list>
  6. </root>
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
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

 
0
  #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 Quick reply to this message  
Join Date: Oct 2008
Posts: 101
Reputation: fpmurphy is an unknown quantity at this point 
Solved Threads: 5
fpmurphy fpmurphy is offline Offline
Junior Poster

Re: Adding New Element using XSLT

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

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the XML, XSLT and XPATH Forum


Views: 1676 | Replies: 3
Thread Tools Search this Thread



Tag cloud for XML, XSLT and XPATH
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC