title: ruby libxml : Where does returned node value come from?
body: <p>When parsing a XML document in Ruby with libxml, I receive too much data from a find XPath call.</p>
  
  <p>My test data is:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" standalone="yes"?&gt;
  &lt;MAIN&gt;
    &lt;EPS&gt;
      &lt;EP ID="EDM01"&gt;EP 1
        &lt;BP ID="EDM01_BP1"&gt;BP1 for EP1
          &lt;Activities&gt;
            &lt;Activity ID="1"&gt;Activity 1 for EDM01_BP1&lt;/Activity&gt;
            &lt;Activity ID="2"&gt;Activity 2 for EDM01_BP1&lt;/Activity&gt;
            &lt;Activity ID="3"&gt;Activity 3 for EDM01_BP1&lt;/Activity&gt;
          &lt;/Activities&gt;
        &lt;/BP&gt;
        &lt;BP ID="EDM01_BP2"&gt;BP2 for EP1
           &lt;Activities&gt;
             &lt;Activity ID="1"&gt;Activity 1 for EDM01_BP2&lt;/Activity&gt;
             &lt;Activity ID="2"&gt;Activity 2 for EDM01_BP2&lt;/Activity&gt;
             &lt;Activity ID="3"&gt;Activity 3 for EDM01_BP2&lt;/Activity&gt;
           &lt;/Activities&gt;
        &lt;/BP&gt;
      &lt;/EP&gt;
      &lt;EP ID="APO01"&gt;EP 2
        &lt;BP ID="APO01_BP1"&gt;BP 1 for EP2
          &lt;Activities&gt;
            &lt;Activity ID="1"&gt;Activity 1 for APO01_BP1&lt;/Activity&gt;
            &lt;Activity ID="2"&gt;Activity 2 for APO01_BP1&lt;/Activity&gt;
            &lt;Activity ID="3"&gt;Activity 3 for APO01_BP1&lt;/Activity&gt;
            &lt;Activity ID="4"&gt;Activity 4 for APO01_BP1&lt;/Activity&gt;
            &lt;Activity ID="5"&gt;Activity 5 for APO01_BP1&lt;/Activity&gt;
          &lt;/Activities&gt;
        &lt;/BP&gt;
      &lt;/EP&gt;
    &lt;/EPS&gt;
  &lt;/MAIN&gt;
  </code></pre>
  
  <p>And I parse it with:  </p>
  
  <pre><code>xmlparser = XML::Parser.string(@strXML,:encoding =&gt; XML::Encoding::UTF_8)
  @xmlDoc = xmlparser.parse
  @projects = nil
  project = nil
  cl = @xmlDoc.find('/MAIN')
  unless (cl.empty?)
    puts ""
    @projects = @xmlDoc.find('//EP [@ID]')
    @projects.each do |p|
      puts('&lt;----------1---------&gt;')
      puts(p.inner_xml)
      bps = p.find('//BP [@ID]')
      bps.each do |bp|
        puts('&lt;----------2---------&gt;')
        puts(bp.inner_xml)
        puts('&lt;---- Activities ----&gt;')
        acts = bp.find('//Activity [@ID]')
        acts.each do |act|
          puts('ActID&gt; ' + act['ID'].to_s)
          puts(act.first.content.to_s)
        end
      end
    end
  end
  assert true
  end
  </code></pre>
  
  <p>when looking at the displayed results, it shows that the fetched xml::node is correct (p.inner_xml)</p>
  
  <pre><code>&lt;----------1---------&gt;
  EP 1      &lt;BP ID="EDM01_BP1"&gt;BP1 for EP1&lt;Activities&gt;&lt;Activity ID="1"&gt;Activity 1 for EDM01_BP1&lt;/Activity&gt;&lt;Activity ID="2"&gt;Activity 2 for EDM01_BP1&lt;/Activity&gt;&lt;Activity ID="3"&gt;Activity 3 for EDM01_BP1&lt;/Activity&gt; &lt;/Activities&gt;&lt;/BP&gt;&lt;BP ID="EDM01_BP2"&gt;BP2 for EP1&lt;Activities&gt;&lt;Activity ID="1"&gt;Activity 1 for EDM01_BP2&lt;/Activity&gt;&lt;Activity ID="2"&gt;Activity 2 for EDM01_BP2&lt;/Activity&gt;&lt;Activity ID="3"&gt;Activity 3 for EDM01_BP2&lt;/Activity&gt;         &lt;/Activities&gt;&lt;/BP&gt;  
  &lt;----------2---------&gt;
  BP1 for EP1&lt;Activities&gt;&lt;Activity ID="1"&gt;Activity 1 for EDM01_BP1&lt;/Activity&gt;          &lt;Activity ID="2"&gt;Activity 2 for EDM01_BP1&lt;/Activity&gt;&lt;Activity ID="3"&gt;Activity 3 for EDM01_BP1&lt;/Activity&gt;&lt;/Activities&gt;
  &lt;---- Activities ----&gt;  
  ActID&gt; 1  
  Activity 1 for EDM01_BP1  
  ActID&gt; 2  
  Activity 2 for EDM01_BP1  
  ActID&gt; 3  
  Activity 3 for EDM01_BP1  
  ActID&gt; 1  
  Activity 1 for EDM01_BP2  
  ActID&gt; 2  
  Activity 2 for EDM01_BP2  
  ActID&gt; 3  
  Activity 3 for EDM01_BP2  
  ActID&gt; 1  
  Activity 1 for APO01_BP1  
  ActID&gt; 2  
  Activity 2 for APO01_BP1  
  ActID&gt; 3  
  Activity 3 for APO01_BP1  
  ActID&gt; 4  
  Activity 4 for APO01_BP1  
  ActID&gt; 5  
  Activity 5 for APO01_BP1  
  </code></pre>
  
  <p>As you can see has the first XML node that is inspected only 3 activities.
  But the program displays all activities from the complete xml doc. Not just from the fetched node.</p>
  
  <p>Is it a wrong assumption that when doing a xmldoc.find() and traversing it with<br>
      nodes.each do |n| 
  the n variable is a libXML::XML::Node that is a subset from the xml document?
  How is it otherwise possible to reference data (activities APOxxx) that is not part of the fetched node?</p>