【问题标题】:How to get the cell above in a table using XPath?如何使用 XPath 在表格中获取上面的单元格?
【发布时间】:2017-08-09 15:46:05
【问题描述】:

我正在尝试将有关每个单元格的所有信息放在一个表格中的一行中。我需要弄清楚如何打印表格中每一列的标题。

td, table {
  border: 2px black solid;
}
<table>
  <tr>
    <td>a1</td>
    <td>a2</td>
    <td>a3</td>
    <td>a4</td>
  </tr>
  <tr>
    <td>b1</td>
    <td>b2</td>
    <td>b3</td>
    <td>b4</td>
  </tr>
  <tr>
    <td>c1</td>
    <td>c2</td>
    <td>c3</td>
    <td>c4</td>
  </tr>
  <tr>
    <td>d1</td>
    <td>d2</td>
    <td>d3</td>
    <td>d4</td>
  </tr>
</table>
Table 1
+----+----+----+----+
| a1 | a2 | a3 | a4 |
+----+----+----+----+
| b1 | b2 | b3 | b4 |
+----+----+----+----+
| c1 | c2 | c3 | c4 |
+----+----+----+----+
| d1 | d2 | d3 | d4 |
+----+----+----+----+

Table 2
+----+----+----+----+
| e1 | e2 | e3 | e4 |
+----+----+----+----+
| f1 | f2 | f3 | f4 |
+----+----+----+----+
| g1 | g2 | g3 | g4 |
+----+----+----+----+
| h1 | h2 | h3 | h4 |
+----+----+----+----+

And Other Tables ...

我想打印列顶部的单元格(即 tr[1])。

输出不应该有第一个 raw ..

第一个输出应该是:

单元格 b1 有标题 a1

..

单元格g2有标题e2

等等..

我正在使用 xidel:

xidel $site -e "//tr[position()&gt;1]/td/concat('The cell ', ., $codeX)"

$codeX 的值应该是多少?

谢谢,

【问题讨论】:

  • 如果您想使用第一行,那么听起来好像使用//tr[1]/td 就是您想要的
  • @MartinHonnen 我想从第二行获取它,实际上我正在使用 concat() 将它们打印在一起。

标签: xpath xidel


【解决方案1】:

您只能使用 xpath 来获取它:

//table//tr[1]/td[count(//table//td[text()='${cellValue}']/preceding-sibling::*) + number(boolean(//table//td[text()='${cellValue}']/preceding-sibling::*))]

注意: 指定包含现有值的单元格(例如'b3')会从标题('a3')中给出正确的单元格。 如果您尝试搜索单元格的无效值,则会收到正确的空值,因为标题中的单元格不存在。

【讨论】:

    【解决方案2】:

    Xidel 支持 XQuery 3.0,因此对于构建任务,我建议例如

    let $rows := //tr,
        $header-cells := $rows[1]/td
    for $data-row in $rows[position() gt 1]
    for $cell at $pos in $data-row/td
    return $cell!('cell ' || . || ' has header ' || $header-cells[$pos])
    

    不确定从命令行中是否可以正常工作,但可以完成工作。

    【讨论】:

    • 这可以更简单:for $row in //tr[position()&gt;1] for $cell at $i in $row/td return concat('cell ',$cell,' has header ',//tr[1]/td[$i]).
    【解决方案3】:

    要获取表头 tex,如果标签 th 用于表头(这是预期的),则不要只获取第一个 tr 数据 //tr[1]/td//tr[1]/th

    按列文本获取标题在该表上尝试此 XPath:https://www.w3schools.com/css/tryit.asp?filename=trycss_table_border

    //th[count(//tr/td[text()='Griffin'])]
    

    逻辑是:通过count()函数找到td的位置,具体文本//tr/td[text()='Griffin']。在这个位置找到th

    【讨论】:

    • 我尝试使用 '../../tr[1]/td[count(./preceding-sibling::td)]' 来执行此操作,但没有成功
    • 你试过我的建议了吗?因为您的定位器 100% 不正确
    • 我应该如何在 td 元素中找到文本?实际上我有很多表格和很多单元格,而且我的帖子过于简化了。
    • 其实我已经修改了我的问题。我希望它更容易理解。感谢您的帮助。
    • 请在 th 标签或 td 内添加表格 html、标题?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-06
    • 2022-01-26
    • 1970-01-01
    相关资源
    最近更新 更多