2

I have an XML file with measurements. I would like to select only those measurementSiteName's that have a latitude and longitude using R.

With this code chunk I can select the name of a site with a specific latitude:

doc <- xml2::read_xml('test.xml')
query <- ".//measurementSiteName/values/value[../../../measurementSiteLocation/locationForDisplay/latitude/text() = '52.23634']"
xml2::xml_text(xml2::xml_find_all(doc, query))

However, there are site names without a latitude or longitude. I don't know how to do this. The latitude and longitude nodes may be missing or empty.

Can somebody please help me out? Please find attached text.xml

<measurements>
  <measurement>
    <measurementSiteName>
      <values>
          <value>abc</value>
      </values>
    </measurementSiteName>
    <measurementSiteLocation>
      <locationForDisplay>
        <latitude>52.23634</latitude>
        <longitude>4.503404</longitude>
      </locationForDisplay>
    </measurementSiteLocation>
  </measurement>
  <measurement>
    <measurementSiteName>
      <values>
          <value>def</value>
      </values>
    </measurementSiteName>
    <measurementSiteLocation>
      <locationForDisplay>
        <latitude></latitude>
        <longitude></longitude>
      </locationForDisplay>
    </measurementSiteLocation>
  </measurement>
  <measurement>
    <measurementSiteName>
      <values>
          <value>ghi</value>
      </values>
    </measurementSiteName>
    <measurementSiteLocation>
      <locationForDisplay>
      </locationForDisplay>
    </measurementSiteLocation>
  </measurement>
</measurements>
0

1 Answer 1

1

This XPath,

//measurementSiteLocation
  [locationForDisplay[    number(latitude)  = number(latitude) 
                      and number(longitude) = number(longitude)]]

will select all measurementSiteLocation elements with a locationForDisplay child that has latitude and longitude children with numeric values.

References:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.