Skip to content
Prev 326771 / 398502 Next

[XML packages] how to get the sub-node according to the sub-node's attribute?

Just use xpathSApply with brackets to specify the specific id...

 doc <- xmlParse(system.file("exampleData", "mtcars.xml", package="XML"))

xpathSApply(doc, "//record[@id='Lotus Europa']", xmlValue)
[1] "        30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2"

#OR?
xpathSApply(doc, "//record[@id='Lotus Europa']")
getNodeSet(doc, "//record[@id='Lotus Europa']")

# And to list all Ids...

xpathSApply(doc, "//record", xmlGetAttr, "id")
 [1] "Mazda RX4"           "Mazda RX4 Wag"       "Datsun 710"   .....