我有一个xml文件,getnodeset返回一个空列表,而我希望它返回一些东西。我不确定是我的xpath关闭了还是别的什么。我使用的是R Studio和XML包。
“My Design.xml”文件保存在本地,但这里有一个示例: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")发布于 2021-03-11 07:53:57
您的xml文件有一个与之关联的命名空间。处理它有两种选择。使用命名空间指定节点或剥离命名空间。
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()https://stackoverflow.com/questions/66521721
复制相似问题