【问题标题】:Why I can't use XPath to query the required node?为什么我不能使用 XPath 来查询所需的节点?
【发布时间】:2012-08-17 09:32:38
【问题描述】:

这是 XML:

<VDWSDirectoriesResult xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
<Success>true</Success>
<TotalResult>6</TotalResult>
<PageResult>6</PageResult>
<Summary/>
<DirectoriesResult>...</DirectoriesResult>
</VDWSDirectoriesResult>

我想获得Success 节点,我尝试了/VDWSDirectoriesResult/SuccessSuccess,但我仍然无法获得Success 节点的值...我做错了什么?谢谢。

【问题讨论】:

    标签: xml xpath xml-parsing


    【解决方案1】:

    您的根节点声明了一个默认命名空间:

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

    这意味着,如果您只是在 XPath 表达式中写入 Success,那么您的 &lt;Success&gt; 节点实际上是在该命名空间中,而不是在 XPath 假定的匿名命名空间中。

    根据您使用的 XPath 库,将有一些方法可以将命名空间前缀映射到命名空间 URI http://tempuri.org/,然后您可以在 XPath 表达式中使用它。

    例如,假设您将命名空间前缀 tmp 映射到该 URI,您的 XPath 应为:

    /tmp:VDWSDirectoriesResult/tmp:Success
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-06
      • 2013-06-01
      • 1970-01-01
      • 2011-04-14
      • 2011-07-24
      • 1970-01-01
      相关资源
      最近更新 更多