2016-11-14 188 views
0

這是我的XML與SOAP標題和正文:XSLT - 刪除前綴命名空間特定節點XML

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> 
    <s:Body> 
     <RequestResponse xmlns="http://tempuri.org/"> 
      <a:RequestResult xmlns:a="http://schemas.datacontract.org/2004/07/MockupTesting" 
      xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
       <a:Message>Message text testing.</a:Message> 
       <a:Response>false</a:Response> 
      </a:RequestResult> 
     </RequestResponse> 
    </s:Body> 
</s:Envelope> 

我需要RequestResult節點只刪除前綴。 從這個

<a:RequestResult xmlns:a="http://schemas.datacontract.org/2004/07/MockupTesting" 
      xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 

要:

<RequestResult xmlns:a="http://schemas.datacontract.org/2004/07/MockupTesting" 
      xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 

這是XSLT配置文件我有2版本中使用:

<xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:output omit-xml-declaration="yes" indent="yes" /> 

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

    <xsl:template match="/"> 
     <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> 
      <s:Body> 
       <xsl:apply-templates /> 
      </s:Body> 
     </s:Envelope> 
    </xsl:template> 

    <xsl:template match="RequestResult |RequestResult//*"> 
     <xsl:element name="a:{name()}" 
      namespace="http://schemas.datacontract.org/2004/07/Testing"> 
      <xsl:namespace name="a" 
       select="'http://schemas.datacontract.org/2004/07/MockupTesting'" /> 
      <xsl:namespace name="i" 
       select="'http://www.w3.org/2001/XMLSchema-instance'" /> 
      <!-- <xsl:copy-of select="namespace::*" /> --> 
      <xsl:apply-templates select="node()|@*" /> 
     </xsl:element> 
    </xsl:template> 

</xsl:stylesheet> 

我要補充或修改,以去除該節點上的前綴?

+1

你不能「刪除」命名空間前綴,在絕大多數情況下,你不需要做那樣的事情。你能解釋一下爲什麼你認爲這是必要的嗎? – Tomalak

+0

由於我使用的WSDL,它需要@Tomalak – gtx911

+0

對不起,這不是一個令人滿意的解釋。我問:「你爲什麼需要它?」你回答說:「因爲我需要它。」我認爲你需要給出一個比這更好的理由。 – Tomalak

回答

0

無法從節點「刪除前綴」。前綴是節點名稱的一部分。要刪除前綴,你必須創建使用其他名稱,可能還有新的節點 - 在您的例子 - 在另一個命名空間:

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:a="http://schemas.datacontract.org/2004/07/MockupTesting"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 

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

<xsl:template match="a:RequestResult"> 
    <xsl:element name="RequestResult" namespace="http://tempuri.org/"> 
     <xsl:copy-of select="namespace::*"/> 
     <xsl:apply-templates/> 
    </xsl:element> 
</xsl:template> 

</xsl:stylesheet> 
+0

但是,如果我添加該命名空間,它將無法工作 – gtx911

+0

@ gtx911請停止在謎語中談話。上面的轉換會產生你所要求的**精確**結果。如果你想要別的東西,編輯你的問題並澄清。同樣的事情適用於您的其他問題:http://stackoverflow.com/a/40589393/3016153 –