使用 XPath 选择两个节点之间的兄弟节点

2024-04-24

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

<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>

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


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

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

首先我导航到table with @class="header_completed".

从那里我选择以下第一个同级表@align="center".

从那里我选择所有具有前一个同级表的前同级表,该表具有@class="header_completed".

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 XPath 选择两个节点之间的兄弟节点 的相关文章

随机推荐