XPath and namespace specification for XML documents with an explicit default namespace

倾然丶 夕夏残阳落幕 提交于 2019-11-27 15:17:45

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 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)

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!