在Nokogiri中,如何找到文档中某个节点之前的所有节点?

时间:2022-06-01 21:06:38

Using Rails 5, Ruby 2.4. If I have located a node using Nokogiri parsing, how would I find all the nodes that occur before my found node that do not also include that found node? That is, let's say my document is

使用Rails 5,Ruby 2.4。如果我使用Nokogiri解析找到了一个节点,我如何找到在找到的节点之前发生的所有节点,这些节点还没有包含找到的节点?也就是说,让我说我的文件是

<outer>
    <p>Hello</p>
    <inner>
        <most_inner class="abc">Howdy</most_inner>
        <most_inner class="def">Next</most_inner>
    </inner>
</outer>

and I run a query like

我运行一个查询

node = doc.search('//*[contains(@class, "def")]').first

How would I locate all the preceding nodes (that don't include the one I just identified)? The nodes I would expect would be

我如何找到所有前面的节点(不包括我刚刚确定的节点)?我期望的节点将是

<p>Hello</p>
<most_inner>Howdy</most_inner>

1 个解决方案

#1


4  

You just need to iterate over the leaf nodes until you reach the target node.

您只需迭代叶节点,直到到达目标节点。

# Node to exclude
node = doc.search('//*[contains(@class, "def")]').first
preceding_nodes = []

# Find all leaf nodes
leaf_nodes = doc.xpath("//*[not(child::*)]")

leaf_nodes.each do |leaf|
  if leaf == node
    break
  else
    preceding_nodes.push(leaf)
  end
end

preceding_nodes # => Contains all preceding leaf nodes

#1


4  

You just need to iterate over the leaf nodes until you reach the target node.

您只需迭代叶节点,直到到达目标节点。

# Node to exclude
node = doc.search('//*[contains(@class, "def")]').first
preceding_nodes = []

# Find all leaf nodes
leaf_nodes = doc.xpath("//*[not(child::*)]")

leaf_nodes.each do |leaf|
  if leaf == node
    break
  else
    preceding_nodes.push(leaf)
  end
end

preceding_nodes # => Contains all preceding leaf nodes