2015-08-22 194 views
0

我有一個XML,如下所示,希望從請求中刪除SOAP信封和正文,並生成如下。使用XSLT從XML中刪除xmlns

當前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

+0

只是一個小小的提示:如果要複製的節點是名稱空間中名稱的元素節點,「@ copy-namespaces」仍將複製名稱空間。它只是刪除未使用的命名空間,當然,強制命名空間作爲[命名空間修復過程](http://www.w3.org/TR/xslt20/#namespace-fixup)的一部分被保留。 – Abel