xpath:有没有办法在xpath中获取所有子文本

Guy*_*Guy 6 xpath

我在Firefox上使用xpath引擎.我有html:

<span>
   <b>prefix one</b> not bold part
</span>
<span>
   prefix two not bold part
</span>
Run Code Online (Sandbox Code Playgroud)

我希望所有span具有以"前缀一"开头的子文本的s.

我试过xpath:

//span[starts-with(child::text(), "prefix one")]
Run Code Online (Sandbox Code Playgroud)

但这不起作用,因为b标签是干扰的.你是如何解决这个问题的?

谢谢

Gai*_*aim 14

如果您知道跨度没有嵌套到其他跨度中,您可以尝试这样做:

//span[ starts-with( descendant-or-self::*/text(),"prefix one" ) ]
Run Code Online (Sandbox Code Playgroud)

descendant-or-self::*/text(),应该返回此子树中的所有文本节点.我不知道究竟starts-with()是如何工作的,但我想当text()子树中的一些节点以"前缀1"开头时条件为真

  • 这相当于`// span [starts-with(.,'prefix one')]`或更健壮,`// span [starts-with(normalize-space(),'prefix one')]` (2认同)