由于命名空间而导致XSL转换失败

时间:2016-08-11 14:28:23

标签: xml xslt xpath

以下是我从我创建的服务中获得的响应XML -

  <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsa="http://www.w3.org/2005/08/addressing">
       <env:Header>
          <wsa:MessageID>urn:3474700f-5fc9-11e6-ba99-00144ff9e607</wsa:MessageID>
          <wsa:ReplyTo>
             <wsa:Address>http://www.w3.org/2005/08/addressing/anonymous</wsa:Address>
             <wsa:ReferenceParameters>
                <instra:tracking.ecid xmlns:instra="http://xmlns.oracle.com/sca/tracking/1.0">fc5bd4a9-5dea-4f03-bef5-b62057ff17d5-00055065</instra:tracking.ecid>
                <instra:tracking.FlowEventId xmlns:instra="http://xmlns.oracle.com/sca/tracking/1.0">280018</instra:tracking.FlowEventId>
                <instra:tracking.FlowId xmlns:instra="http://xmlns.oracle.com/sca/tracking/1.0">140005</instra:tracking.FlowId>
                <instra:tracking.CorrelationFlowId xmlns:instra="http://xmlns.oracle.com/sca/tracking/1.0">0000LPtpMnV4Muk_Ox5Eid1Ncr^i000006</instra:tracking.CorrelationFlowId>
             </wsa:ReferenceParameters>
          </wsa:ReplyTo>
          <wsa:FaultTo>
             <wsa:Address>http://www.w3.org/2005/08/addressing/anonymous</wsa:Address>
          </wsa:FaultTo>
       </env:Header>
       <env:Body>
          <SCOR_SCORING_RES>
             <TABLE>
                <RESULT>
                   <CALC_MAX_REPAYMENT_CLIENT>100000</CALC_MAX_REPAYMENT_CLIENT>
                   <INTERNAL_CREDIT_RATING_CD>3</INTERNAL_CREDIT_RATING_CD>
                   <OUTCOME_CD>Good</OUTCOME_CD>
                   <CALC_MAX_LIMIT_ALTERN>50000</CALC_MAX_LIMIT_ALTERN>
                </RESULT>
             </TABLE>
          </SCOR_SCORING_RES>
       </env:Body>
    </env:Envelope>

我想在此XML上应用XSLT并获取数据。 我写了一个小的XSLT代码 -

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="Envelope">
          <xsl:value-of select="Body/SCOR_SCORING_RES/TABLE/RESULT/CALC_MAX_REPAYMENT_CLIENT"/>~
</xsl:template>
</xsl:stylesheet>

但结果XML没有给我标签CALC_MAX_REPAYMENT_CLIENT的值,但下面的结果是响应XML中所有标签的值 -

urn:3474700f-5fc9-11e6-ba99-00144ff9e607http://www.w3.org/2005/08/addressing/anonymousfc5bd4a9-5dea-4f03-bef5-b62057ff17d5-000550652800181400050000LPtpMnV4Muk_Ox5Eid1Ncr^i000006http://www.w3.org/2005/08/addressing/anonymous1000003Good50000

根据我对XSLT和XPATH的一点知识,Body / SCOR_SCORING_RES / TABLE / RESULT / CALC_MAX_REPAYMENT_CLIENT应该只返回一个值。请让我知道我做错了什么。

1 个答案:

答案 0 :(得分:1)

这是名称空间的问题。 EnvelopeBody元素位于输入XML的命名空间中,但您没有在XSLT中考虑到这一点。在你的XSLT中,你试图匹配一个没有空格的Envelope,这不是一回事。

试试这个XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
                xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">

  <xsl:template match="env:Envelope">
    <xsl:value-of select="env:Body/SCOR_SCORING_RES/TABLE/RESULT/CALC_MAX_REPAYMENT_CLIENT"/>
  </xsl:template>
</xsl:stylesheet>