How to specify Xpath that return whole table without last row?

半世苍凉 提交于 2020-04-18 05:48:26

问题


Here is the code of a table. I need to extract whole table without last row.

Whole table:

 <table class="product-content__table">
    <tr><th class="product-content__th">Состав</th><td>нержавеющая сталь, натуральная кожа </td></tr>
    <tr><th class="product-content__th">Ширина</th><td>2 см</td></tr><tr><th class="product-content__th">Цвет</th><td>серый </td></tr>
    <tr><th class="product-content__th">Страна производства</th><td>Россия </td></tr><tr><th class="product-content__th">Сезон</th><td>Мульти </td></tr>
    <tr><th class="product-content__th">Коллекция</th><td>Весна-лето </td></tr>
    <tr><th class="product-content__th">Артикул</th><td itemprop="sku">RO003DMCMA98</td></tr>
    </table>

I need to extract whole table without this row:

<tr><th class="product-content__th">Артикул</th><td itemprop="sku">RO003DMCMA98</td></tr>

回答1:


<td> is sibling of <th> not child so you don't actually need th in your xpath. And you want to filter out the last tr within the same table instead of filtering out the last td within the same tr :

//table[@class="product-content__table"]//tr[position() < last()]/td

remove trailing /td if you want to get list of <tr> instead of <td>.




回答2:


I need all tags including table tag.

XPath can only select nodes that are present in your input. If there is a table element in your input with five rows, and you want a table element with four rows, then there is no such table element in your input so you cannot select it with XPath. If you want to get a node that differs from any node in your input, you need XSLT or XQuery.




回答3:


This works:

//table//tr[position()<last()]


来源:https://stackoverflow.com/questions/29861062/how-to-specify-xpath-that-return-whole-table-without-last-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!