1

I am trying to eliminate all the child nodes and copy all the data to the parent node but the output remains the same as the input.

Input XML -

<?xml version="1.0" encoding="ISO-8859-1"?>
<PersonData>    
    <Header>
    </Header>
    <Person>
      <Personal>
         <FirstName>abc</FirstName>
         <LastName>cde</LastName>
         <ID>12345</ID>
      </Personal>
      <AddressData>
         <Address1>abc123</Address1>
         <Address2>def345</Address2>
      </AddressData>
      <PhoneData>
         <Phone1>111111111</Phone1>
      </PhoneData>
    </Person>
 </PersonData>

I have already tried the below code but the output remains the same as input thus not removing child nodes and the data as well remaining within them not moving to parent node i.e. Person.

   <?xml version='1.0'?>
   <xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

   <xsl:output method="xml" indent="yes"/>
   <xsl:strip-space elements="*" />

   <xsl:template match="@*|node()">
     <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
     </xsl:copy>
   </xsl:template>

   <xsl:template match="Person">
   <xsl:copy>
       <xsl:apply-templates select=""@*|node()[not(self::Person)]""/>
   </xsl:copy>
   <xsl:apply-templates select="Person" />
   </xsl:template>

   </xsl:stylesheet>    

Desired Output -

   <?xml version="1.0" encoding="ISO-8859-1"?>
   <PersonData>    
   <Person>
     <FirstName>abc</FirstName>
     <LastName>cde</LastName>
     <ID>12345</ID>
     <Address1>abc123</Address1>
     <Address2>def345</Address2>
     <Phone1>111111111</Phone1>
    </Person>
    </PersonData>

I get the same output as input XML and not the above expected output without child nodes

kjhughes
  • 106,133
  • 27
  • 181
  • 240
User2300
  • 35
  • 3

1 Answers1

1

How about:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/PersonData">
    <xsl:copy>
        <xsl:apply-templates select="Person"/>
     </xsl:copy>
</xsl:template>

<xsl:template match="Person">
    <xsl:copy>
        <xsl:copy-of select="*/*"/>
     </xsl:copy>
</xsl:template>

</xsl:stylesheet>
michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
  • Hey, Thanks Michael. In my actual XML, there are namespaces like ab:PersonData, ab:Person and in every child node. Does the above code change drastically as i included name space in your xslt but it just results in only data without any nodes. – User2300 Jul 17 '19 at 14:48
  • I am afraid I cannot comment on things I cannot see. See if this helps:https://stackoverflow.com/questions/34758492/xslt-transform-doesnt-work-until-i-remove-root-node/34762628#34762628 – michael.hor257k Jul 17 '19 at 15:13
  • Thanks Michael. There was a minor issue in namespace which was corrected and everything working fine. – User2300 Jul 17 '19 at 18:38
  • In this same XSLT, can we remove the namespace as well along with this code. I am trying to achieve getting the namespace removed along with this code. – User2300 Jul 17 '19 at 20:24
  • I am afraid it's not that simple. I suggest you post a new question. – michael.hor257k Jul 18 '19 at 00:19