2015-02-11 100 views
0

我有一個XML發佈到WebService,我有問題,因爲它期望其他XML格式。 我需要改變下一個XML添加的xmlns:。ANS =「http://www.ans.gov.br/padroes/tiss/schemas和刪除solicitacaoProcedimentoWS節點的命名空間XSLT變換移動命名空間

一些幫助將不勝感激 謝謝事先, 路易斯

<?xml version="1.0"?> 
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <SOAP-ENV:Header/> 
    <SOAP-ENV:Body> 
    <solicitacaoProcedimentoWS xmlns="http://www.ans.gov.br/padroes/tiss/schemas"> 
     <cabecalho> 
     <identificacaoTransacao> 
      <tipoTransacao>SOLICITACAO_PROCEDIMENTOS</tipoTransacao> 
      <sequencialTransacao>k1</sequencialTransacao> 
     </identificacaoTransacao> 
     </cabecalho> 
     <hash>393d3f1e310f3385ad0398dd9b65dc4a</hash> 
    </solicitacaoProcedimentoWS> 
    </SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 

我倒是想轉換爲:

<?xml version="1.0"?> 
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ans="http://www.ans.gov.br/padroes/tiss/schemas" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
     <SOAP-ENV:Header/> 
     <SOAP-ENV:Body> 
     <ans:solicitacaoProcedimentoWS> 
      <ans:cabecalho> 
      <ans:identificacaoTransacao> 
       <ans:tipoTransacao>SOLICITACAO_PROCEDIMENTOS</ans:tipoTransacao> 
       <ans:sequencialTransacao>k1</ans:sequencialTransacao> 
      </ans:identificacaoTransacao> 
      </ans:cabecalho> 
      <ans:hash>393d3f1e310f3385ad0398dd9b65dc4a</ans:hash> 
     </ans:solicitacaoProcedimentoWS> 
     </SOAP-ENV:Body> 
    </SOAP-ENV:Envelope> 
+0

輸入和輸出之間有什麼區別 - 除了化妝品? – 2015-02-11 12:08:46

+0

嗨,沒有區別,但如果我不轉換XML格式,web服務不接受。我不能改變web服務,因爲不是我的。 – 2015-02-11 13:28:45

回答

1

下面的XSLT 2.0樣式表應該做你需要什麼:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" 
       xmlns:ans="http://www.ans.gov.br/padroes/tiss/schemas"> 

    <!-- identity transformation - keep everything the same except when overridden --> 
    <xsl:template match="@*|node()"> 
    <xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy> 
    </xsl:template> 

    <!-- add an extra NS declaration to the document element --> 
    <xsl:template match="/*"> 
    <xsl:copy> 
     <xsl:namespace name="ans" select="'http://www.ans.gov.br/padroes/tiss/schemas'"/> 
     <xsl:apply-templates select="@*|node()" /> 
    </xsl:copy> 
    </xsl:template> 

    <!-- use a prefixed name for any element in the namespace 
     http://www.ans.gov.br/padroes/tiss/schemas --> 
    <xsl:template match="ans:*"> 
    <xsl:element name="ans:{local-name()}" namespace="http://www.ans.gov.br/padroes/tiss/schemas"> 
     <xsl:apply-templates select="@*|node()" /> 
    </xsl:element> 
    </xsl:template> 

</xsl:stylesheet> 

但真正的問題是你說的服務 - 如你所知,該文件的兩個版本在所有相同的命名空間都是一樣的元素,因此應被視爲任何相同支持名稱空間的XML處理器。如果他們關心以這種方式使用特定的前綴,那麼他們可能沒有使用合適的名稱空間感知XML解析器,並且誰知道他們忽略或濫用的XML規範的其他方面。如果他們需要以ans:前綴命名主體元素,這是否意味着他們還需要信封才能使用SOAP-ENV:前綴?我已經在過去使用的至少一個SOAP工具包使用soap:對於這個命名空間,而不是SOAP-ENV: ...


如果你只限於XSLT 1.0,那麼你不能使用<xsl:namespace>創建任意命名空間綁定,而不是嘗試類似於

<xsl:copy-of select="document('')/*/namespace::ans" /> 

從樣式表文檔本身複製名稱空間聲明。

+0

非常感謝。你的xslt工作得很好。正文內容前綴是必需的,但信封前綴是免費的。 – 2015-02-11 19:05:12

+0

@LuizAlves很樂意幫忙。在Stack Overflow上,當你得到一個可以解決你的問題的答案時,將其標記爲_accepted_(通過點擊左邊的刻度標記)是禮貌的 – 2015-02-11 19:21:49

+0

對不起,伊恩羅伯茨先生。我剛剛做完。我是Stack Overflow的新手。 – 2015-02-11 21:02:23