Home > Enterprise >  Finding names of XPath nodes in R
Finding names of XPath nodes in R

Time:11-07

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)

CodePudding user response:

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"
  • Related