如何通过 XPath 选择表条目 [英] How to select table entry via XPath

查看:23
本文介绍了如何通过 XPath 选择表条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下表,我想要一个表达式来获取类别OC"的百分比.是否可以通过 XPath 提取?

I have the following table, and I wanted an expression to get the Percentage of the Category "OC". Is it possible to extract via XPath?

<tbody>
  <tr>
    <th class="textL">Category</th>
    <th class="textR">No. of Items</th>
    <th class="textR">Percentage</th>
  </tr>
  <tr class="data_row">
    <td>OC</td>
    <td class="textR">100</td>
    <td class="textR">4.70</td>
  </tr>
  <tr class="data_row">
    <td>FP</td>
    <td class="textR">200</td>
    <td class="textR">38.82</td>
  </tr>
  <tr class="data_row">
    <td>FI</td>
    <td class="textR">300</td>
    <td class="textR">20.39</td>
  </tr>
</tbody>

推荐答案

根据另一个条目的值选择表条目

要为给定的OC"类别选择百分比:

Selecting table entry based on value of another entry

To select the Percentage for the given "OC" Category:

//td[.='OC']/following-sibling::td[count(../..//th[.='Percentage']/preceding-sibling::th)]/text()

上面的XPath会返回

The above XPath will return

"4.70"

根据要求.

请注意,只要目标列继续命名为百分比"并保留在第一列中的类别列之后,它就会在面临许多更改(包括行和列重新排列)时继续工作.甚至可以通过取两列位置的差异而不是假设 Category 是第一列来进一步概括该表达式.

Note that it will continue to work in the face of many changes, including row and column rearrangements as long as the targeted column continues to be named "Percentage" and remains after the Category column in the first column. One could even further generalize the expression by taking the difference of the positions of the two columns rather than assuming that Category is the first column.

说明:从包含OC"的td中,遍历等于百分比"列标题位置的兄弟数量,然后选择文本在正确的兄弟 td.

Explanation: From the td that contains "OC", go over the number of siblings equal to the position of the "Percentage" column header, and there select the text in the correct sibling td.

这篇关于如何通过 XPath 选择表条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆