【问题标题】:Get the index of a child node in parent's node using xpath使用xpath获取父节点中子节点的索引
【发布时间】:2018-07-16 17:43:17
【问题描述】:
<table>
    <tr>
        <th class="a">Name</th>
        <th class="b">Old </th>
        <th class="x">New</th>
    </tr>
</table>

在上面使用 X-path 的 sn-p 中,我需要获取 "th"w.r.to "tr" 节点的索引,即 3.How我能做到吗?

我尝试使用

找到节点
//aside//table//tr/th[text() = 'New']

但我不知道找到它的相对索引

是否有任何轴说明符来获取它的索引?

【问题讨论】:

    标签: html dom xpath


    【解决方案1】:

    尝试在XPath下面获取节点索引:

    //table//tr/th[text() = 'New']/count(preceding-sibling::th) + 1
    

    【讨论】:

    • 我相信它是 "count(//aside//table//tr/th[text() = 'New Value']/preceding-sibling::th) + 1" ??如有错误请指正
    • 是的。这种语法也应该是可以接受的。我的表达式应该适用于 XPath 2.0、3.0
    猜你喜欢
    • 1970-01-01
    • 2015-03-30
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多