Transform xml into one without soapEnvolpe and namespaces, also add namespace to inner element using xslt

Viewed 76

I have been working on transforming an xml to a new one with no soapEnvolope and soapBody. Also, I am trying to delete the existing multiple namespaces and get it replaced with a single (and new) one.

Please find the xml I am trying to convert below,

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <ns11:IATA_InvGuaranteeRS xmlns:ns2="http://www.iata.org/IATA/2015/00/2019.2/IATA_InvGuaranteeRS" xmlns:ns3="http://www.iata.org/IATA/2015/00/2019.2/IATA_OrderChangeRQ" xmlns:ns4="http://www.iata.org/IATA/2015/00/2019.2/IATA_OrderViewRS" xmlns:ns5="http://www.iata.org/IATA/2015/00/2019.2/IATA_OrderRetrieveRQ" xmlns:ns6="http://www.iata.org/IATA/2015/00/2019.2/IATA_InvReleaseNotifRQ" xmlns:ns7="http://www.iata.org/IATA/2015/00/2019.2/IATA_Acknowledgement" xmlns:ns8="http://www.iata.org/IATA/2015/00/2019.2/IATA_OfferPriceRQ" xmlns:ns9="http://www.iata.org/IATA/2015/00/2019.2/IATA_OfferPriceRS" xmlns:ns10="http://www.iata.org/IATA/2015/00/2019.2/IATA_AirShoppingRQ" xmlns:ns11="http://www.iata.org/IATA/2015/00/2019.2/IATA_AirShoppingRS" xmlns:ns12="http://www.iata.org/IATA/2015/00/2019.2/IATA_OrderCreateRQ" xmlns:ns13="http://www.iata.org/IATA/2015/00/2019.2/IATA_OrderReshopRQ" xmlns:ns14="http://www.iata.org/IATA/2015/00/2019.2/IATA_OrderReshopRS" xmlns:ns15="http://www.iata.org/IATA/2015/00/2019.2/IATA_SeatAvailabilityRQ" xmlns:ns16="http://www.iata.org/IATA/2015/00/2019.2/IATA_SeatAvailabilityRS" xmlns:ns17="http://www.iata.org/IATA/2015/00/2019.2/IATA_OrderCancelRQ" xmlns:ns18="http://www.iata.org/IATA/2015/00/2019.2/IATA_OrderCancelRS" xmlns:ns19="http://www.iata.org/IATA/2015/00/2019.2/IATA_OrderRulesRQ" xmlns:ns20="http://www.iata.org/IATA/2015/00/2019.2/IATA_OrderRulesRS" xmlns:ns21="http://www.iata.org/IATA/2015/00/2019.2/IATA_OrderListRQ" xmlns:ns22="http://www.iata.org/IATA/2015/00/2019.2/IATA_OrderListRS" xmlns:ns23="http://www.iata.org/IATA/2015/00/2019.2/IATA_ServiceListRQ" xmlns:ns24="http://www.iata.org/IATA/2015/00/2019.2/IATA_ServiceListRS" xmlns:ns25="http://www.iata.org/IATA/2015/00/2019.2/IATA_InvGuaranteeRQ" xmlns:ns26="http://www.travel.com/wsdl/ndc">
            <ns11:Response>
                <ns11:DataLists>
                    <ns11:OriginDestList>
                        <ns11:OriginDest>
                            <ns11:DestCode>AYT</ns11:DestCode>
                            <ns11:Origin Code="FRA">FRA</ns11:Origin>
                            <ns11:OriginDestID>V1_OD.1595044928630</ns11:OriginDestID>
                            <ns11:PaxJourneyRefID>V1_FL.1595044929364</ns11:PaxJourneyRefID>
                        </ns11:OriginDest>
                    </ns11:OriginDestList>
                </ns11:DataLists>
            </ns11:Response>
        </ns11:IATA_InvGuaranteeRS>
    </soap:Body>
</soap:Envelope>

Please find the xslt I came up with, but it is incomplete. I would like to know what I miss. I am trying to implement a xslt 1.0 solution.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:iata="http://www.iata.org/IATA/2015/00/2019.2/IATA_InvGuaranteeRS"
                xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" version="2.0">
    <xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <!-- remove all elements in the soapenv namespace -->
    <xsl:template match="soapenv:*">
        <xsl:apply-templates select="node()"/>
    </xsl:template>
    <xsl:template match="*">
        <xsl:element name="{local-name()}">
            <xsl:apply-templates select="@* | node()" />
        </xsl:element>
    </xsl:template>
    <xsl:template match="@*">
        <xsl:attribute name="{local-name()}">
            <xsl:value-of select="." />
        </xsl:attribute>
    </xsl:template>
    <xsl:template match="text() | comment() | processing-instruction()">
        <xsl:copy />
    </xsl:template>
</xsl:stylesheet>

The expected result is

<IATA_InvGuaranteeRS xmlns:iata="http://www.iata.org/IATA/2015/00/2019.2/IATA_InvGuaranteeRS">
    <Response>
        <DataLists>
            <OriginDestList>
                <OriginDest>
                    <DestCode>AYT</DestCode>
                    <Origin Code="FRA">FRA</Origin>
                    <OriginDestID>V1_OD.1595044928630</OriginDestID>
                    <PaxJourneyRefID>V1_FL.1595044929364</PaxJourneyRefID>
                </OriginDest>
            </OriginDestList>
        </DataLists>
    </Response>
</IATA_InvGuaranteeRS>

Can someone help me understand what I miss? The xslt which I generated removes all namespaces, and I am not able to add any namespaces after it.

1 Answers

For default namespace on root node, simply add a namespace argument to the * template:

<xsl:template match="*">
    <xsl:element name="{local-name()}" namespace="http://www.iata.org/IATA/2015/00/2019.2/IATA_InvGuaranteeRS">
        <xsl:apply-templates select="@* | node()" />
    </xsl:element>
</xsl:template>

Online Demo

For prefixed namespace like iata on root node, add a new template to rewrite root:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"                    
                xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
                xmlns:ns11="http://www.iata.org/IATA/2015/00/2019.2/IATA_AirShoppingRS"
                exclude-result-prefixes="soapenv ns11"
                version="1.0">

    ...same as before...

    <xsl:template match="ns11:IATA_InvGuaranteeRS">
        <IATA_InvGuaranteeRS xmlns:iata="http://www.iata.org/IATA/2015/00/2019.2/IATA_InvGuaranteeRS">
            <xsl:apply-templates select="@* | node()" />
        </IATA_InvGuaranteeRS>
    </xsl:template>
</xsl:stylesheet>

Online Demo

Related