2017-01-13 36 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"名称空间中的元素。