title: How to navigate XML with xpath in R
body: <p>Can I have some xpath navigational assistance please, for use with an XML document in R?</p>
  
  <p>I have provided a very stripped down version of my actual data to illustrate ('my_xml'). In general, I am looking to import a pmml doc (exported by spss in XML) into R.</p>
  
  <p>For this example I would like to know how I can return the 'value' attribute values in nodes where the property attributes ='valid'. You will see that I have set one property to 'notvalid'.</p>
  
  <pre><code>my_xml = xmlParse('&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;PMML xmlns="http://www.dmg.org/PMML-4_1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="4.1" xsi:schemaLocation="http://www.dmg.org/PMML-4_1 pmml-4-1.xsd"&gt;
  &lt;Header copyright="(C) Copyright IBM Corp. 1989, 2014."&gt;
  &lt;Application name="IBM SPSS Statistics 23.0" version="23.0.0.0"/&gt;
  &lt;/Header&gt;&lt;DataDictionary numberOfFields="15"&gt;
  &lt;DataField dataType="string" displayName="Target_Status_OIDV_M!" name="Target_Status_OIDV_M!" optype="categorical"&gt;
  &lt;Extension extender="spss.com" name="format" value="1"/&gt;
  &lt;Extension extender="spss.com" name="width" value="1"/&gt;
  &lt;Extension extender="spss.com" name="decimals" value="0"/&gt;
  &lt;Value displayValue="F" property="valid" value="F"/&gt;
  &lt;Value displayValue="T" property="valid" value="T"/&gt;
  &lt;/DataField&gt;
  &lt;DataField dataType="string" displayName="Status_OIDV_M2" name="Status_OIDV_M2" optype="categorical"&gt;
  &lt;Extension extender="spss.com" name="format" value="1"/&gt;
  &lt;Extension extender="spss.com" name="width" value="4"/&gt;    
  &lt;Extension extender="spss.com" name="decimals" value="0"/&gt;    
  &lt;Value displayValue="0000" property="valid" value="0000"/&gt;    
  &lt;Value displayValue="0001" property="valid" value="0001"/&gt;    
  &lt;Value displayValue="0100" property="notvalid" value="0100"/&gt;    
  &lt;/DataField&gt;
  &lt;/DataDictionary&gt;
  &lt;/PMML&gt;')
  </code></pre>
  
  <p>I check the class...and look for all the values</p>
  
  <pre><code>class(my_xml) #  "XMLInternalDocument" - excellent... just what xpathApply is looking for :)
  
  get_value_attr = xpathApply(my_xml, "//@value")
  print(get_value_attr) # a start in the right direction
  </code></pre>
  
  <p>So next i try with my property condition...but i just get an empty list</p>
  
  <pre><code>get_value_attr_with_condition = xpathApply(my_xml, "//@value[@property='valid']") 
  print(get_value_attr_with_condition) # returns an empty list
  </code></pre>
  
  <p>This makes me realise that of all the examples I have seen, square bracket attribute conditions are only used on a node, I've never seen one run with another attribute (i.e. //mynodename[@attribute='superduper'])</p>
  
  <p>But when I search with xpath for the 'Value' nodes any where in the doc (i.e. with '//').... it returns an empty list (NB - I am now search for the 'Value' node with a capital 'V', not the 'value' attribute)</p>
  
  <pre><code>get_values = xpathApply(my_xml, "//Value") 
  print(get_values) 
  </code></pre>
  
  <p>If I search for the current node, using a period...</p>
  
  <pre><code>my_current_node = xpathApply(my_xml, ".") 
  print(my_current_node) 
  </code></pre>
  
  <p>It's another empty list - why doesn't this select my current node?</p>
  
  <p>I thought maybe xpathApply was looking for some additional args, 3 attempts...</p>
  
  <pre><code>get_that_value = xpathApply(my_xml, "//Value", xmlGetAttr, "value")
  print(get_that_value) # empty list again
  
  get_that_property = xpathApply(my_xml, "//Value", xmlGetAttr, "property")
  print(get_that_property) # empty list again
  
  get_the_xmlValue = xpathApply(my_xml, "//Value", xmlValue)
  print(get_the_xmlValue) 
  </code></pre>
  
  <p>Nope - I must be doing something wrong! But what???</p>