英文:
XPath targeting specific child and grandchild (child of child)
问题
我试图选择所有p标签,其中没有strong标签作为子标签或p标签的子代标签(子标签的子标签)。
这里是示例:
<p>
Lorem ipsum dolor sit amet consectetur, adipisicing elit. <a>Lorem ipsum dolor sit amet consectetur, adipisicing elit.</a>
</p>
<p>
<strong>Lorem ipsum dolor sit amet consectetur, adipisicing elit. <a>Lorem ipsum dolor sit amet consectetur, adipisicing elit.</a></strong>
</p>
<p>
Lorem ipsum dolor sit amet consectetur, adipisicing elit. <a><strong>Lorem ipsum dolor sit amet consectetur, adipisicing elit.</strong></a>
</p>
<p>
Lorem ipsum dolor sit amet consectetur, adipisicing elit. <a>Lorem ipsum dolor sit amet consectetur, adipisicing elit.</a>
</p>
<p>
Lorem ipsum dolor sit amet consectetur, adipisicing elit. <a><strong>Lorem ipsum dolor sit amet consectetur, adipisicing elit.</strong></a>
</p>
在上面的代码中,第二、第三和最后一个是我不想要的,但我想要其余的。我已经得到了这个XPath:
//p[not(child::strong)]
,这将删除第二个段落,但当我添加a>strong时,//p[not(child::strong) or not(child::a > strong)]
第二部分被忽略。有人可以指出我哪里出错以及如何解决这个问题吗?
不翻译的代码部分:
//p[not(child::strong)]
//p[not(child::strong) or not(child::a > strong)]
英文:
I am trying to target all the p tags, where there is no strong tag as a child or grandchild(child of the child) of the p tag.
here is the sample
<p>
Lorem ipsum dolor sit amet consectetur, adipisicing elit. <a>Lorem ipsum dolor sit amet consectetur, adipisicing elit.</a>
</p>
<p>
<strong>Lorem ipsum dolor sit amet consectetur, adipisicing elit. <a>Lorem ipsum dolor sit amet consectetur, adipisicing elit.</a></strong>
</p>
<p>
Lorem ipsum dolor sit amet consectetur, adipisicing elit. <a><strong>Lorem ipsum dolor sit amet consectetur, adipisicing elit.</strong></a>
</p>
<p>
Lorem ipsum dolor sit amet consectetur, adipisicing elit. <a>Lorem ipsum dolor sit amet consectetur, adipisicing elit.</a>
</p>
<p>
Lorem ipsum dolor sit amet consectetur, adipisicing elit. <a><strong>Lorem ipsum dolor sit amet consectetur, adipisicing elit.</strong></a>
</p>
In the above code, the 2nd, 3rd, and last one are the ones that I don't want but I want the rest of them. I have gotten as far as this XPath:
//p[not(child::strong)]
which removes the 2nd paragraph, but when i add the a>strong like //p[not(child::strong) or not(child::a > strong)]
the second part is being ignored. Can anyone point out where am I going wrong and how to fix this issue?
答案1
得分: 1
你想要 //p[not(strong) and not(*/strong)]
。
英文:
You want //p[not(strong) and not(*/strong)]
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论