0

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>
Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
jaya k
  • 47
  • 1
  • 7

1 Answers1

2

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="*">.

Lingamurthy CS
  • 5,412
  • 2
  • 13
  • 21
matthias_h
  • 11,356
  • 9
  • 22
  • 40
  • Your first template and the `./` in `./*` are redundant. – JLRishe Nov 19 '14 at 21:28
  • @JLRishe Thanks for mentioning, already removed the 2nd redundancy and will add information about the first template to the answer instead of just removing it. Used to start from root though not necessary here. – matthias_h Nov 19 '14 at 21:36
  • Why do you match `node()` and not `*`? – Mathias Müller Nov 19 '14 at 22:31
  • @MathiasMüller Thanks for mentioning. Both works for the example provided, but `*` maybe better. Adjusted. In case someone notices the comments wondering about the difference, as reference http://stackoverflow.com/questions/12071402/difference-between-and-node-in-xslt#12073208 – matthias_h Nov 19 '14 at 22:44