html - Find the first ancestor node with X attribute using XPath -
i have duplicate nested structures in dom , trying find input elements @ current node level.
for example, current node <div data-model="user" data-id="1">. how the <input value=foo without getting <input value=bar (since it's under it's own data-model)?
<div> <div data-model="user" data-id="1"> <div> <input data-field="email" value="foo"> <div data-model="user" data-id="2"> <div> <input data-field="email" value="bar"> </div> </div> </div> </div> </div> here have in xpath far:
//*[@data-model="user"]//*[@data-field , ancestor::*[@data-model="user" ....]]
you in 2 steps. first evaluate
count(ancestor-or-self::div[@data-model = 'user']) as number, current div context node. take number $n , evaluate
.//input[count(ancestor::div[@data-model = 'user']) = $n] the idea here find descendant input elements have same number of containing data-model="user" divs context node started (inclusive).
from xslt in single pass because have access current() function lets "escape" predicate
.//input[count(ancestor::div[@data-model = 'user']) = count(current()/ancestor-or-self::div[@data-model = 'user'])] this function isn't available in pure xpath if library lets pass variable bindings expressions provide context node in variable , use in place of current().
Comments
Post a Comment