对于在顶部元素定义了显式命名空间的 XML 文档,我正在努力获得 XPath 表达式和命名空间规范的正确组合,这是 package XML
(argument namespaces
)所要求的。xmlns
更新
感谢 har07 我能够把它放在一起:
查询命名空间后,第一个条目ns
还没有名称,这就是问题所在:
nsDefs <- xmlNamespaceDefinitions(doc)
ns <- structure(sapply(nsDefs, function(x) x$uri), names = names(nsDefs))
> ns
omegahat r
"http://something.org" "http://www.omegahat.org" "http://www.r-project.org"
所以我们只需分配一个名称作为前缀(这可以是任何有效的 R 名称):
names(ns)[1] <- "xmlns"
现在我们要做的就是在我们的 XPath 表达式中到处使用默认的命名空间前缀:
getNodeSet(doc, "/xmlns:doc//xmlns:b[@omegahat:status='foo']", ns)
对于那些对基于name()
和namespace-uri()
(以及其他)的替代解决方案感兴趣的人可能会发现这篇文章很有帮助。
仅供参考:这是我们找到解决方案之前的试错代码:
考虑以下示例?xmlParse
:
require("XML")
doc <- xmlParse(system.file("exampleData", "tagnames.xml", package = "XML"))
> doc
<?xml version="1.0"?>
<doc>
<!-- A comment -->
<a xmlns:omegahat="http://www.omegahat.org" xmlns:r="http://www.r-project.org">
<b>
<c>
<b/>
</c>
</b>
<b omegahat:status="foo">
<r:d>
<a status="xyz"/>
<a/>
<a status="1"/>
</r:d>
</b>
</a>
</doc>
nsDefs <- xmlNamespaceDefinitions(getNodeSet(doc, "/doc/a")[[1]])
ns <- structure(sapply(nsDefs, function(x) x$uri), names = names(nsDefs))
getNodeSet(doc, "/doc//b[@omegahat:status='foo']", ns)[[1]]
然而,在我的文档中,命名空间已经在<doc>
标记中定义,因此我相应地修改了示例 XML 代码:
xml_source <- c(
"<?xml version=\"1.0\"?>",
"<doc xmlns:omegahat=\"http://www.omegahat.org\" xmlns:r=\"http://www.r-project.org\">",
"<!-- A comment -->",
"<a>",
"<b>",
"<c>",
"<b/>",
"</c>",
"</b>",
"<b omegahat:status=\"foo\">",
"<r:d>",
"<a status=\"xyz\"/>",
"<a/>",
"<a status=\"1\"/>",
"</r:d>",
"</b>",
"</a>",
"</doc>"
)
write(xml_source, file="exampleData_2.xml")
doc <- xmlParse("exampleData_2.xml")
nsDefs <- xmlNamespaceDefinitions(doc)
ns <- structure(sapply(nsDefs, function(x) x$uri), names = names(nsDefs))
getNodeSet(doc, "/doc", namespaces = ns)
getNodeSet(doc, "/doc//b[@omegahat:status='foo']", namespaces = ns)[[1]]
一切仍然正常。不过,更重要的是,我的 XML 代码还明确定义了默认命名空间 ( xmlns
):
xml_source <- c(
"<?xml version=\"1.0\"?>",
"<doc xmlns=\"http://something.org\" xmlns:omegahat=\"http://www.omegahat.org\" xmlns:r=\"http://www.r-project.org\">",
"<!-- A comment -->",
"<a>",
"<b>",
"<c>",
"<b/>",
"</c>",
"</b>",
"<b omegahat:status=\"foo\">",
"<r:d>",
"<a status=\"xyz\"/>",
"<a/>",
"<a status=\"1\"/>",
"</r:d>",
"</b>",
"</a>",
"</doc>"
)
write(xml_source, file="exampleData_3.xml")
doc <- xmlParse("exampleData_3.xml")
nsDefs <- xmlNamespaceDefinitions(doc)
ns <- structure(sapply(nsDefs, function(x) x$uri), names = names(nsDefs))
过去的工作现在失败了:
> getNodeSet(doc, "/doc", namespaces = ns)
list()
attr(,"class")
[1] "XMLNodeSet"
Warning message:
using http://something.org as prefix for default namespace http://something.org
> getNodeSet(doc, "/xmlns:doc", namespaces = ns)
XPath error : Undefined namespace prefix
XPath error : Invalid expression
Error in xpathApply.XMLInternalDocument(doc, path, fun, ..., namespaces = namespaces, :
error evaluating xpath expression /xmlns:doc
In addition: Warning message:
using http://something.org as prefix for default namespace http://something.org
getNodeSet(doc, "/xmlns:doc",
namespaces = matchNamespaces(doc, namespaces="xmlns", nsDefs = nsDefs)
)
这似乎让我更接近:
> getNodeSet(doc, "/xmlns:doc",
+ namespaces = matchNamespaces(doc, namespaces="xmlns", nsDefs = nsDefs)
+ )[[1]]
<doc xmlns="http://something.org" xmlns:omegahat="http://www.omegahat.org" xmlns:r="http://www.r-project.org">
<!-- A comment -->
<a>
<b>
<c>
<b/>
</c>
</b>
<b omegahat:status="foo">
<r:d>
<a status="xyz"/>
<a/>
<a status="1"/>
</r:d>
</b>
</a>
</doc>
attr(,"class")
[1] "XMLNodeSet"
然而,现在我不知道如何进行才能到达子节点:
> getNodeSet(doc, "/xmlns:doc//b[@omegahat:status='foo']", ns)[[1]]
XPath error : Undefined namespace prefix
XPath error : Invalid expression
Error in xpathApply.XMLInternalDocument(doc, path, fun, ..., namespaces = namespaces, :
error evaluating xpath expression /xmlns:doc//b[@omegahat:status='foo']
In addition: Warning message:
using http://something.org as prefix for default namespace http://something.org
> getNodeSet(doc, "/xmlns:doc//b[@omegahat:status='foo']",
+ namespaces = c(
+ matchNamespaces(doc, namespaces="xmlns", nsDefs = nsDefs),
+ matchNamespaces(doc, namespaces="omegahat", nsDefs = nsDefs)
+ )
+ )
list()
attr(,"class")
[1] "XMLNodeSet"