首页 > 解决方案 > 将xml转换为没有soapEnvolpe和命名空间的xml,还使用xslt将命名空间添加到内部元素

问题描述

我一直致力于将 xml 转换为没有soapEnvolope 和soapBody 的新xml。此外,我正在尝试删除现有的多个命名空间并将其替换为单个(和新的)命名空间。

请在下面找到我要转换的 xml,

<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>

请找到我想出的xslt,但它不完整。我想知道我想念什么。我正在尝试实施 xslt 1.0 解决方案。

<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>

预期的结果是

<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>

有人可以帮助我了解我想念的东西吗?我生成的 xslt 删除了所有命名空间,我无法在它之后添加任何命名空间。

标签: xmlxslt

解决方案


对于根节点上的默认命名空间,只需向模板添加一个namespace参数:*

<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

对于iata根节点上的前缀命名空间,添加一个新模板来重写根:

<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


推荐阅读