简体   繁体   中英

R - GetNodeSet Returning empty list

I have an xml file and getnodeset is returning an empty list, when I would expect it to return something. I'm not sure if my xpath is off or something else. I'm using R Studio and the XML package.

The 'My Design.xml" file is saved locally but an example of it is here: https://raw.githubusercontent.com/hpxmlwg/hpxml/master/examples/bpi2101.xml

library(XML)
#Parsing this way works fine
hpx <- xmlRoot(xmlTreeParse("My Design.xml"))
hpx[[1]][[2]]    

#But this way returns empty lists
hpx2 <- xmlInternalTreeParse("My Design.xml")
getNodeSet(hpx2, "/HPXML/XMLTransactionHeaderInformation/XMLGeneratedBy")

Your xml file has an associated namespace associated with it. There are two options in dealing with it. Use the namespace to specify the nodes or strip the name space out.

library(xml2)
library(magrittr)

page<-read_xml("https://raw.githubusercontent.com/hpxmlwg/hpxml/master/examples/bpi2101.xml")

#specify the namespace
xml_ns(page)
xml_find_all(page, ".//d1:XMLGeneratedBy") %>% xml_text()


#or strip the namespace out
xml_ns_strip(page)
xml_find_all(page, ".//XMLGeneratedBy") %>% xml_text()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM