使用 XSLT 从 XML 中删除 xmlns [英] Remove xmlns from XML using XSLT
问题描述
我有一个如下的 XML,并希望从请求中删除 SOAP 信封和正文并生成如下.
I have an XML as below and looking to remove the SOAP envelope and body from the request and generate as below.
当前的 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>
所需的 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>
我正在使用以下 XSLT
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>
但它没有删除 xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 行.它只去除肥皂:信封和肥皂:身体.任何人都可以帮我写一个 XSLT 来按照我的要求删除标签吗?
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?
推荐答案
使用 XSLT 2.0,您可以将身份转换编写为
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>
参见http://xsltransform.net/jyH9rNm.
或者,对于 XSLT 1.0,您需要使用
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>
确保像soap 命名空间这样的作用域命名空间不被复制.在样式表中,我会将 xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
移动到
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>
或在 xsl:stylesheet
上添加 exclude-result-prefixes="soap"
.请参阅http://xsltransform.net/nc4NzRo.
or add exclude-result-prefixes="soap"
on the xsl:stylesheet
. See http://xsltransform.net/nc4NzRo.
这篇关于使用 XSLT 从 XML 中删除 xmlns的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!