Jsoup 选择器语法未按预期工作

Jsoup Selector Syntax Not Working as Expected

以下HTML:

<table> 
 <tbody>
  <tr valign="TOP">
   <td align="LEFT"><b>Licensee Name:</b></td>
   <td align="LEFT">Some-last-name Some-first-name</td>
  </tr> 
  <tr valign="TOP">
   <td align="LEFT"><b>License Type:</b></td>
   <td align="LEFT">Optometrist (OPT)</td>
  </tr> 
.
.
.
 </tbody>
</table>

以下代码生成一个空的元素集合:

Elements rows = docOptometristDetail.select("body > table ~ tr");

但是这段代码有效:

tables = docOptometristDetail.select("body > table");
Elements rows = tables.select("tr");

我期待波浪号运算符:

table ~ tr

要查找 <table> 元素,请跳过 <tbody> 元素并构建 <tr> 元素的集合。

我是否发现了 Jsoup 的选择器语法解析器的弱点,或者我是否试图违反某些运算符优先级规则?

我试过 (body > table) ~ tr 但会抛出 SelectorParseException.

有没有一种方法可以使用单个选择器表达式进行此选择(即获得 Elements<tr> 元素的集合)?

在 CSS 中,波浪字符 ~general sibling combinator.

select或table ~ tr将尝试selecttr兄弟元素跟随table 元素。由于 table 元素和 tr 元素不能是兄弟姐妹,因此不会 selected.

理论上,select或table ~ tr会select以下tr个元素:

<table></table>
<tr></tr> <!-- These 'tr' elements are following siblings of the 'table' -->
<tr></tr> <!-- This is invalid HTML, though. -->

听起来您只需要 select 后代,因此 body > table tr 就可以了。