title: Nokogiri recursively get all children
body: <h2>The Problem</h2>
  
  <p>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.</p>
  
  <h2>Goal</h2>
  
  <ul>
  <li>Recursively get all children of a given element.</li>
  </ul>
  
  <p>Inputs: a Nokogiri Element</p>
  
  <p>Outputs: an array of Nokogiri Elements OR the count of total number of children</p>
  
  <h2>Setup</h2>
  
  <ul>
  <li>Ruby 1.9.2</li>
  <li>Nokogiri gem</li>
  </ul>
  
  <h2>What I ended up coming up with (this works, but isn't as pretty as my chosen answer below)</h2>
  
  <pre><code>getChildCount(elem)
      children = elem.children
      return 0 unless children and children.count &gt; 0
      child_count = children.count
      children.each do |child|
          child_count += getChildCount(child)
      end
      child_count
  end
  </code></pre>