Here is the XML source that I need to transform using XSLT
<?xml version="1.0" encoding="UTF-8"?>
<tns:Grand_Parent_XML xmlns:tns="">
<GrandParent>
<Parent>
<Child>
<Age>3</Age>
</Child>
<Child>
<Gender>Male</Gender>
</Child>
<Child>
<Name>Todd</Name>
</Child>
<Other>1234</Other>
</Parent>
</GrandParent>
</tns:Grand_Parent_XML>
Here is the desired output after transforming through XSLT
<?xml version="1.0" encoding="UTF-8"?>
<tns:Grand_Parent_XML xmlns:tns="">
<GrandParent>
<Parent>
<Child>
<Age>3</Age>
<Gender>Male</Gender>
<Name>Todd</Name>
</Child>
<Other>1234</Other>
</Parent>
</GrandParent>
</tns:Grand_Parent_XML>
Here's what's actually happening...
<?xml version="1.0" encoding="UTF-8"?>
<tns:Grand_Parent_XML xmlns:tns="">
<GrandParent>
<Child>
<Age>3</Age>
<Gender>Male</Gender>
<Name>Todd</Name>
</Child>
</GrandParent>
</tns:Grand_Parent_XML>
And I'm using this XSLT...
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns="">
<xsl:template match="Grand_Parent_XML/GrandParent/Parent">
<Child>
<xsl:for-each select="Child">
<xsl:if test="Age !=''">
<Age><xsl:value-of select="Age"/></Age>
</xsl:if>
<xsl:if test="Gender !=''">
<Gender><xsl:value-of select="Gender"/></Gender>
</xsl:if>
<xsl:if test="Name !=''">
<Name><xsl:value-of select="Nanme"/></Name>
</xsl:if>
</xsl:for-each>
</Child>
</xsl:template>
</xsl:stylesheet>
I have little command of XSLT as of the moment and I would appreciate any help I can get. Using the XSLT I've created, the Parent is overridden by Child which shouldn't be the case. Also, the other child nodes of Parent i.e Other is removed. The actual XML I'm using has far more fields than the one I included here. I can choose to manually include all the nodes in the XSLT but I feel that there's a more efficient way to do it. Thank you!
Here is a generic approach that should work on a variety of different inputs:
When run on your sample input, the result is:
Try it this way:
Or, if you prefer: