使用XPath获取XML包中的元素值(R)



假设我有以下XML片段:

<book>
<title lang="en">Harry Potter</title>
<price>29.99</price>
</book>

如何获得价格值?我使用XPath尝试了这个代码片段的各种变体:

library("XML")
price = xpathSApply(doc, '//book/price')

但是它没有返回我期望的29.99

我们可以用read_xml阅读

library(xml2)
dat <- read_xml("doc1.xml")
as.numeric(xml_text(xml_find_all(dat, "price")))
[1] 29.99

最新更新