简体   繁体   English

从XPATH后代值获取父节点

[英]Get parent node from XPATH descendant value

I have a HTML structure that I am attempting to test using XPATH. 我有一个HTML结构,我试图使用XPATH进行测试。 The structure I'll looking at currently is something like: 我目前要看的结构是这样的:

<div id="item_1" class="item-wrapper">
    <div class="container item">

        <div class="container-header">
            <h2 id="title">test-title</h2>
            <h5 id="description">test-description</h5>
        </div>

    </div>
</div>

There are a number of 'items' in my document, each with a unique ID. 我的文档中有许多“项目”,每个项目都有唯一的ID。 Using XPATH I want to 'get' the item_1 node from the value of the title id node ("test-title" in this case). 使用XPATH我想从title id节点的值'获取'item_1节点(在本例中为“test-title”)。

I also want to do this in such a way that is not dependant on the node tag ( div / h2 /etc). 我还希望以不依赖于节点标记( div / h2 / etc)的方式执行此操作。

I've been playing around with predicates, but I can't seem to get a solution that works. 我一直在玩谓词,但我似乎无法找到一个有效的解决方案。 For example: 例如:

//*[@class="item-wrapper"][./*[@id="title"][text()="test-title"]] 

Thanks for any help! 谢谢你的帮助!

You can use the below XPATH : 您可以使用以下XPATH:

 //*[@id="title"]/ancestor::*[@id="item_1"][1]

Or, you can also use the function Function: node-set id(object) 或者,您也可以使用函数Function: node-set id(object)

 id('title')//ancestor::*[@id="item_1"][1]

Update 更新

 //*[.="test-title"]/ancestor::*[@class="item-wrapper"][1]

output 产量

<div id="item_1" class="item-wrapper">
    <div class="container item">

        <div class="container-header">
            <h2 id="title">test-title</h2>
            <h5 id="description">test-description</h5>
        </div>

    </div>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM