根据两个条件查找xpath元素

时间:2016-08-17 12:01:41

标签: html xml xpath

我希望能够获得一个XPATH表达式,该表达式返回满足两个条件的行的第二列的链接:

  1. 第三列包含Disk_/proc/timer_stats
  2. 第四列包含Critical condition
  3. XML是:

    <tbody>
    <tr id="table2-0" style="" class="datos2">
    <td id="table2-0-0" style=""   class="datos2 "></td>
    <td id="table2-0-1" style=""   class="datos2 "><a href="index.php?sec=estado&sec2=operation/agentes/ver_agente&refr=0&filter=all_enabled&filter_standby=all&ag_group=0&tag_filter=0&action_filter=0&id_agente=1&tab=alert&amp;id_alert=1&amp;force_execution=1&refr=60"><img src="http://192.168.50.50:84/pandora_console/images/target.png" data-title="Force" data-use_title_for_force_title="1" class="forced_title" style="border:0px;" alt="Force" /></a></td>
    <td id="table2-0-2" style=""   class="datos2 "><span style="font-size: 7.2pt">AvailableMemory</span></td>
    <td id="table2-0-3" style=""   class="datos2 "><a class="template_details" href="ajax.php?page=godmode/alerts/alert_templates&get_template_tooltip=1&id_template=1"><img src="http://192.168.50.50:84/pandora_console/images/zoom.png" /></a> <span style="font-size: 7.1pt">Critical condition</span></td>
    <td id="table2-0-4" style=""   class="datos2 ">Mail&#x20;to&#x20;Admin <i>(Default)</i></td>
    <td id="table2-0-5" style=""   class="datos2 "><span title="Unknown/Never" style="white-space:nowrap;">Unknown</span></td>
    <td id="table2-0-6" style=" text-align:center; width:5%;"   class="datos2 "><img src="http://192.168.50.50:84/pandora_console/images/status_sets/default/alert_not_fired.png" data-title="Alert not fired" data-use_title_for_force_title="1" class="forced_title" alt="Alert not fired" /></td>
    <td id="table2-0-7" style=" text-align:center; width:5%;"   class="datos2 "><input name="validate[]" type="checkbox" value="1"  id="checkbox-validate"  />
    <input id="hidden-validate[]_sent" name="validate[]_sent" type="hidden"  value="1" /></td>
    </tr>
    <tr id="table2-1" style="" class="datos">
    <td id="table2-1-0" style=""   class="datos "></td>
    <td id="table2-1-1" style=""   class="datos "><a href="index.php?sec=estado&sec2=operation/agentes/ver_agente&refr=0&filter=all_enabled&filter_standby=all&ag_group=0&tag_filter=0&action_filter=0&id_agente=1&tab=alert&amp;id_alert=2&amp;force_execution=1&refr=60"><img src="http://192.168.50.50:84/pandora_console/images/target.png" data-title="Force" data-use_title_for_force_title="1" class="forced_title" style="border:0px;" alt="Force" /></a></td>
    <td id="table2-1-2" style=""   class="datos "><span style="font-size: 7.2pt">Disk_/proc/timer_stats</span></td>
    <td id="table2-1-3" style=""   class="datos "><a class="template_details" href="ajax.php?page=godmode/alerts/alert_templates&get_template_tooltip=1&id_template=1"><img src="http://192.168.50.50:84/pandora_console/images/zoom.png" /></a> <span style="font-size: 7.1pt">Critical condition</span></td>
    <td id="table2-1-4" style=""   class="datos ">Mail&#x20;to&#x20;Admin <i>(Default)</i></td>
    <td id="table2-1-5" style=""   class="datos "><span title="Unknown/Never" style="white-space:nowrap;">Unknown</span></td>
    <td id="table2-1-6" style=" text-align:center; width:5%;"   class="datos "><img src="http://192.168.50.50:84/pandora_console/images/status_sets/default/alert_not_fired.png" data-title="Alert not fired" data-use_title_for_force_title="1" class="forced_title" alt="Alert not fired" /></td>
    <td id="table2-1-7" style=" text-align:center; width:5%;"   class="datos "><input name="validate[]" type="checkbox" value="2"  id="checkbox-validate1"  />
    <input id="hidden-validate[]_sent" name="validate[]_sent" type="hidden"  value="1" /></td>
    </tr>
    </tbody>
    

    我期望得到的是:

    <a href="index.php?sec=estado&sec2=operation/agentes/ver_agente&refr=0&filter=all_enabled&filter_standby=all&ag_group=0&tag_filter=0&action_filter=0&id_agente=1&tab=alert&amp;id_alert=2&amp;force_execution=1&refr=60">

2 个答案:

答案 0 :(得分:2)

/tbody/tr[td[3] = 'Disk_/proc/timer_stats' and normalize-space(td[4]) = 'Critical condition']/td[2]/a
  • /tbody/tr选择根tr的{​​{1}}子项,其中:
  • tbody第三列包含文字td[3] = 'Disk_/proc/timer_stats'
  • Disk_/proc/timer_stats
  • and第四列的文本内容,忽略前导和尾随空格
  • normalize-space(td[4])等于
  • =
  • 'Critical condition'然后选择第二列的链接

答案 1 :(得分:0)

感谢answer of Keith Hall,我得到了XPATH表达式:

'//tr[td[3][contains(.,"Disk_/proc/timer_stats")] and td[4][contains(.,"Critical condition")]]/td[2]/a'

返回

[<a href=​"index.php?sec=estado&sec2=operation/​agentes/​ver_agente&refr=0&filter=all_…action_filter=0&id_agente=1&tab=alert&id_alert=1&force_execution=1&refr=60">​…​</a>​]