使用XPath选择两个节点之间的兄弟节点 [英] Selecting siblings between two nodes using XPath

查看:191
本文介绍了使用XPath选择两个节点之间的兄弟节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何选择id为header_completed的表与header_completed中的中心对齐后的第一个表之间的所有表?这里是我选择它的html:

 < table border =0cellpadding =0cellspacing = 0width =920align =center>< / table> 
< table border =0cellpadding =0cellspacing =0width =920>< / table>
< table border =0cellpadding =0cellspacing =0width =920>< / table>
< table border =0cellpadding =0cellspacing =0width =920align =centerclass =header_completed>< / table>
< table border =0cellpadding =0cellspacing =0width =920>< / table> < -
< table border =0cellpadding =0cellspacing =0width =920>< / table> < -
< table border =0cellpadding =0cellspacing =0width =920>< / table> < - 这5
< table border =0cellpadding =0cellspacing =0width =920>< / table> < -
< table border =0cellpadding =0cellspacing =0width =920>< / table> < -
< table border =0cellpadding =0cellspacing =0width =920align =center>< / table>
< table border =0cellpadding =0cellspacing =0width =920>< / table>
< table border =0cellpadding =0cellspacing =0width =920>< / table>
< table border =0cellpadding =0cellspacing =0width =920align =center>< / table>

我尝试使用 // table [@ id =header_completed] / following-sibling :: node()[following-sibling :: table [@ align =center] [1]] 但它没有用。

解决方案

我相信这个XPath表达式会选择你想要的节点:

  // table [@ class =header_completed] / 
following-sibling :: table [@ align =center] [1] /
before-sibling :: table [
先前的sibling :: table [@ class =header_completed]
]

使用 @ class =header_completed导航到

从那里我选择了第一个下面的兄弟表,其中 @ align =center



我选择所有前面的兄弟表,它们都有一个前面的兄弟表,这个表是 @ class =header_completed


How would I select all the tables between the table whose id is header_completed and the first table after the header_completed one that has an align of center? Here is the html I am selecting it from:

<table border="0" cellpadding="0" cellspacing="0" width="920" align="center"></table>
<table border="0" cellpadding="0" cellspacing="0" width="920"></table>
<table border="0" cellpadding="0" cellspacing="0" width="920"></table>
<table border="0" cellpadding="0" cellspacing="0" width="920" align="center" class="header_completed"></table>
<table border="0" cellpadding="0" cellspacing="0" width="920"></table> <--
<table border="0" cellpadding="0" cellspacing="0" width="920"></table> <--
<table border="0" cellpadding="0" cellspacing="0" width="920"></table> <-- these 5
<table border="0" cellpadding="0" cellspacing="0" width="920"></table> <--
<table border="0" cellpadding="0" cellspacing="0" width="920"></table> <--
<table border="0" cellpadding="0" cellspacing="0" width="920" align="center"></table>
<table border="0" cellpadding="0" cellspacing="0" width="920"></table>
<table border="0" cellpadding="0" cellspacing="0" width="920"></table>
<table border="0" cellpadding="0" cellspacing="0" width="920" align="center"></table>

I tried using //table[@id="header_completed"]/following-sibling::node()[following-sibling::table[@align="center"][1]] but it didn't work.

解决方案

I believe this XPath expression selects the nodes you want:

//table[@class="header_completed"]/
    following-sibling::table[@align="center"][1]/
        preceding-sibling::table[
            preceding-sibling::table[@class="header_completed"]
        ]

First I navigate to the table with @class="header_completed".

From there I select the first following sibling table with @align="center".

From there I select all preceding sibling tables that have a preceding sibling which is the table with @class="header_completed".

这篇关于使用XPath选择两个节点之间的兄弟节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆