0
votes

I am using xslt 1.0 My input xml is as below

<?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>
<Spouse>
<FirstName>Rita</FirstName>
<LastName>Hudson</LastName>
</Spouse>  
</Employee>

I an trying to write a xsl to produce below output...

<?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>ServerName</name>
<value>tmn.eu.com</value>
</stringVariable>
<stringVariable>
<name>EmpFirstName</name>
<value>John</value>
</stringVariable>
<stringVariable>
<name>EmpLastName</name>
<value>Peter</value>
</stringVariable>
<stringVariable>
<name>SpouseFirstName</name>
<value>Rita</value>
</stringVariable>
<stringVariable>
<name>SpouseLastName</name>
<value>Hudson</value>
</stringVariable>
</ArrayOfstringVariable>

The output xml contains ArrayOfstringVariable stringVariable name value pair.. The name is hardcoded and the value is from input xml.. Name value "ServerName" is hardcoded.

I tried with xsl code below but it create name value pair with all the elements from input xml

<?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>

Can anyone help me to write xsl to produce above output?

Thanks in advance

1
You've got to try something first.. You'll get help correcting your mistakes.Lingamurthy CS
Hi Lingamurthy, I tried <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> but it copies each elements from source xmljaya k
It is not much readable to add your code as comment. You can edit your question with the same thoughLingamurthy CS
Hi Kingamurthy.. editing donejaya k

1 Answers

0
votes

The XSLT will be straight forward as most of your elements are hardcoded:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:ns="http://schemas.abc.org/2004/07/">
<xsl:output method="xml" omit-xml-declaration="no" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*"/>
<xsl:template match="Employee">
    <ns:ArrayOfstringVariable>
        <ns:stringVariable>
            <ns:name>ServerName</ns:name>
            <ns:value>tmn.eu.com</ns:value>
        </ns:stringVariable>
        <ns:stringVariable>
            <ns:name>EmpFirstName</ns:name>
            <ns:value>
                <xsl:value-of select="FirstName"/>
            </ns:value>
        </ns:stringVariable>
        <ns:stringVariable>
            <ns:name>EmpLastName</ns:name>
            <ns:value>
                <xsl:value-of select="LastName"/>
            </ns:value>
        </ns:stringVariable>
        <ns:stringVariable>
            <ns:name>SpouseFirstName</ns:name>
            <ns:value>
                <xsl:value-of select="Spouse/FirstName"/>
            </ns:value>
        </ns:stringVariable>
        <ns:stringVariable>
            <ns:name>SpouseLastName</ns:name>
            <ns:value>
                <xsl:value-of select="Spouse/LastName"/>
            </ns:value>
        </ns:stringVariable>
    </ns:ArrayOfstringVariable>
</xsl:template>
</xsl:stylesheet>