Search code examples
xslt-2.0

XSLT on XML Get parent Value from a mixed Node


I have an XML with some mixed Nodes,and I want to get just the value of the parent and not the child.

My XML

<?xml version="1.0" encoding="UTF-8"?>
<Records>
   <DET>
      <detnumber>100126</detnumber>
      <EmployeeNo>100126</EmployeeNo>
      <action>CHANGE</action>
      <first_name> NewHire-4th
                        <previous>NewHire</previous>
                     </first_name>
      <last_name>Test-Changed 4th
                        <previous>Test-Changed 3rd</previous>
                     </last_name>
      <birth_name>
                        NewHire-Changed 4th
                        <previous>NewHire-Changed 3rd</previous>
                     </birth_name>
      <formal_name>
                        NewHire-4th Test-Changed 4th
                        <previous>NewHire Test-Changed 3rd</previous>
                     </formal_name>
      <salutation>
                        MISS
                        <previous>MRS</previous>
                     </salutation>
      <email_address>
                        [email protected]
                        <previous>[email protected]</previous>
                     </email_address>
   </DET>
</Records>

Using XSLT 2.0 ,

I am mostly using copy of in my xslt, But the whole Node and its child are being copied. I need to be able to restrict only to the parent.

<xsl:copy-of select="first_name"/>
<xsl:copy-of select="last_name"/>
<xsl:copy-of select="birth_name"/>
<xsl:copy-of select="formal_name"/>
<xsl:copy-of select="salutation"/>

Below is my preferred output

<?xml version="1.0" encoding="UTF-8"?>
<Records>
   <DET>
      <detnumber>100126</detnumber>
      <EmployeeNo>100126</EmployeeNo>
      <action>CHANGE</action>
      <first_name> NewHire-4th</first_name>
      <last_name>Test-Changed 4th</last_name>
      <birth_name>NewHire-Changed 4th</birth_name>
      <formal_name>NewHire-4th Test-Changed 4th</formal_name>
      <salutation>MISS</salutation>
      <email_address>[email protected]</email_address>
   </DET>
</Records>

Solution

  • Check this Code:-
    
    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="yes"/>
    
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    
        <xsl:template match="previous"/>
    </xsl:stylesheet>