I\'m struggling to get the correct combination of an XPath expression and the namespace specification as required by package XML (argument namespaces) f
I had a similar issue, but in my case I did not care about the namespace and would like a solution that ignored the namespace.
Assume that we have the following XML in the variable myxml:
Test
In R we want to read this so we run:
myxml <- '
Test
'
myxmltop <- xmlParse(myxml)
ns <- xmlNamespaceDefinitions(myxmltop, simplify = TRUE)
Here I have simplified Rappster's code by using the simplify=TRUE parameter. Now we can add the name/prefix of the namespace as in Rappster's code:
names(ns)[1] <- "xmlns"
Now we can refer to this namespace by:
getNodeSet(myxmltop, "//xmlns:Parameter", namespaces =ns)
Simpler solution (ignoring namespaces)
We can also be more flexible by matching on any namespace by doing:
myxmltop <- xmlParse(myxml)
getNodeSet(myxmltop, "//*[local-name() = 'Parameter']")
This solution was inspired by this SO answer.