Search code examples
xsltxslt-1.0

XSLT mapping to remove header segment


I am trying to use XSLT 1.0 to remove header segment from my input xml, not able to get the desired output. Please help me here.

Input

<?xml version="1.0" encoding="UTF-8"?>
<HEADER>
    <CHILD1 BEGIN="1">
        <EDI SEGMENT="1">
            <FIELD1>EDI</FIELD1>
            <FIELD2>2</FIELD2>
            <FIELD3>HEADER</FIELD3>
        </EDI>      
    </CHILD1>
</HEADER>

output (No XML declaration and HEADER segment)


    <CHILD1 BEGIN="1">
        <EDI SEGMENT="1">
            <FIELD1>EDI</FIELD1>
            <FIELD2>2</FIELD2>
            <FIELD3>HEADER</FIELD3>
        </EDI>      
    </CHILD1>

code i tried

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:template match="HEADER">
              <CHILD BEGIN="1">
            <xsl:copy-of select="."/>
            
            </CHILD>
    </xsl:template>
</xsl:stylesheet>

Solution

  • Use

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        version="1.0">
        <xsl:template match="/HEADER">
            <xsl:copy-of select="*"/>
        </xsl:template>
    </xsl:stylesheet>
    

    As for not having an XML declaration in the serialized result of the transformation, you can declare <xsl:output method="xml" omit-xml-declaration="yes"/>.