title: xml xpath and nesting in python
body: <p>Im trying to print out the description within <code>.//statement/statement/description</code> which would be the following statements </p>
  
  <blockquote>
    <p>" Implements a process for ensuring that organizational plans for
    conducting security testing, training, and monitoring activities
    associated with organizational information systems:" </p>
    
    <p>"Reviews testing, training, and monitoring plans for consistency with
    the organizational risk management strategy and organization-wide
    priorities for risk response actions."</p>
  </blockquote>
  
  <p>but for some reason it also drills deeper and prints out the following two statement as well </p>
  
  <blockquote>
    <p>"Are developed and maintained; and"</p>
    
    <p>"Continue to be executed in a timely manner;"</p>
  </blockquote>
  
  <p>This is the order it prints it in </p>
  
  <blockquote>
    <p>Implements a process for ensuring that organizational plans for
    conducting security testing, training, and monitoring activities
    associated with organizational information systems:</p>
    
    <p>Reviews testing, training, and monitoring plans for consistency with
    the organizational risk management strategy and organization-wide
    priorities for risk response actions.</p>
    
    <p>Are developed and maintained; and </p>
    
    <p>Continue to be executed in a timely manner;</p>
  </blockquote>
  
  <p>what should I change so that it only prints </p>
  
  <blockquote>
    <p>Implements a process for ensuring that organizational plans for
    conducting security testing, training, and monitoring activities
    associated with organizational information systems:</p>
    
    <p>Reviews testing, training, and monitoring plans for consistency with
    the organizational risk management strategy and organization-wide
    priorities for risk response actions.</p>
  </blockquote>
  
  <p>Python Code</p>
  
  <pre><code>import xml.etree.ElementTree as ET 
  import csv
  
  
  xmlFile='/Users/username/Desktop/xmlFile.xml'
  tree = ET.parse(xmlFile) 
  root = tree.getroot()
  
  # open a file for writing
  excelFile = open('/Users/username/Desktop/table2.csv', 'w')
  
  # creates the csv writer object / varible to write to csv
  csvwriter = csv.writer(excelFile)
  
  # list that contains the header
  list_head = []
  count = 0
  
  for element in root.findall('control'):
      list_nodes=[]
      if count == 0:
  
          number = element.find('number').tag
          list_head.append(number)
  
          description =element.find('.//statement/description').tag
          list_head.append(description)
  
          csvwriter.writerow(list_head)
          count = count + 1
  
      # Control number 
      number = 'Nist800-53-V4-' + element.find('number').text  
      list_nodes.append(number)
  
  
      # Control Description 
      if element.find('.//statement'):
          if element.find('.//statement/statement/') is not None:
              for descrip in element.findall('.//statement/statement/description'):
                  descrip_value = descrip.text
                  print(descrip_value)
  
      csvwriter.writerow(list_nodes)
  excelFile.close()
  
  </code></pre>
  
  <p>XML file</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;controls&gt;
    &lt;control&gt;
      &lt;family&gt;PROGRAM MANAGEMENT&lt;/family&gt;
      &lt;number&gt;PM-14&lt;/number&gt;
      &lt;title&gt;TESTING, TRAINING, AND MONITORING&lt;/title&gt;
      &lt;statement&gt;
        &lt;description&gt;The organization:&lt;/description&gt;
        &lt;statement&gt;
          &lt;number&gt;PM-14a.&lt;/number&gt;
          &lt;description&gt;
          Implements a process for ensuring that organizational plans for conducting security testing, training, and monitoring activities associated with organizational information systems:
          &lt;/description&gt;
          &lt;statement&gt;
            &lt;number&gt;PM-14a.1.&lt;/number&gt;
            &lt;description&gt;Are developed and maintained; and&lt;/description&gt;
          &lt;/statement&gt;
          &lt;statement&gt;
            &lt;number&gt;PM-14a.2.&lt;/number&gt;
            &lt;description&gt;Continue to be executed in a timely manner;&lt;/description&gt;
          &lt;/statement&gt;
        &lt;/statement&gt;
        &lt;statement&gt;
          &lt;number&gt;PM-14b.&lt;/number&gt;
          &lt;description&gt;
          Reviews testing, training, and monitoring plans for consistency with the organizational risk management strategy and organization-wide priorities for risk response actions.
          &lt;/description&gt;
        &lt;/statement&gt;
      &lt;/statement&gt;
      &lt;supplemental-guidance&gt;
        &lt;description&gt;
        This control ensures that organizations provide oversight for the security testing, training, and monitoring activities conducted organization-wide and that those activities are coordinated. With the importance of continuous monitoring programs, the implementation of information security across the three tiers of the risk management hierarchy, and the widespread use of common controls, organizations coordinate and consolidate the testing and monitoring activities that are routinely conducted as part of ongoing organizational assessments supporting a variety of security controls. Security training activities, while typically focused on individual information systems and specific roles, also necessitate coordination across all organizational elements. Testing, training, and monitoring plans and activities are informed by current threat and vulnerability assessments.
        &lt;/description&gt;
        &lt;related&gt;AT-3&lt;/related&gt;
        &lt;related&gt;CA-7&lt;/related&gt;
        &lt;related&gt;CP-4&lt;/related&gt;
        &lt;related&gt;IR-3&lt;/related&gt;
        &lt;related&gt;SI-4&lt;/related&gt;
      &lt;/supplemental-guidance&gt;
      &lt;references&gt;
        &lt;reference&gt;
          &lt;item xml:lang="en-US" href="https://csrc.nist.gov/publications/search?keywords-lg=800-16"&gt;NIST Special Publication 800-16&lt;/item&gt;
        &lt;/reference&gt;
        &lt;reference&gt;
          &lt;item xml:lang="en-US" href="https://csrc.nist.gov/publications/search?keywords-lg=800-37"&gt;NIST Special Publication 800-37&lt;/item&gt;
        &lt;/reference&gt;
        &lt;reference&gt;
          &lt;item xml:lang="en-US" href="https://csrc.nist.gov/publications/search?keywords-lg=800-53A"&gt;NIST Special Publication 800-53A&lt;/item&gt;
        &lt;/reference&gt;
        &lt;reference&gt;
          &lt;item xml:lang="en-US" href="https://csrc.nist.gov/publications/search?keywords-lg=800-137"&gt;NIST Special Publication 800-137&lt;/item&gt;
        &lt;/reference&gt;
      &lt;/references&gt;
    &lt;/control&gt;
  &lt;/controls&gt;
  </code></pre>