XPath 定位特定子元素和子孙元素(子元素的子元素)

huangapple go评论41阅读模式
英文:

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)].

huangapple
  • 本文由 发表于 2023年2月16日 17:10:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75469971.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定