【问题标题】:XPATH Firebug filter does not filter as expectedXPATH Firebug 过滤器未按预期过滤
【发布时间】:2016-05-02 05:14:28
【问题描述】:

基本上我有一份总统名单,我只对尼克松链接感兴趣,而不是克林顿或奥巴马。 我发现,我所做的过滤返回了正确数量的总统(在这种情况下为 1),但返回了所有 a 链接,而不仅仅是尼克松的链接。

HTML:

<a href="#" name="obama"></a> <div class="headlineBlock">
<h2>Obama</h2>
<p class="tudor"><strong>Conditions:</strong> Always sunny </p>
<table class="resultGrid"><tr> <td class="first">
<h4><a href="http://www.thelinkiwant.com?params" title="Click to view result"</a></h4>
<a href="#" name="nixon"></a> <div class="headlineBlock">
<h2>Nixon</h2>
<p class="nixon"><strong>Conditions:</strong> Sometimes late </p>
<table class="resultGrid"><tr> <td class="first">
<h4><a href="http://www.thelinkiwant.com/?params" title="Click to view result"</a></h4>
<a href="#" name="clinton"></a> <div class="headlineBlock">
<h2>Clinton</h2>
<p class="tudor"><strong>Conditions:</strong> Never rainy </p>
<table class="resultGrid"><tr> <td class="first">
<h4><a href="http://www.thelinkiwant/?params" title="Click to view result"</a></h4>

XPATH:

$x("//div[@class='headlineBlock']/h2[not(contains('|Clinton|Obama|',concat('|',.,'|') ))]//../../table/a/@href")

【问题讨论】:

    标签: xpath


    【解决方案1】:

    您的示例存在几个问题。

    在单个“点击查看结果”之后缺少括号,您的“headlineBlock” div 和表格未关闭等。所以首先您应该确保您的数据格式正确。

    W3C's Xml Validator can help with that

    您的 XPath 看起来基本没问题,我认为问题在于最后的 // - 他们有点太早了。试试这个:

    //div[@class='headlineBlock']/h2[not(contains('|Clinton|Obama|',concat('|',.,'|') ))]/..//a/@href
    

    //div[@class='headlineBlock']

    headlineBlock 类的所有 div ...

    /h2[not(contains('|Clinton|Obama|',concat('|',.,'|') ))]

    ...不包含某些术语。

    /..

    上一层(现在我们又回到了 div headlineBlock

    //a

    元素类型a的任何直系后代

    /@href

    H-Ref 属性

    【讨论】:

    • 这可能是由于您的数据结构混乱 - 修复它,它将起作用。否则,您将不得不使用其他选择器,例如 [last()-1] following 等。当前结构不兼容 XML,因此可以根据您的解析器实现以不同方式解释。
    猜你喜欢
    • 2011-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多