如何获取xpath中的最后一个table?
How to get the last table in xpath?
所以我有以下情况:
<table class="table-main detail-odds sortable">
..
</table>
<table class="table-main detail-odds sortable">
..
</table>
如你所见,我有两个 table 具有相同的 类,我想得到最后一个 table(我不能使用索引,因为 table 改变)。
目前我有这个代码:
HtmlNode oddsTable = doc.DocumentNode
.SelectNodes("//table[@class='table-main detail-odds sortable']");
不幸的是我找不到任何 .Last()
方法,也许可以直接用 xpath
做这个而不用 SelectNodes()
?
您可以使用last()
作为索引
"(//table[@class='table-main detail-odds sortable'])[last()]"
务必将表达式括在括号中。
last()
将 return 你最后一个 table 只有当两个 table 是同一个 parent 的 children 时。所以如果HTML真的像
<table class="table-main detail-odds sortable">
..
</table>
<table class="table-main detail-odds sortable">
..
</table>
然后
//table[@class='table-main detail-odds sortable'][last()]
将获取所需的 table...
但万一
<div>
<table class="table-main detail-odds sortable">
..
</table>
</div>
<div>
<table class="table-main detail-odds sortable">
..
</table>
</div>
您可能需要
(//table[@class='table-main detail-odds sortable'])[count(//table[@class='table-main detail-odds sortable'])]
所以我有以下情况:
<table class="table-main detail-odds sortable">
..
</table>
<table class="table-main detail-odds sortable">
..
</table>
如你所见,我有两个 table 具有相同的 类,我想得到最后一个 table(我不能使用索引,因为 table 改变)。
目前我有这个代码:
HtmlNode oddsTable = doc.DocumentNode
.SelectNodes("//table[@class='table-main detail-odds sortable']");
不幸的是我找不到任何 .Last()
方法,也许可以直接用 xpath
做这个而不用 SelectNodes()
?
您可以使用last()
作为索引
"(//table[@class='table-main detail-odds sortable'])[last()]"
务必将表达式括在括号中。
last()
将 return 你最后一个 table 只有当两个 table 是同一个 parent 的 children 时。所以如果HTML真的像
<table class="table-main detail-odds sortable">
..
</table>
<table class="table-main detail-odds sortable">
..
</table>
然后
//table[@class='table-main detail-odds sortable'][last()]
将获取所需的 table...
但万一
<div>
<table class="table-main detail-odds sortable">
..
</table>
</div>
<div>
<table class="table-main detail-odds sortable">
..
</table>
</div>
您可能需要
(//table[@class='table-main detail-odds sortable'])[count(//table[@class='table-main detail-odds sortable'])]