title: elementtree: get the contents of specific tags in xml document
body: <p>I am trying to extract the contents of specific tags in an XML file.</p>
  
  <p>Sample XML:</p>
  
  <pre><code>&lt;facts&gt;
          &lt;fact&gt;
              &lt;name&gt;crash&lt;/name&gt;
              &lt;full_name&gt;Crash&lt;/full_name&gt;
              &lt;variables&gt;
                  &lt;variable&gt;
                      &lt;name&gt;id&lt;/name&gt;
                      &lt;proper_name&gt;Crash Instance&lt;/proper_name&gt;
                      &lt;type&gt;INT&lt;/type&gt;
                      &lt;interpretation&gt;key&lt;/interpretation&gt;
                  &lt;/variable&gt;
                  &lt;variable&gt;
                      &lt;name&gt;accident_key&lt;/name&gt;
                      &lt;proper_name&gt;Case Identifier&lt;/proper_name&gt;
                      &lt;interpretation&gt;string&lt;/interpretation&gt;
                      &lt;type&gt;CHAR(9)&lt;/type&gt;
                  &lt;/variable&gt;
                  &lt;variable&gt;
                      &lt;name&gt;accident_year&lt;/name&gt;
                      &lt;proper_name&gt;Crash Year&lt;/proper_name&gt;
                      &lt;interpretation&gt;dim&lt;/interpretation&gt;
                      &lt;type&gt;INT&lt;/type&gt;
                  &lt;/variable&gt;
              &lt;/variables&gt;
          &lt;/fact&gt;
      &lt;fact&gt;
          &lt;name&gt;vehicle&lt;/name&gt;
          &lt;full_name&gt;Vehicle&lt;/full_name&gt;
          &lt;variables&gt;
              &lt;variable&gt;
                  &lt;name&gt;id&lt;/name&gt;
                  &lt;proper_name&gt;Vehicle Instance&lt;/proper_name&gt;
                  &lt;type&gt;INT&lt;/type&gt;
              &lt;/variable&gt;
              &lt;variable&gt;
                  &lt;name&gt;crash_id&lt;/name&gt;
                      &lt;proper_name&gt;Crash Instance&lt;/proper_name&gt;
                  &lt;type&gt;INT&lt;/type&gt;
              &lt;/variable&gt;
          &lt;/variables&gt;
      &lt;/fact&gt;
  &lt;/facts&gt;
  </code></pre>
  
  <p>I want to pull all of the contents of the  tag from the  nodes, but only in the Crash fact.</p>
  
  <p>Here is my code so far.</p>
  
  <pre><code>def header(filename, fact):    
      lst = []
      tree = ET.parse(filename) #read in the XML
      for fact in tree.iter(tag = 'fact'):
          factname = fact.find('name').text
          if factname == fact: #choose the fact to pull from
              for var in fact.iter(tag = 'variable'):
                  name = var.find('name').text
                  lst.append(name)
       return lst #return a list of all the &lt;name&gt; tags from the Crash fact
  
  newlst = header('schema.xml','crash')
  </code></pre>
  
  <p>My output, newlst, should be a list of all the  tags from the Crash facts. But it keeps returning empty.</p>
  
  <p>Strangely, it returns the correct output if I hard-code everything (and remove the function):</p>
  
  <pre><code>lst = []
  tree = ET.parse('schema.xml')
  for fact in tree.iter(tag = 'fact'):
      factname = fact.find('name').text
      if factname == 'crash': 
          for var in fact.iter(tag = 'variable'):
              name = var.find('name').text
              lst.append(name)
   print(lst)
  
  
   Output: ['id',
   'accident_key',
   'accident_year']
  </code></pre>