title: PL/SQL: Count number of nodes in xml
body: <p>I am working with Oracle.</p>
  
  <p>Is there a way to count the number of nodes (including descendants) within an XML file using PL/SQL?</p>
  
  <p>I would like to be able to save the result as a variable to be used as an upper limit for a loop iterator.</p>
  
  <p>I have the following xml, and I want to count the number of nodes within the row node:</p>
  
  <pre><code>&lt;row&gt;
    &lt;date name="date1" id="101"&gt;&lt;/date&gt;
    &lt;element1 name="ele1" id="111"&gt;
      &lt;stuff&gt;&lt;/stuff&gt;
      &lt;stuff&gt;&lt;/stuff&gt;
      &lt;stuff&gt;&lt;/stuff&gt;
    &lt;/element1&gt;
    &lt;element2 name="ele2" id="121"&gt;&lt;/element2&gt;
    &lt;element3 name="ele3" id="131&gt;&lt;/element15&gt;
  &lt;/row&gt;
  </code></pre>
  
  <p>The result should be 7.</p>
  
  <p>@johnbk I am working with Oracle</p>
  
  <p>The idea here is that after I get the number of nodes I can use it in:</p>
  
  <pre><code>nodeCount := 1;
      FOR i IN 1 .. numNodes
      LOOP
          xpath1 := '/row/*[' || nodeCount || ']/@name';
          SELECT EXTRACT(form_xml, xpath1) as other_name;
          nodeCount := nodeCount +1;
      END LOOP;
  </code></pre>
  
  <p>Thanks for your help.</p>