943,815 Members | Top Members by Rank

Ad:
Jan 12th, 2009
0

Adding New Element using XSLT

Expand Post »
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
learningxslt is offline Offline
3 posts
since Jan 2009
Jan 12th, 2009
0

Re: Adding New Element using XSLT

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>
Reputation Points: 22
Solved Threads: 11
Junior Poster
fpmurphy is offline Offline
144 posts
since Oct 2008
Jan 15th, 2009
0

Re: Adding New Element using XSLT

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
learningxslt is offline Offline
3 posts
since Jan 2009
Jan 16th, 2009
0

Re: Adding New Element using XSLT

Yes, you have to specifically handle the extra namespace issues.
Reputation Points: 22
Solved Threads: 11
Junior Poster
fpmurphy is offline Offline
144 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in XML, XSLT and XPATH Forum Timeline: Refresh XML/XSL fails because XML page being updated
Next Thread in XML, XSLT and XPATH Forum Timeline: Template is not invoked.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC