title: Pythonic way to get a XML tree element in the same subtree with another element
body: <p>Is there a more elegant Pythonic way to get certain elements from xml tree that are in the same subtree than iterating with nested loops and ifs?</p>
  
  <p>I.e. in pseudo SQL </p>
  
  <pre><code>select UsageStatistic/PageViews/PerUser/Value from Tree where UsageStatistic/TimeRange/Days=7 
  </code></pre>
  
  <p>Here is a well formed subset of the XML response from Alexa Amazon AWIS:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;aws:UrlInfoResponse xmlns:aws="http://alexa.amazonaws.com/doc/2005-10-05/"&gt;&lt;Response&gt;&lt;OperationRequest&gt;&lt;RequestId&gt;dsfadf&lt;/RequestId&gt;&lt;/OperationRequest&gt;&lt;UrlInfoResult&gt;&lt;Alexa&gt;
  &lt;TrafficData&gt;
  &lt;DataUrl type="canonical"&gt;yahoo.com&lt;/DataUrl&gt;
  &lt;UsageStatistics&gt;
  
  &lt;UsageStatistic&gt;
  &lt;TimeRange&gt;
  &lt;Days&gt;7&lt;/Days&gt;
  &lt;/TimeRange&gt;
  &lt;Rank&gt;
  &lt;Value&gt;5&lt;/Value&gt;
  &lt;Delta&gt;0&lt;/Delta&gt;
  &lt;/Rank&gt;
  &lt;Reach&gt;
  &lt;Rank&gt;
  &lt;Value&gt;5&lt;/Value&gt;
  &lt;Delta&gt;0&lt;/Delta&gt;
  &lt;/Rank&gt;
  &lt;PerMillion&gt;
  &lt;Value&gt;111,200&lt;/Value&gt;
  &lt;Delta&gt;-0.49%&lt;/Delta&gt;
  &lt;/PerMillion&gt;
  &lt;/Reach&gt;
  &lt;PageViews&gt;
  &lt;PerMillion&gt;
  &lt;Value&gt;11,442&lt;/Value&gt;
  &lt;Delta&gt;-1.71%&lt;/Delta&gt;
  &lt;/PerMillion&gt;
  &lt;Rank&gt;
  &lt;Value&gt;7&lt;/Value&gt;
  &lt;Delta&gt;1&lt;/Delta&gt;
  &lt;/Rank&gt;
  &lt;PerUser&gt;
  &lt;Value&gt;6.42&lt;/Value&gt;
  &lt;Delta&gt;-1.20%&lt;/Delta&gt;
  &lt;/PerUser&gt;
  &lt;/PageViews&gt;
  &lt;/UsageStatistic&gt;
  
  &lt;UsageStatistic&gt;
  &lt;TimeRange&gt;
  &lt;Days&gt;3&lt;/Days&gt;
  &lt;/TimeRange&gt;
  &lt;Rank&gt;
  &lt;Value&gt;5&lt;/Value&gt;
  &lt;Delta&gt;0&lt;/Delta&gt;
  &lt;/Rank&gt;
  &lt;Reach&gt;
  &lt;Rank&gt;
  &lt;Value&gt;5&lt;/Value&gt;
  &lt;Delta&gt;0&lt;/Delta&gt;
  &lt;/Rank&gt;
  &lt;PerMillion&gt;
  &lt;Value&gt;112,130&lt;/Value&gt;
  &lt;Delta&gt;-14.85%&lt;/Delta&gt;
  &lt;/PerMillion&gt;
  &lt;/Reach&gt;
  &lt;PageViews&gt;
  &lt;PerMillion&gt;
  &lt;Value&gt;11,314&lt;/Value&gt;
  &lt;Delta&gt;-13.39%&lt;/Delta&gt;
  &lt;/PerMillion&gt;
  &lt;Rank&gt;
  &lt;Value&gt;6&lt;/Value&gt;
  &lt;Delta&gt;0&lt;/Delta&gt;
  &lt;/Rank&gt;
  &lt;PerUser&gt;
  &lt;Value&gt;7.99&lt;/Value&gt;
  &lt;Delta&gt;+1.4%&lt;/Delta&gt;
  &lt;/PerUser&gt;
  &lt;/PageViews&gt;
  &lt;/UsageStatistic&gt;
  
  &lt;UsageStatistic&gt;
  &lt;TimeRange&gt;
  &lt;Months&gt;3&lt;/Months&gt;
  &lt;/TimeRange&gt;
  &lt;Rank&gt;
  &lt;Value&gt;5&lt;/Value&gt;
  &lt;Delta&gt;0&lt;/Delta&gt;
  &lt;/Rank&gt;
  &lt;Reach&gt;
  &lt;Rank&gt;
  &lt;Value&gt;5&lt;/Value&gt;
  &lt;Delta&gt;0&lt;/Delta&gt;
  &lt;/Rank&gt;
  &lt;PerMillion&gt;
  &lt;Value&gt;112,130&lt;/Value&gt;
  &lt;Delta&gt;-14.85%&lt;/Delta&gt;
  &lt;/PerMillion&gt;
  &lt;/Reach&gt;
  &lt;PageViews&gt;
  &lt;PerMillion&gt;
  &lt;Value&gt;11,314&lt;/Value&gt;
  &lt;Delta&gt;-13.39%&lt;/Delta&gt;
  &lt;/PerMillion&gt;
  &lt;Rank&gt;
  &lt;Value&gt;6&lt;/Value&gt;
  &lt;Delta&gt;0&lt;/Delta&gt;
  &lt;/Rank&gt;
  &lt;PerUser&gt;
  &lt;Value&gt;6.99&lt;/Value&gt;
  &lt;Delta&gt;+1.6%&lt;/Delta&gt;
  &lt;/PerUser&gt;
  &lt;/PageViews&gt;
  &lt;/UsageStatistic&gt;
  
  &lt;/UsageStatistics&gt;
  &lt;/TrafficData&gt;
  &lt;/Alexa&gt;&lt;/UrlInfoResult&gt;&lt;aws:ResponseStatus&gt;&lt;aws:StatusCode&gt;Success&lt;/aws:StatusCode&gt;&lt;/aws:ResponseStatus&gt;&lt;/Response&gt;&lt;/aws:UrlInfoResponse&gt;
  </code></pre>
  
  <p>Here is the code I got so far. It reads in the above response XML file as alexa_response.xml</p>
  
  <pre><code>import xml.etree.ElementTree as ET
  prefix = "aws"
  uri = "http://alexa.amazonaws.com/doc/2005-10-05/"
  ET.register_namespace(prefix, uri)
  tree = ET.parse('alexa_response.xml')
  root = tree.getroot()
  for a in root.iter("UsageStatistic"):
      for b in a:
          if b.tag == 'TimeRange':
              for c in b: 
                  print c.tag, c.text
          if b.tag == 'PageViews':
              for d in b: 
                  if d.tag == 'PerUser':
                      for f in d:
                          if f.tag == 'Value':
                              print f.tag, f.text
      print
  </code></pre>
  
  <p>Result:</p>
  
  <pre><code>Days 7
  Value 6.42
  
  Months 3
  Value 6.99
  </code></pre>
  
  <p>I only need </p>
  
  <pre><code>Days 7 
  Value 6.42
  </code></pre>
  
  <p>That is the PageViews/PerUser/Value/6.42 from the same subtree where the TimeRange/Days/7 is located.</p>
  
  <p>And I wonder if there is a better way then iterate through with a number of nested loops and ifs?</p>