title: Parse Nested XML (with namespaces) in R
body: <p>I am trying to parse an xml response from a web API.</p>
  
  <p>For a simple xml as below, I am able to work with xpathSApply and get the relevant data out very easily.</p>
  
  <p>Following is example.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;CATALOG&gt;
      &lt;PLANT&gt;
          &lt;COMMON&gt;Bloodroot&lt;/COMMON&gt;
          &lt;BOTANICAL&gt;Sanguinaria canadensis&lt;/BOTANICAL&gt;
          &lt;ZONE&gt;4&lt;/ZONE&gt;
          &lt;LIGHT&gt;Mostly Shady&lt;/LIGHT&gt;
          &lt;PRICE&gt;$2.44&lt;/PRICE&gt;
          &lt;AVAILABILITY&gt;031599&lt;/AVAILABILITY&gt;
      &lt;/PLANT&gt;
      &lt;PLANT&gt;
          &lt;COMMON&gt;Columbine&lt;/COMMON&gt;
          &lt;BOTANICAL&gt;Aquilegia canadensis&lt;/BOTANICAL&gt;
          &lt;ZONE&gt;3&lt;/ZONE&gt;
          &lt;LIGHT&gt;Mostly Shady&lt;/LIGHT&gt;
          &lt;PRICE&gt;$9.37&lt;/PRICE&gt;
          &lt;AVAILABILITY&gt;030699&lt;/AVAILABILITY&gt;
      &lt;/PLANT&gt;
  &lt;/CATALOG&gt;
  
  &gt;library(XML)
  &gt;doc&lt;-xmlTreeParse("example.xml",useInternal=TRUE) 
  &gt;rootNode&lt;-xmlRoot(doc)
  &gt;xpathSApply(rootNode,"//COMMON",xmlValue)
  [1] "Bloodroot" "Columbine"
  
  &gt; getNodeSet(doc,"//PLANT")
  [[1]]
  &lt;PLANT&gt;
    &lt;COMMON&gt;Bloodroot&lt;/COMMON&gt;
    &lt;BOTANICAL&gt;Sanguinaria canadensis&lt;/BOTANICAL&gt;
    &lt;ZONE&gt;4&lt;/ZONE&gt;
    &lt;LIGHT&gt;Mostly Shady&lt;/LIGHT&gt;
    &lt;PRICE&gt;$2.44&lt;/PRICE&gt;
    &lt;AVAILABILITY&gt;031599&lt;/AVAILABILITY&gt;
  &lt;/PLANT&gt; 
  
  [[2]]
  &lt;PLANT&gt;
    &lt;COMMON&gt;Columbine&lt;/COMMON&gt;
    &lt;BOTANICAL&gt;Aquilegia canadensis&lt;/BOTANICAL&gt;
    &lt;ZONE&gt;3&lt;/ZONE&gt;
    &lt;LIGHT&gt;Mostly Shady&lt;/LIGHT&gt;
    &lt;PRICE&gt;$9.37&lt;/PRICE&gt;
    &lt;AVAILABILITY&gt;030699&lt;/AVAILABILITY&gt;
  &lt;/PLANT&gt; 
  
  attr(,"class")
  [1] "XMLNodeSet"
  
  &gt; xmlSApply(getNodeSet(rootNode,"//PRICE"),xmlValue) #provides a list of all PRICE values in the xml
  [1] "$2.44" "$9.37"
  </code></pre>
  
  <p>However, the same commands do not work for the below xml which have namespace details. Is there anyway that I can fetch the data in the nodes / tags.</p>
  
  <p>Following is example1.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"&gt;&lt;s:Body&gt;&lt;GetByFilterTradeResponse xmlns="http://entrader.contigoenergy.com/Contigo.Entrader.Service"&gt;&lt;GetByFilterTradeResult xmlns:i="http://www.w3.org/2001/XMLSchema-instance"&gt;
  &lt;CATALOG&gt;
      &lt;CATEGORY&gt;
          &lt;FAMILY&gt;
              &lt;PLANT&gt;
                  &lt;COMMON&gt;Bloodroot&lt;/COMMON&gt;
                  &lt;BOTANICAL&gt;Sanguinaria canadensis&lt;/BOTANICAL&gt;
                  &lt;ZONE&gt;4&lt;/ZONE&gt;
                  &lt;DETAILS&gt;
                      &lt;PRICEINBULK&gt;2.3&lt;/PRICEINBULK&gt;
                      &lt;MINVOLUME&gt;100&lt;/MINVOLUME&gt;
                  &lt;/DETAILS&gt;
                  &lt;LIGHT&gt;Mostly Shady&lt;/LIGHT&gt;
                  &lt;PRICE&gt;$2.44&lt;/PRICE&gt;
                  &lt;AVAILABILITY&gt;031599&lt;/AVAILABILITY&gt;
              &lt;/PLANT&gt;
              &lt;PLANT&gt;
                  &lt;COMMON&gt;Columbine&lt;/COMMON&gt;
                  &lt;BOTANICAL&gt;Aquilegia canadensis&lt;/BOTANICAL&gt;
                  &lt;ZONE&gt;3&lt;/ZONE&gt;
                  &lt;DETAILS&gt;
                      &lt;PRICEINBULK&gt;9.00&lt;/PRICEINBULK&gt;
                      &lt;MINVOLUME&gt;100&lt;/MINVOLUME&gt;
                  &lt;/DETAILS&gt;
                  &lt;LIGHT&gt;Mostly Shady&lt;/LIGHT&gt;
                  &lt;PRICE&gt;$9.37&lt;/PRICE&gt;
                  &lt;AVAILABILITY&gt;030699&lt;/AVAILABILITY&gt;
              &lt;/PLANT&gt;
          &lt;/FAMILY&gt;
      &lt;/CATEGORY&gt; 
  &lt;/CATALOG&gt;
  &lt;/GetByFilterTradeResult&gt;&lt;/GetByFilterTradeResponse&gt;&lt;/s:Body&gt;&lt;/s:Envelope&gt;
  </code></pre>
  
  <p>Following commands does not extract the node values from the above xml</p>
  
  <pre><code>&gt;doc&lt;-xmlTreeParse("example1.xml",useInternal=TRUE) 
  &gt;rootNode&lt;-xmlRoot(doc) 
  &gt; xpathSApply(rootNode,"//COMMON",xmlValue) 
  list()
  
  &gt; getNodeSet(doc,"//PLANT")
  list()
  attr(,"class")
  [1] "XMLNodeSet"
  
  &gt; xmlSApply(getNodeSet(rootNode,"//PRICE"),xmlValue) 
  list()
  </code></pre>