I have below XML
<?xml version="1.0" encoding="UTF-8"?>
<Employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<FirstName>John</FirstName>
<LastName>Peter</LastName>
<Initial>T</Initial>
</Employee>
In XSLT 1.0 I want to write an XSLT to produce below XML from the xml above. can anyone help to me write this xslt?
<?xml version="1.0" encoding="UTF-8"?>
<ArrayOfstringVariable xmlns="http://schemas.abc.org/2004/07/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<stringVariable>
<name>FirstName</name>
<value>John</value>
</stringVariable>
<stringVariable>
<name>LastName</name>
<value>Peter</value>
</stringVariable>
<stringVariable>
<name>Initial</name>
<value>T</value>
</stringVariable>
</ArrayOfstringVariable>
Following XSLT
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" omit-xml-declaration="no"
encoding="UTF-8" indent="yes" />
<xsl:template match="Employee">
<ArrayOfstringVariable>
<xsl:apply-templates select="*"/>
</ArrayOfstringVariable>
</xsl:template>
<xsl:template match="*">
<stringVariable>
<name>
<xsl:value-of select="local-name()"/>
</name>
<value>
<xsl:value-of select="."/>
</value>
</stringVariable>
</xsl:template>
</xsl:stylesheet>
when applied to the example input XML from your question produces the following output:
<?xml version="1.0" encoding="UTF-8"?>
<ArrayOfstringVariable>
<stringVariable>
<name>FirstName</name>
<value>John</value>
</stringVariable>
<stringVariable>
<name>LastName</name>
<value>Peter</value>
</stringVariable>
<stringVariable>
<name>Initial</name>
<value>T</value>
</stringVariable>
</ArrayOfstringVariable>
In case you want to have the namespace in the output XML at the ArrayOfStringVariable
element, this can be done with two adjustments: add xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
to the xsl:stylesheet
declaration and adjust <ArrayOfstringVariable
to <ArrayOfstringVariable xmlns="http://schemas.abc.org/2004/07/" >
at the <xsl:template match="Employee">
and also adjust <stringVariable>
to <stringVariable xmlns="http://schemas.abc.org/2004/07/">
at the <xsl:template match="*">
.