I\'m struggling to get the correct combination of an XPath expression and the namespace specification as required by package XML (argument namespaces
) f
Namespace definition without prefix (xmlns="..."
) is default namespace. In case of XML document having default namespace, the element where default namespace declared and all of it's descendant without prefix and without different default namespace declaration are considered in that aforementioned default namespace.
Therefore, in your case you need to use prefix registered for default namespace at the beginning of all elements in the XPath, for example :
/xmlns:doc//xmlns:b[@omegahat:status='foo']
UPDATE :
Actually I'm not a user of r
, but looking at some references on net something like this may work :
getNodeSet(doc, "/ns:doc//ns:b[@omegahat:status='foo']", c(ns="http://something.org"))
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:
<root xmlns="uri:someuri.com:schema">
<Parameter>Test
</Parameter>
</root>
In R we want to read this so we run:
myxml <- '
<root xmlns="uri:someuri.com:schema">
<Parameter>Test
</Parameter>
</root>
'
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.
I think @HansHarhoff provides a very good solution.
For anybody else still searching for a solution, in my experience I think the following works more generally since a single XML document can have multiple namespaces.
doc <- xmlInternalTreeParse(xml_source)
ns <- getDefaultNamespace(doc)[[1]]$uri
names(ns)[1] <- "xmlns"
getNodeSet(doc, "//xmlns:Parameter", namespaces = ns)