The Problem
I am running some statistics against various URLS. I want to find the top level element with the most concentrated number of children. The method that I would like to follow is to identify all top level elements and then determine what percentage of all the elements on the page belong to it.
Goal
- Recursively get all children of a given element.
Inputs: a Nokogiri Element
Outputs: an array of Nokogiri Elements OR the count of total number of children
Setup
- Ruby 1.9.2
- Nokogiri gem
What I ended up coming up with (this works, but isn't as pretty as my chosen answer below)
getChildCount(elem)
children = elem.children
return 0 unless children and children.count > 0
child_count = children.count
children.each do |child|
child_count += getChildCount(child)
end
child_count
end
the traverse method yields the current node and all children to a block, recursively.