title: Repeat name for each time an element appears in parent XML node
body: <p>Here is a simplified version of an XML file I'm trying to parse in R using the <code>XML</code> package:</p>
  
  <pre><code>testXML&lt;-
  '&lt;MYDOC&gt;
    &lt;PARENT&gt;
      &lt;NAME&gt;A&lt;/NAME&gt;
      &lt;COUNTTHIS&gt;blah&lt;/COUNTTHIS&gt;
      &lt;COUNTTHIS&gt;blah&lt;/COUNTTHIS&gt;
    &lt;/PARENT&gt;
    &lt;PARENT&gt;
      &lt;NAME&gt;B&lt;/NAME&gt;
      &lt;COUNTTHIS&gt;blah&lt;/COUNTTHIS&gt;
    &lt;/PARENT&gt;
    &lt;PARENT&gt;
      &lt;NAME&gt;C&lt;/NAME&gt;
      &lt;COUNTTHIS&gt;blah&lt;/COUNTTHIS&gt;
      &lt;COUNTTHIS&gt;blah&lt;/COUNTTHIS&gt;
      &lt;COUNTTHIS&gt;blah&lt;/COUNTTHIS&gt;
    &lt;/PARENT&gt;
  &lt;/MYDOC&gt;'
  </code></pre>
  
  <p>I would like to grab the <code>NAME</code> value in each <code>PARENT</code> node, repeated by the number of times that <code>COUNTTHIS</code> appears within <code>PARENT</code>.  I would also like the running count of <code>COUNTTHIS</code> within <code>PARENT</code>.  So the desired output would be a data frame like this:</p>
  
  <pre><code># Name  ID
  #    A   1
  #    A   2
  #    B   1
  #    C   1
  #    C   2
  #    C   3
  </code></pre>
  
  <p>I've got something that works, but it seems like there should be a better way.  I'm going to be parsing lots of XML files, so I'd like to make sure it's efficient.</p>
  
  <pre><code>library(XML)
  
  parsedXML &lt;- xmlTreeParse(testXML, useInternalNodes=T)
  
  cnt &lt;- unlist(getNodeSet(parsedXML, "//NAME", fun=xmlValue))
  
  names &lt;- NULL
  for(i in 1:length(cnt)) {names[i] &lt;- length(getNodeSet(parsedXML, paste0("//PARENT[NAME='",cnt[i],"']/COUNTTHIS")))}
  
  rep(cnt, times=names)
  #[1] "A" "A" "B" "C" "C" "C"
  </code></pre>
  
  <p>(Note that I'm not bothering to add the ID column here, since I already know how to do that.  But please feel free to include that step in your answer, since you may have a better method for that too.)</p>