| | |
Adding New Element using XSLT
Please support our XML, XSLT and XPATH advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2009
Posts: 3
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Oct 2008
Posts: 95
Reputation:
Solved Threads: 5
Here is a simple example of adding an element. Suppose you have the folllowing XML document
and you want to add an extra element <c>. The following stylesheet is one way of adding this element.
The output is
XML, XSLT and XPATH Syntax (Toggle Plain Text)
<root> <list> <a>aaaa</a> <b>bbbb</b> </list> </root>
XML, XSLT and XPATH Syntax (Toggle Plain Text)
<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>
XML, XSLT and XPATH Syntax (Toggle Plain Text)
<root> <list> <a>aaaa</a> <b>bbbb</b> <c>cccc</c></list> </root>
•
•
Join Date: Jan 2009
Posts: 3
Reputation:
Solved Threads: 0
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
Thanks
![]() |
Other Threads in the XML, XSLT and XPATH Forum
- Previous Thread: Refresh XML/XSL fails because XML page being updated
- Next Thread: Template is not invoked.
| Thread Tools | Search this Thread |
api blogger blogging code delete development dynamiccreationofnvariablesinxslt error firstthreecharacterofastringrequired flipbook gdata google html include java link linspire linux microsoft news node openoffice overwrite precedence programming rss standards swf template transform variable w3c web xml xmlnotloading xmlonserver xsl xslt





