Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用xpath查找列的顶部_Xpath_Nokogiri - Fatal编程技术网

使用xpath查找列的顶部

使用xpath查找列的顶部,xpath,nokogiri,Xpath,Nokogiri,假设HTML表如下所示: <tr> <td> </td> <th> black </th> <th> white </th> </tr> <tr> <th> 1st </th> <td> stuff </td> <td> stuff </td> </tr> <tr> <th>

假设HTML表如下所示:

<tr> <td>     </td> <th> black </th> <th> white </th> </tr>
<tr> <th> 1st </th> <td> stuff </td> <td> stuff </td> </tr>
<tr> <th> 2nd </th> <td> earth </td> <td> stuff </td> </tr>
<tr> <th> 3rd </th> <td> stuff </td> <td> bingo </td> </tr>
(你和Nokogiri就是这么做的。)

那么,采用@cell并使用它查找包含此单元格的列顶部的标题的规范方法是什么


也就是说,将“宾果”转换为“白色”和将“地球”转换为“黑色”的标准方式是什么?

对于规范化的表,这是来自任何
td
th
“单元格”的相对XPath表达式:


我不知道
position()mod count(../*)=0与查找正确的列有什么关系。@LumpN:对于一个R x C规格化的表,任何单元格与其对应单元格之间的距离都是C x N。C将是count(../*),因为它是一个规范化的表
position()
是前面的
轴中的距离。给你,谢谢亚历杭德罗。normalized是什么意思?@steven_noble:“normalized”的意思是既没有colspan也没有rowspan。@Alejandro:对于这个特定的例子,不需要最后一个子句--[last()]。我想知道您是否有一个示例显示何时需要它?请注意,如果在第一行周围使用
,这将更容易,而且在语义上也会更好。(假设您可以控制标记。)
@cell = @table.xpath('.//td[contains(text(), "bingo")]')
preceding::*[
   self::td|self::th
][
   position() mod count(../*) = 0
][
   last()
]