With this:
<p>Some variable length text <abbr>abc</abbr> and more text.</p>
<p><abbr>abc</abbr>Some variable length text.</p>
<p><abbr>abc</abbr>Some variable length text. <a href="somelink">blah</a></p>
I'm trying to build an Xpath that will find p elements that:
- Have a child
<abbr>with value of "abc", but that child has no intervening text between the parent and the child. - Does not have a child
<a>.
#1 means the first line should not match, because it has text between the parent <p> and the child <abbr>.
#2 means the third line should not match, because although there is a child <abbr> and no leading text, it also has a child <a>.
Thus, only the second line,
<p><abbr>abc</abbr>Some variable length text.</p>
should match: no leading text, and no child <a>.
I've played with an XPath testbed for an hour, and done quite a bit of searching, but haven't figured out how to handle both of those requirements, but especially #1.
CodePudding user response:
This XPath,
//p[node()[1][self::abbr]='abc'][not(a)]
selects all p elements
- with a first sibling
abbrelement that has a string value of"abc"1, and - without an
achild,
as requested.
1 Note that #1 implies that abbr must be the first node, which prevents any node (text, another element, etc) from be a previous sibling to abbr.
CodePudding user response:
Try this XPath-1.0 expression:
//p[node()[1]!=text()[1] and abbr='abc' and not(a)]
It checks if the first child node is a text() node. Further checks are that it has a child "abbr" with the content 'abc' and no children named "a".
