Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding names of XPath nodes in R

Tags:

r

xml

xpath

I would like to find the parent names of nodes that does not have any children. I tried my XPath expression on, for example, xpather.com and it works fine, but I cannot get it to work in R.

//*[not(*)]/parent::*/name()

The result should be aa, ca

    library(XML)
    xml_doc <- ("<ca>
  <ai>67400000</ai>
  <ssci>FN</ssci>
  <aa>
    <ta>1280</ta>
    <tc>EUR</tc>
  </aa>
</ca>")

xml_parsed <- xmlParse(xml_doc)
a <- getNodeSet(xml_parsed, "//*[not(*)]/parent::*")
names(a)
like image 431
MLEN Avatar asked Jan 22 '26 16:01

MLEN


1 Answers

library(xml2)
doc <- read_xml(xml_doc)
# Xpath for parents of nodes without children 
#  xml_name() for name extraction
xml_name(xml_find_all(doc, ".//*[not(*)]/parent::*"))
# [1] "ca" "aa"
like image 165
Wimpel Avatar answered Jan 24 '26 07:01

Wimpel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!