xml - 将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 删除了所有命名空间,我无法在它之后添加任何命名空间。
解决方案
对于根节点上的默认命名空间,只需向模板添加一个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>
对于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>
推荐阅读
- mysql - mysql outfile 有多少记录然后计数
- indexing - 按过程 ORA-01450 重建所有索引时出错:最大密钥长度
- vhdl - 测试台模拟错误:预期长度为 32;实际长度为4
- c - 如何从输入中获取数字并将其打印出来?
- python - JSON在python中转换为字符串
- ios - UMPConsentStatusObtained 对用户消息传递平台 SDK 的同意意味着什么
- javascript - Puppeteer 是否让我的 NodeJS 进程退出?
- javascript - Redux hook useSelector 没有给出正确的状态
- race-condition - 一次运行一条指令的程序中是否可能出现竞争条件?
- c# - C# 检查字符串数组中是否包含任何字符串并获取值