使用xpath选择某些子节点

时间:2014-02-19 10:13:35

标签: xml r xpath

我正在使用R函数xpathSApply,我想选择某些子节点。目前我只能选择一个子节点,例如

xpathSApply(dat, "//Establishment[AddressLine3='Mumbles']/BusinessName",xmlValue)

这给了我一个子节点AddressLine3 = Mumbles的restaraunts列表。还有一个我感兴趣的名为Rating的子节点(完整路径为//Establishment/Rating)。我想要做的是在一个xPath查询中带回一对值BusinessNameRating子节点。可以这样做吗?

2 个答案:

答案 0 :(得分:1)

XPath查询不是R特定的,因此this建议

query <- "//EstablishmentDetail[AddressLine3='Mumbles']/
              *[self::BusinessName or self::RatingValue]"

可以作为

library(XML)
xml <- xmlParse("http://ratings.food.gov.uk/OpenDataFiles/FHRS568en-GB.xml")
as.data.frame(split(vapply(xml[query], xmlValue, character(1)), 1:2))

但也许写起来更清楚

query <- "//EstablishmentDetail[AddressLine3='Mumbles']"
xmlToDataFrame(xml[query])[, c("BusinessName", "RatingValue")]

答案 1 :(得分:1)

你可以用“|”分开2个查询。但显然它会在同一个向量中返回值,然后你必须合成结果。

url <- "http://ratings.food.gov.uk/OpenDataFiles/FHRS568en-GB.xml"
doc <- xmlParse(url)

datas <- xpathSApply(doc, "//EstablishmentDetail[AddressLine3='Mumbles']/BusinessName | //EstablishmentDetail[AddressLine3='Mumbles']/RatingValue", xmlValue)

data.frame(BusinessName = datas[seq(1, 117, by = 2)], RatingValue = datas[-seq(1, 117, by = 2)])