2017-01-13 70 views
-1

首先,我將提供我已有的代碼。 XML:從SOAP響應中提取XSL字符串

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <soapenv:Body> 
     <getUserDataResponse xmlns="http://wtp"> 
     <getUserDataReturn>Matt</getUserDataReturn> 
     <getUserDataReturn>NY</getUserDataReturn> 
     </getUserDataResponse> 
    </soapenv:Body> 
</soapenv:Envelope> 

這是XSLT我有:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope"> 
<xsl:output method="xml" omit-xml-declaration="no" encoding="utf-8" indent="yes" /> 
    <xsl:template match="/"> 
     <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
      <soapenv:Body> 
       <getUserDataResponse xmlns="http://wtp"> 
        <xsl:for-each select="soapenv:Envelope/soapenv:Body/soapenv:getUserDataResponse/soapenv:getUserDataReturn"> 
         <xsl:value-of select="." /> 
        </xsl:for-each> 
       </getUserDataResponse> 
      </soapenv:Body> 
     </soapenv:Envelope> 
    </xsl:template> 
</xsl:stylesheet> 

我想要做到的,是提取陣列的只有第一個元素,在這種情況下,字符串「馬特,然後把它喜歡它是隻是定期迴應,將其進一步發送到另一個端點「。我不知道什麼可能是不正確的。

輸出,我想:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <soapenv:Body> 
     <getUserDataResponse xmlns="http://wtp"> 
     <getUserDataReturn>Matt</getUserDataReturn> 
     </getUserDataResponse> 
    </soapenv:Body> 
</soapenv:Envelope> 

輸出現在我得到的是隻是沒有數據肥皂模板。

會很感激,如果有人可以幫助:)

乾杯!

回答

0

你想要的輸出可通過以下方式實現:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:wtp="http://wtp"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 

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

<xsl:template match="wtp:getUserDataResponse"> 
    <xsl:copy> 
     <xsl:apply-templates select="wtp:getUserDataReturn[1]"/> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 

要做到這一點,你已經開始了,你就必須做:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:wtp="http://wtp"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 

<xsl:template match="/"> 
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
     <soapenv:Body> 
      <getUserDataResponse xmlns="http://wtp"> 
       <getUserDataReturn> 
        <xsl:value-of select="soapenv:Envelope/soapenv:Body/wtp:getUserDataResponse/wtp:getUserDataReturn[1]" /> 
       </getUserDataReturn> 
      </getUserDataResponse> 
     </soapenv:Body> 
    </soapenv:Envelope> 
</xsl:template> 

</xsl:stylesheet> 

注意使用前綴來選擇默認的"http://wtp"名稱空間中的元素。