【问题标题】:XPATH does not give a result. Wrong namespace?XPATH 没有给出结果。命名空间错误?
【发布时间】:2015-02-11 01:17:35
【问题描述】:

我正在尝试解析以下 SOAP 响应:

    <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">  
  <s:Body> 
    <TestFunctionResponse xmlns="http://tempuri.org/">  
      <TestFunctionResult>Verarbeitet: test</TestFunctionResult> 
    </TestFunctionResponse> 
  </s:Body> 
</s:Envelope>

为此,我运行了一个 PL/SQL 函数:

    l_value_xml := apex_web_service.parse_xml( p_xml => l_soap_response,
                                           p_xpath   => '/TestFunctionResult/text()',
                                           p_ns      => 'xmlns="http://tempuri.org/"'
                                          );

它没有给我任何回应。我在 xpath 在线测试器中尝试了 xpath。也没有回应。我认为这是一个错误的命名空间,但我找不到错误。

【问题讨论】:

    标签: xml soap xpath plsql oracle-apex


    【解决方案1】:

    (免责声明:我不熟悉pl/sql,只熟悉XPath)

    命名空间 URI 是正确的,但是您原来的 XPath 表达式:

    p_xpath   => '/TestFunctionResult/text()'
    

    尝试查找 SOAP 响应中不存在的内容。它查找类似于以下内容的 XML 文档:

    <TestFunctionResult>Verarbeitet: test</TestFunctionResult>
    

    TestFunctionResult 元素是最外层的元素。在您的实际文档中,情况并非如此。

    使用// (descendant-or-self::) 轴:

    p_xpath   => '//TestFunctionResult/text()'
    

    或者,更好的是,像这样输入层次结构:

    p_xpath   => '/Envelope/Body/TestFunctionResponse/TestFunctionResult/text()'
    

    另一个潜在的错误来源可能是您在 PL/SQL 代码中声明了默认命名空间,但该命名空间并未自动与路径表达式中的元素关联。

    在具有 XPath 功能的编程语言中,使用 前缀注册或声明命名空间更为常见:

    xmlns:soap="http://tempuri.org/"
    

    然后在路径表达式中为属于该命名空间的所有元素添加前缀:

    p_xpath   => '//soap:TestFunctionResult/text()'
    

    【讨论】:

    • 非常感谢。我用前缀试过了,一切都像我想要的那样工作。
    猜你喜欢
    • 1970-01-01
    • 2020-06-30
    • 1970-01-01
    • 2018-12-04
    • 1970-01-01
    • 1970-01-01
    • 2014-05-03
    • 2012-06-29
    • 1970-01-01
    相关资源
    最近更新 更多