2015-08-22 2 views
0

У меня есть XML, как показано ниже, и вы хотите удалить из запроса SOAP-конверт и тело и сгенерировать, как показано ниже.Удалить xmlns из XML с помощью XSLT

Текущий 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 строки: мыло = "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> 

или добавить exclude-result-prefixes="soap" на xsl:stylesheet. См. http://xsltransform.net/nc4NzRo.

+0

Просто крошечное примечание: '@ copy-namespaces' все равно копируют пространства имен, если узел, который нужно скопировать, является узлом элемента с именем в пространстве имен. Он просто удаляет неиспользуемые пространства имен, обязательные пространства имен, конечно, сохраняются как часть [процесса исправления пространства имен] (http://www.w3.org/TR/xslt20/#namespace-fixup). – Abel

Смежные вопросы