2016-06-09 40 views
0

我有以下XML:複製輸入XML輸出並刪除不需要的命名空間出來的

<soap:Envelope 
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:ns="http:test/201/2" 
xmlns:m0="http:test/201/3" 
xmlns:ns0="http:test/201/4" 
xmlns:ns2="http:test/201/5" 
xmlns:ns1="http:test/201/6" 
xmlns:ns3="http:test/201/7" 
xmlns:ns6="http:test/201/8" 
xmlns:ns4="http:test/201/9" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<soap:Header> 
<ns0:ResponseHeader> 
<ns:Env>Dev</ns:Env> 
<ns:Version>1</ns:Version> 
<ns:Server></ns:Server> 
<ns:Name>NAME</ns:Name> 
</ns0:ResponseHeader> 
</soap:Header> 
<soap:Body> 
<ns2:ResponseData> 
<ns2:Employee > 
<ns2:MessageList xsi:type="ns3:Info"> 
<ns2:Message> 
<ns4:Type>new</ns4:Type> 
<ns4:Code>2</ns4:Code> 
<ns4:Source>Emp</ns4:Source> 
<ns4:Description>new hire</ns4:Description> 
</ns2:Message> 
</ns2:MessageList> 
<ns2:MessageList xsi:type="ns3:Info"> 
<ns2:Message> 
<ns4:Type>new</ns4:Type> 
<ns4:Code>1</ns4:Code> 
<ns4:Source>contract</ns4:Source> 
<ns4:Description>new hire</ns4:Description> 
</ns2:Message> 
</ns2:MessageList> 
</ns2:Employee> 
</ns2:ResponseData> 
</soap:Body> 
</soap:Envelope> 

我的要求是,拷貝完整的輸入XML元素和屬性來輸出XML包括除的xmlns所有命名空間:SOAP =」 http://schemas.xmlsoap.org/soap/envelope/」。 所以慾望輸出是:

我的要求是,複製完整的輸入xml元素和屬性以輸出xml包括除xmlns之外的所有命名空間:soap =「http://schemas.xmlsoap.org/soap/envelope/」。 願意的話輸出是:

<Envelope 
xmlns:ns="http:test/201/2" 
xmlns:m0="http:test/201/3" 
xmlns:ns0="http:test/201/4" 
xmlns:ns2="http:test/201/5" 
xmlns:ns1="http:test/201/6" 
xmlns:ns3="http:test/201/7" 
xmlns:ns6="http:test/201/8" 
xmlns:ns4="http:test/201/9" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<Header> 
<ns0:ResponseHeader> 
<ns:Env>Dev</ns:Env> 
<ns:Version>1</ns:Version> 
<ns:Server></ns:Server> 
<ns:Name>NAME</ns:Name> 
</ns0:ResponseHeader> 
</Header> 
<Body> 
<ns2:ResponseData> 
<ns2:Employee > 
<ns2:MessageList xsi:type="ns3:Info"> 
<ns2:Message> 
<ns4:Type>new</ns4:Type> 
<ns4:Code>2</ns4:Code> 
<ns4:Source>Emp</ns4:Source> 
<ns4:Description>new hire</ns4:Description> 
</ns2:Message> 
</ns2:MessageList> 
<ns2:MessageList xsi:type="ns3:Info"> 
<ns2:Message> 
<ns4:Type>new</ns4:Type> 
<ns4:Code>1</ns4:Code> 
<ns4:Source>contract</ns4:Source> 
<ns4:Description>new hire</ns4:Description> 
</ns2:Message> 
</ns2:MessageList> 
</ns2:Employee> 
</ns2:ResponseData> 
</Body> 
</Envelope> 
+0

你的輸出包含'肥皂:Envelope'所以它需要有一些'的xmlns:SOAP = 「...」'聲明。 –

+0

我的不好。肥皂前綴元素將被本地名稱替換。所以這就是爲什麼它不是必需的。所以最終的願望輸出是。 – Mike

+0

請編輯您的問題的代碼示例以反映所需的輸出。看來你也已經用一個可接受的解決方案發布了一個類似的問題,這個問題不同嗎?如果是這樣,爲什麼? –

回答

0

如果你想改變所有soap:foo元素foo元素沒有命名空間,並刪除soap命名空間,然後使用

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
    exclude-result-prefixes="xs soap" 
    version="2.0"> 

    <xsl:template match="@* | node()"> 
     <xsl:copy copy-namespaces="no"> 
      <xsl:apply-templates select="@* | node()"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="soap:*"> 
     <xsl:element name="{local-name()}"> 
      <xsl:copy-of select="namespace::*[not(. = namespace-uri(current()))]"/> 
      <xsl:apply-templates select="@* | node()"/> 
     </xsl:element> 
    </xsl:template> 

</xsl:stylesheet> 

應該足夠了,假設你的根元素是soap命名空間並在範圍內包含所有名稱空間聲明。

當我在帖子中應用上面XSLT將輸入樣本

<?xml version="1.0" encoding="UTF-8"?> 
<soap:Envelope 
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:ns="http:test/201/2" 
    xmlns:m0="http:test/201/3" 
    xmlns:ns0="http:test/201/4" 
    xmlns:ns2="http:test/201/5" 
    xmlns:ns1="http:test/201/6" 
    xmlns:ns3="http:test/201/7" 
    xmlns:ns6="http:test/201/8" 
    xmlns:ns4="http:test/201/9" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <soap:Header> 
     <ns0:ResponseHeader> 
      <ns:Env>Dev</ns:Env> 
      <ns:Version>1</ns:Version> 
      <ns:Server></ns:Server> 
      <ns:Name>NAME</ns:Name> 
     </ns0:ResponseHeader> 
    </soap:Header> 
    <soap:Body> 
     <ns2:ResponseData> 
      <ns2:Employee > 
       <ns2:MessageList xsi:type="ns3:Info"> 
        <ns2:Message> 
         <ns4:Type>new</ns4:Type> 
         <ns4:Code>2</ns4:Code> 
         <ns4:Source>Emp</ns4:Source> 
         <ns4:Description>new hire</ns4:Description> 
        </ns2:Message> 
       </ns2:MessageList> 
       <ns2:MessageList xsi:type="ns3:Info"> 
        <ns2:Message> 
         <ns4:Type>new</ns4:Type> 
         <ns4:Code>1</ns4:Code> 
         <ns4:Source>contract</ns4:Source> 
         <ns4:Description>new hire</ns4:Description> 
        </ns2:Message> 
       </ns2:MessageList> 
      </ns2:Employee> 
     </ns2:ResponseData> 
    </soap:Body> 
</soap:Envelope> 

然後撒克遜9.6創建結果

<?xml version="1.0" encoding="UTF-8"?><Envelope xmlns:ns="http:test/201/2" xmlns:m0="http:test/201/3" xmlns:ns0="http:test/201/4" xmlns:ns2="http:test/201/5" xmlns:ns1="http:test/201/6" xmlns:ns3="http:test/201/7" xmlns:ns6="http:test/201/8" xmlns:ns4="http:test/201/9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <Header> 
     <ns0:ResponseHeader> 
      <ns:Env>Dev</ns:Env> 
      <ns:Version>1</ns:Version> 
      <ns:Server/> 
      <ns:Name>NAME</ns:Name> 
     </ns0:ResponseHeader> 
    </Header> 
    <Body> 
     <ns2:ResponseData> 
      <ns2:Employee> 
       <ns2:MessageList xsi:type="ns3:Info"> 
        <ns2:Message> 
         <ns4:Type>new</ns4:Type> 
         <ns4:Code>2</ns4:Code> 
         <ns4:Source>Emp</ns4:Source> 
         <ns4:Description>new hire</ns4:Description> 
        </ns2:Message> 
       </ns2:MessageList> 
       <ns2:MessageList xsi:type="ns3:Info"> 
        <ns2:Message> 
         <ns4:Type>new</ns4:Type> 
         <ns4:Code>1</ns4:Code> 
         <ns4:Source>contract</ns4:Source> 
         <ns4:Description>new hire</ns4:Description> 
        </ns2:Message> 
       </ns2:MessageList> 
      </ns2:Employee> 
     </ns2:ResponseData> 
    </Body> 
</Envelope> 
+0

以上xslt工作良好,期望有soap名稱空間屬性複製soap前綴元素的子元素。

> – Mike

+0

我已經測試了針對您發佈的輸入示例的代碼,並將結果包含在答案中,沒有'xmlns:soap'聲明,因此您必須詳細告訴我們您使用的XSLT處理器如果你真的還有問題。 –

+0

我在您發佈的datapower aboue xslt代碼中使用。 – Mike

0

的DataPower只實現XSLT 1.0,而不是2.0。

您可以通過複製控制你的命名空間:

<xsl:copy> 
    <xsl:element name="ns:Element" namespace="http://www.xml.com/ns"> 
</xsl:copy> 
相關問題