2015-08-22 107 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