如何使用xpath按列标题名称选择表 [英] how to select table column by column header name with xpath
问题描述
我有一张结果表,看起来像这样:
I have a table of results and it looks like following:
<table>
<thead>
<tr>
<th>Id</th>
<th>Type</th>
<th>Amount</th>
<th>Price</th>
<th>Name</th>
<th>Expiration</th>
</tr>
</thead>
<tbody>
<tr>
<td>123</td>
<td>Paper</td>
<td>10 pcs.</td>
<td>$10</td>
<td>Premium Copier paper</td>
<td>None</td>
</tr>
<tr>
<td>321</td>
<td>Paper</td>
<td>20 pcs.</td>
<td>$20</td>
<td>Extra Copier paper</td>
<td>None</td>
</tr>
</tbody>
我想用xpath选择整个列的名称,例如我希望返回的结果是由列名称价格"选择的{<td>$10</td>, <td>$20</td>}
数组.
我是xpath的新手,不确定如何执行此操作,但是我很确定这是可能的.
And i want to select the whole column by its name with xpath e.g. i want the returned result to be an array of {<td>$10</td>, <td>$20</td>}
if selected by column name "Price".
I'm new to xpath and not really sure how to do this, but i'm pretty sure it's possible.
推荐答案
好,我已经找到了足够的答案,看起来也很优雅.这是必需的XPath字符串:
Ok, i've found the answer that would suffice and look quite elegant. Here goes the required XPath string:
//table/tbody/tr/td[count(//table/thead/tr/th[.="$columnName"]/preceding-sibling::th)+1]
输入列名,而不是$ columnName.这对我来说很好.没有XSL或其他任何东西,只有纯xpath字符串.如何应用-这是另一个问题.
Put a name of the column instead of $columnName. This works well for me. There's no XSL or anything, just pure xpath string. How to apply it - it's another question.
这篇关于如何使用xpath按列标题名称选择表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!