使用 XSLT 从 XML 中删除 xmlns
Posted
技术标签:
【中文标题】使用 XSLT 从 XML 中删除 xmlns【英文标题】:Remove xmlns from XML using XSLT 【发布时间】:2015-11-16 10:32:12 【问题描述】:我有一个如下所示的 XML,并希望从请求中删除 SOAP 信封和正文并生成如下。
当前 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
<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 来按我的要求删除标签吗?
【问题讨论】:
【参考方案1】:使用 XSLT 2.0,您可以将身份转换编写为
<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,您需要使用
<xsl:template match="*">
<xsl:element name="name()" namespace="namespace-uri()">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
以确保在范围内的名称空间(如肥皂名称空间)不会被复制。在样式表中,我会将xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
移动到
<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。
【讨论】:
请注意:如果要复制的节点是名称空间中的元素节点,@copy-namespaces
仍将复制名称空间。它只是删除未使用的命名空间,当然,强制命名空间作为the namespace fixup process 的一部分保留。以上是关于使用 XSLT 从 XML 中删除 xmlns的主要内容,如果未能解决你的问题,请参考以下文章