title: How to bind XML attributes and all of their ancestors with xpath and lxml?
body: <p>I have a XML file similar to the sample below. I was tasked to query that file with xpaths and lxml, and to store them in a database via Python and Django. </p>
  
  <pre><code>&lt;foo n='foo56'&gt;
    &lt;moo n='moo254'&gt;
      &lt;too n='too24' =&gt;
        &lt;bar n='bar3'&gt;
      &lt;mar n='mar1'
           x = '1;2;3;4;5;6'
           y = '7;8;9;10;11;12'
           z = '13;14;15;16'
          /&gt;
      &lt;mar n='mar2'
           x = '3;2;8;4;9;6'
           y = '7;8;7;14;13;12'
           z = '5;11;16;10'
          /&gt;
        &lt;/bar&gt;    
        &lt;bar n='bar56'&gt;
      &lt;mar n='mar1'
           x = '1;2;3;4;5;6'
           y = '7;8;9;10;11;12'
           z = '13;14;15;16'
          /&gt;
      &lt;mar n='mar2'
           x = '3;2;8;4;9;6'
           y = '7;8;7;14;13;12'
           z = '5;11;16;10'
          /&gt;
        &lt;/bar&gt;    
        &lt;bar n='barX'&gt;
      &lt;mar n='mar1'
           x = values x
           y = values y
           z = values z
          /&gt;
      &lt;mar n='mar2'
           x = values x
           y = values y
           z = values z
          /&gt;  
        &lt;/bar&gt;    
       &lt;/too&gt;
     &lt;/moo&gt;
  &lt;/foo&gt;
  </code></pre>
  
  <p>I need to retrieve the x, y, and z values, and place these in a Container python 
  object, these Containers will be linked by foreign key to another python object
  we'll call a Node. </p>
  
  <p>On top of retrieving these data, I'll need to associate to each Node several Containers object in which I'll have to put the 'n' value of a XML element, associated with the tag of that XML element.</p>
  
  <p>Here is a simplification of my data model :</p>
  
  <p>Node : 
  name : link
  id : link_id</p>
  
  <p>Container :
  key : x
  value : '1;2;3;4;5;6'
  node : link_id</p>
  
  <p>Container :
  key : y
  value : values y
  node : link_id</p>
  
  <p>Container :
  key : z
  value : values z
  node : link_id</p>
  
  <p>Container :
  key : mar
  value : mar1
  node : link_id</p>
  
  <p>Container :
  key : bar
  value : bar3
  node : link_id</p>
  
  <p>I must use one xpath to retrieve the data of each container, I can't use more
  or can't use less.</p>
  
  <p>I already have a parser which can handle XML files, and parse them based on
  pre-writen Xpaths (but that can't register these in my database yet, since data are not sorted at all)</p>
  
  <p>My problem remains, I don't know how to get the relationship between the elements and attributes after applying the xpath. Thus, I can't save in my database any data I retrieve with the xpath, otherwise I'll have useless x, y and z values without knowing to what these values refer. </p>
  
  <p>To sum up :</p>
  
  <p><strong>Is there a way to know that x, y, and z belongs to mar1, which
  belongs to bar3 ?</strong> </p>
  
  <p><strong>Is it something that can be done in Python, with lxml and xpath to retrieve some data or all data and associate these just like in my example with Node and Containers ?</strong></p>
  
  <p>Thanks for reading.</p>
  
  <p>EDIT 24/06/2016 :</p>
  
  <p>Here is a sample of my XML file, and an expected output :</p>
  
  <pre><code>   &lt;treatment name="testing"&gt;      
       &lt;process  name="running_test"&gt;       
        &lt;measure name="memory_user_1"&gt;
         &lt;values name="Amount" 
                    x="6" 
                    y=".25"  
                    z="3.00" 
          /&gt; 
        &lt;/measure&gt; 
        &lt;measure name="memory_user_2"&gt; 
         &lt;values name="Amount" 
                    x="3" 
                    y=".14" 
                    z="1.68" 
          /&gt; 
        &lt;/measure&gt; 
       &lt;/process&gt;
     &lt;/treatment&gt;
  </code></pre>
  
  <p>After parsing such XML I'd like to retrieve something like :</p>
  
  <pre><code>[
    {treatment : 'testing', process : 'running_test', measure : 'memory_user_1' , values : 'amount', x : '6', y : '.25', z : '3.00'},
    {treatment : 'testing', process : 'running_test', measure : 'memory_user_2' , values : 'amount', x : '3', y : '.14', z : '1.68'}
  ]
  </code></pre>