使用 XSLT 从 XML 中删除 xmlns

Remove xmlns from XML using XSLT

我有一个如下所示的 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/"。它仅删除 soap:Envelope 和 soap:Body。能帮我写一个 XSLT 来按照我的要求删除标签吗?

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

确保范围内的名称空间(如 soap 名称空间)不会被复制。在样式表中,我会将 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