简体   繁体   English

XPATH-Ruby-Nokogiri-节点集

[英]XPATH - Ruby - Nokogiri - Nodeset

I have a NodeSet of a table that looks similar to this: 我有一个表的NodeSet,看起来与此类似:

<table cellpadding="1" cellspacing="0" width="100%" border="0">
  <tr>
      <td colspan="9" class="csoGreen"><b class="white">Bill Statement Detail</b></td>
  </tr>
  <tr>
      <td><b>Bill Date</b></td>
      <td"><b>Bill Amount</b></td>

      <td"><b>Bill Due Date</b></td>
      <td"><b>Bill (PDF)</b></td>
  </tr>

<tr vAlign="top">
  <td>blahA</td>
  <td>blahB</td>
  <td>blahC</td>

  <td><a href="javascript: void(0);" onclick="javascript:window.open('/cso/displaypdfbill?selectedBillkey=447403730','_blank');">View Bill</a></td>
</tr>

Now I plan on looping through each onclick in the table. 现在,我计划遍历表中的每个onclick。

I've been attempting to loop through the NodeSet unsuccessfully. 我一直在尝试不成功地遍历NodeSet。

I ended up with many failed attempts, but I imagine it would end up looking something like this: 我以许多失败的尝试而告终,但我想它最终看起来像这样:

doc_list.each_element ("//a[td/text()='onclick']/@href") do |  |
      #here I want to scan and save BlahA into a Variable 
end

You want to iterate through everything with an onclick? 您想通过onclick遍历所有内容吗? Maybe: 也许:

doc.css('*[onclick]').each do |el|
    puts el[:onclick]
end

Edit: what you probably really want is the first td of every row starting with the row 3. in that case: 编辑:可能真正想要的是从第3行开始的每一行的第一个td。在这种情况下:

table.css('td[1]')[2..-1].each do |td|
    puts td.text
end

The key to doing this efficiently is not in your question, but in your comment "I want to extract the first td in the tr where there is an onclick". 有效执行此操作的关键不在您的问题中,而是在您的注释“我要提取存在onclick的tr中的第一个td”中。

This expression does exactly that: 该表达式正是这样做的:

doc.xpath('//tr[td/a/@onclick]/td[1]/text()')

In fact this will give you the set of all such matches. 实际上,这将为您提供所有此类匹配项的集合。 No iteration needed. 无需迭代。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM