2016-09-23 154 views
0

我有一個輸入xml其中S:故障xmlns:ns4 =「http://www.w3.org/2003/05/soap-envelope」這給從故障節點提取數據帶來問題。 。命名空間問題

<?xml version="1.0" encoding="UTF-8"?> 
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"> 
    <S:Body> 
     <S:Fault xmlns:ns4="http://www.w3.org/2003/05/soap-envelope"> 
      <faultcode>S:Server</faultcode> 
      <faultstring>The GTIN is not valid or the system can not map the Company Prefix to an existing Company Prefix from the Setting</faultstring> 
     </S:Fault> 
    </S:Body> 
</S:Envelope> 

XLST代碼是不工作...

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns0="example" xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:ns4="http://www.w3.org/2003/05/soap-envelope" 
exclude-result-prefixes="S ns4"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 

    <xsl:template match="/S:Envelope"> 
      <ns0:MT_CreateSerialNumberResponse_IB xmlns:ns0="example"> 
      <serialNumberList xmlns="urn:abcd:1"> 
       <body> 
        <message> 
         <xsl:value-of select="S:Body/ns4:Fault/ns4:faultstring"/> 
        </message> 
       </body> 
      </serialNumberList> 
     </ns0:MT_CreateSerialNumberResponse_IB> 
    </xsl:template> 
</xsl:stylesheet> 

預期的結果...請幫助發現代碼錯誤,如果我錯過了什麼

<ns0:MT_CreateSerialNumberResponse_IB xmlns:ns0="example"> 
    <serialNumberList xmlns="urn:abcd:1"> 
     <body><message>The GTIN is not valid or the system can not map the Company Prefix to an existing Company Prefix from the Setting</message> 
     </body> 
    </serialNumberList> 
</ns0:MT_CreateSerialNumberResponse_IB> 

回答

1

試試這個

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns0="example" xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:ns4="http://www.w3.org/2003/05/soap-envelope" 
exclude-result-prefixes="S ns4"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 

    <xsl:template match="/S:Envelope"> 
      <ns0:MT_CreateSerialNumberResponse_IB xmlns:ns0="example"> 
      <serialNumberList xmlns="urn:abcd:1"> 
       <body> 
        <message> 
         <xsl:value-of select="S:Body/S:Fault/faultstring"/> 
        </message> 
       </body> 
      </serialNumberList> 
     </ns0:MT_CreateSerialNumberResponse_IB> 
    </xsl:template> 
</xsl:stylesheet> 
+0

沒有爲XSLT驗證一個免費的在線工具:HTTP:// xslttest。 appspot.com/ – Naidu

1
  1. 有一個在XML輸入其名稱的前綴沒有節點通過ns4:。這使得名稱空間聲明xmlns:ns4="http://www.w3.org/2003/05/soap-envelope"完全是多餘的(在XML和XSLT中)。

  2. 只有默認名稱空間聲明(無前綴)被繼承。 faultstring元素沒有前綴,並且沒有默認的名稱空間聲明。這意味着它是沒有命名空間,它的路徑(從S:Envelope上下文)是:

    S:Body/S:Fault/faultstring