title: Python / lxml: Nested for loops
body: <p>I've got some XML that I'm trying to parse. Example:</p>
  
  <pre><code>&lt;TVAMain&gt;
      &lt;ProgramDescription&gt;
          &lt;ProgramLocationTable&gt;
              &lt;Schedule value1="1234"&gt;
                  &lt;ScheduleEvent&gt;
                      &lt;Program value2="1234567890" /&gt;
                  &lt;/ScheduleEvent&gt;
                  &lt;ScheduleEvent&gt;
                      &lt;Program value2="1234567891" /&gt;
                  &lt;/ScheduleEvent&gt;
              &lt;/Schedule&gt;
              &lt;Schedule value1="5678"&gt;
                  &lt;ScheduleEvent&gt;
                      &lt;Program value2="1234567892" /&gt;
                  &lt;/ScheduleEvent&gt;
                  &lt;ScheduleEvent&gt;
                      &lt;Program value2="1234567893" /&gt;
                  &lt;/ScheduleEvent&gt;
              &lt;/Schedule&gt;
          &lt;/ProgramLocationTable&gt;
      &lt;/ProgramDescription&gt;
  &lt;/TVAMain&gt;
  </code></pre>
  
  <p>I am trying to retrieve all entries of value1, and all entries of value2, and output them in a file as value1|value2.  I can successfully get value 1 or value 2 to write out to a file, but I can't get them at the same time!</p>
  
  <p>This is my code so far (I've ditched the writing step for the time being, just trying to get it to print out both bits of data first):</p>
  
  <pre><code>from lxml import etree
  
  parser = lxml.etree.XMLParser()
  
  
  tree = lxml.etree.parse(file_name, parser)
  root = tree.getroot()
  
  nsmap = {'xmlns': 'urn:tva:metadata:2010'} 
  
  with codecs.open(file_name+'.log', mode='w', encoding='utf-8') as f:
      for info in root.xpath('//xmlns:Schedule', namespaces=nsmap):
           value1 = (info.get('value1'))
           print (serviceid)
           for info in root.xpath('//xmlns:Schedule[@value1 = "value1"]/ScheduleEvent/Program', namespaces=nsmap):
                 value2 = (info.get('value2'))
                 print (crid)
  </code></pre>
  
  <p>This code will successfully print all of the 'value1' values but no value2.</p>
  
  <p>I have tried the following:
   - using 'info2' in the second for loop
   - playing around with the second xpath, entering known values for value1</p>
  
  <p>Can someone point me in the right direction?</p>