Wednesday, May 9, 2012

Append Nodes to a an existing XML via XSL

Many a times we have a need to manipulate an xml to append similar repeating nodes to it. This post addresses that need through a very simply written xsl.
For the context what I have here is an Employees xml schema as below -
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/Employees" xmlns:tns="http://www.example.org/Employees"  elementFormDefault="qualified">

<element name="Employees">
<complexType>
<sequence>
<element name="Employee" type="tns:empType" minOccurs="1" maxOccurs="unbounded" />
</sequence>
</complexType>
</element>
<complexType name="empType">
<sequence>
<element name="Name" type="string" />
<element name="ID" type="string" />
<element name="Designation" type="string" />
<element name="Dept" type="string" />
</sequence>
</complexType>
</schema>


I have an xml corresponding to the above schema as shown below -

<?xml version="1.0" encoding="UTF-8"?>
<tns:Employees xmlns:tns="http://www.example.org/Employees"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<tns:Employee>
<tns:Name>Jack</tns:Name>
<tns:ID>1</tns:ID>
<tns:Designation>Developer</tns:Designation>
<tns:Dept>Java</tns:Dept>
</tns:Employee>
<tns:Employee>
<tns:Name>Bill</tns:Name>
<tns:ID>2</tns:ID>
<tns:Designation>Architect</tns:Designation>
<tns:Dept>SOA</tns:Dept>
</tns:Employee>
</tns:Employees>

All that I would be doing is copying the entire xml in its entirety and then appending an extra tag to it after the last one. The xsl for achieving this is as below -
<?xml version="1.0" encoding="UTF-8"?>


<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:tns="http://www.example.org/Employees"
exclude-result-prefixes="#default tns">

<xsl:template match="/">
<tns:Employees>
<xsl:for-each select="/tns:Employees/tns:Employee">
<xsl:copy-of select="." />
<xsl:if test="position()=last()">
<xsl:call-template name="append" /> </xsl:if>
</xsl:for-each>
</tns:Employees>
</xsl:template>
<xsl:template name="append">
<tns:Employee>
<tns:Name>Demo-Name</tns:Name>
<tns:ID>3</tns:ID>
<tns:Designation>Demo-Designation</tns:Designation>
<tns:Dept>Demo-Dept</tns:Dept>
</tns:Employee>
</xsl:template>
</xsl:stylesheet>


The template named 'append' contains the new <Employee> tag that needs to be appended.
So the output xml obtained after executing the above xsl with the Employees.xml as input is as below -

<?xml version="1.0" encoding="UTF-8"?>
<tns:Employees xmlns:tns="http://www.example.org/Employees">
<tns:Employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<tns:Name>Jack</tns:Name>
<tns:ID>1</tns:ID>
<tns:Designation>Developer</tns:Designation>
<tns:Dept>Java</tns:Dept>
</tns:Employee>
<tns:Employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<tns:Name>Bill</tns:Name>
<tns:ID>2</tns:ID>
<tns:Designation>Architect</tns:Designation>
<tns:Dept>SOA</tns:Dept>
</tns:Employee>
<tns:Employee>
<tns:Name>Demo-Name</tns:Name>
<tns:ID>3</tns:ID>
<tns:Designation>Demo-Designation</tns:Designation>
<tns:Dept>Demo-Dept</tns:Dept>
</tns:Employee>
</tns:Employees>
You see the new <Employee> appended right after the last <Employee> tag of the original xml.Hope it helps! 


Do let me know if you face problems with xml manipulation using xsl. I'll be happy to help :)



















































No comments:

Post a Comment