Remove xmlns from XML using XSLT

soapxml-namespacesxslt

I have an XML as below and looking to remove the SOAP envelope and body from the request and generate as below.

Current XML

<?xml version="1.0" encoding="utf-8"?>
 <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<AsnPODetailsRequest>
<AsnPODetails>
    <RequestSourcedFrom>EDI</RequestSourcedFrom>
    <ValidationLevelInd></ValidationLevelInd>
    <AdvanceShipmentNotificationId></AdvanceShipmentNotificationId>
    <PoAttributeList>
        <PoAttribute>
            <Department></Department>
            <FreightTerms></FreightTerms>
            <Location></Location>
            <LocationType></LocationType>
            <MFOrderNo>MOMILQ</MFOrderNo>
            <NotBeforeDate>2014-06-24 01:21:36</NotBeforeDate>
            <PurchaseOrderType></PurchaseOrderType>
            <RetekPurchaseOrderNo></RetekPurchaseOrderNo>
            <sku></sku>
            <Status></Status>
            <Supplier></Supplier>
            <SupplierDesc></SupplierDesc>
            <upc></upc>
            <VendorOrderNo></VendorOrderNo>
        </PoAttribute>
        <PoAttribute>
        <Department></Department>
        <FreightTerms></FreightTerms>
        <Location></Location>
        <LocationType></LocationType>
        <MFOrderNo>MOMILN</MFOrderNo>
        <NotBeforeDate>2014-06-24 01:21:36</NotBeforeDate>
        <PurchaseOrderType></PurchaseOrderType>
        <RetekPurchaseOrderNo></RetekPurchaseOrderNo>
        <sku></sku>
        <Status></Status>
        <Supplier></Supplier>
        <SupplierDesc></SupplierDesc>
        <upc></upc>
        <VendorOrderNo></VendorOrderNo>
        </PoAttribute>
    </PoAttributeList>
    </AsnPODetails>
   </AsnPODetailsRequest>
  </soap:Body>
 </soap:Envelope>

Desired XML:

<?xml version="1.0" encoding="utf-8"?>
<AsnPODetailsRequest>
<AsnPODetails>
    <RequestSourcedFrom>EDI</RequestSourcedFrom>
    <ValidationLevelInd></ValidationLevelInd>
    <AdvanceShipmentNotificationId></AdvanceShipmentNotificationId>
    <PoAttributeList>
        <PoAttribute>
            <Department></Department>
            <FreightTerms></FreightTerms>
            <Location></Location>
            <LocationType></LocationType>
            <MFOrderNo>MOMILQ</MFOrderNo>
            <NotBeforeDate>2014-06-24 01:21:36</NotBeforeDate>
            <PurchaseOrderType></PurchaseOrderType>
            <RetekPurchaseOrderNo></RetekPurchaseOrderNo>
            <sku></sku>
            <Status></Status>
            <Supplier></Supplier>
            <SupplierDesc></SupplierDesc>
            <upc></upc>
            <VendorOrderNo></VendorOrderNo>
        </PoAttribute>
        <PoAttribute>
        <Department></Department>
        <FreightTerms></FreightTerms>
        <Location></Location>
        <LocationType></LocationType>
        <MFOrderNo>MOMILN</MFOrderNo>
        <NotBeforeDate>2014-06-24 01:21:36</NotBeforeDate>
        <PurchaseOrderType></PurchaseOrderType>
        <RetekPurchaseOrderNo></RetekPurchaseOrderNo>
        <sku></sku>
        <Status></Status>
        <Supplier></Supplier>
        <SupplierDesc></SupplierDesc>
        <upc></upc>
        <VendorOrderNo></VendorOrderNo>
        </PoAttribute>
    </PoAttributeList>
    </AsnPODetails>
  </AsnPODetailsRequest>

I am using the below XSLT

<xsl:stylesheet version="1.0" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output encoding="UTF-8" indent="yes" method="xml" version="1.0"/>
  <xsl:strip-space elements="*"/>

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

<xsl:template match="soap:Envelope | soap:Body">
  <xsl:apply-templates select="@*|node()"/>
</xsl:template>
</xsl:stylesheet>

but it is not removing the line xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/". it is removing only soap:Envelope and soap:Body. can any please help me to write an XSLT to remove tags as i requested?

Best Answer

Using XSLT 2.0 you could write your identity transformation as

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

see http://xsltransform.net/jyH9rNm.

Or, with XSLT 1.0, you need to use

<xsl:template match="*">
  <xsl:element name="{name()}" namespace="{namespace-uri()}">
    <xsl:apply-templates select="@*|node()"/>
  </xsl:element>
</xsl:template>

to make sure in scope namespaces like the soap namespaces are not copied through. And in the stylesheet I would move xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" to

<xsl:template xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" match="soap:Envelope | soap:Body">
  <xsl:apply-templates select="@*|node()"/>
</xsl:template>

or add exclude-result-prefixes="soap" on the xsl:stylesheet. See http://xsltransform.net/nc4NzRo.

Related Topic