使用XSLT从XML中删除xmlns

时间:2015-08-22 15:46:14

标签: xslt soap xml-namespaces

我有一个XML,如下所示,希望从请求中删除SOAP信封和正文,并生成如下。

当前XML

true

所需的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>

我正在使用以下XSLT

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

但它不是删除行xmlns:soap =&#34; http://schemas.xmlsoap.org/soap/envelope/"。它只去除肥皂:信封和肥皂:身体。可以帮我写一个XSLT来删除我要求的标签吗?

1 个答案:

答案 0 :(得分:2)

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

或在exclude-result-prefixes="soap"上添加xsl:stylesheet。请参阅http://xsltransform.net/nc4NzRo