XPath中的可选元素

Pab*_*blo 5 xpath

如何在下面的表达式中制作ba选择?

//td[@class='ttr_interest']/b/a/text()
Run Code Online (Sandbox Code Playgroud)

基本上/b/a可能存在或不存在于树中(仅存在a或仅存在b或两者都存在或不存在).一般如何指定可选元素?

我想捕捉文本封闭到td是否该文本是由另外封闭<a><b>.

按要求提供样品

<td>
<a href="#">text_to_capture</a>
</td>

<td>
<b><a href="#">text_to_capture</a></b>
</td>

<td>
text_to_capture
</td>
Run Code Online (Sandbox Code Playgroud)

Ser*_*geS 0

编辑:评论更改 xpath 以适应问题后

<bar>
  xxxx
  <foo>xxx</foo>
  <barfoo>
    <foo>xxx</foo>
  </barfoo>
</bar>
Run Code Online (Sandbox Code Playgroud)

使用这个xpath

//bar//*/text()|//bar/text()
Run Code Online (Sandbox Code Playgroud)