具有显式默认命名空间的XML文档的XPath和命名空间规范

Rap*_*ter 5 xml xpath namespaces r default-namespace

我正在努力获得XPath表达式和命名空间规范的正确组合,这是package XML(argument namespaces)对于xmlns在顶部元素中定义了显式命名空间的XML文档所要求的.

UPDATE

感谢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" 
Run Code Online (Sandbox Code Playgroud)

所以我们只需指定一个充当前缀的名称(这可以是任何有效的R名称):

names(ns)[1] <- "xmlns"
Run Code Online (Sandbox Code Playgroud)

现在,我们所要做的就是在XPath表达式中的任何地方使用默认名称空间前缀:

getNodeSet(doc, "/xmlns:doc//xmlns:b[@omegahat:status='foo']", ns)
Run Code Online (Sandbox Code Playgroud)

对于那些对基于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]]
Run Code Online (Sandbox Code Playgroud)

但是,在我的文档中,命名空间已经在<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]]  
Run Code Online (Sandbox Code Playgroud)

一切都还行.但更重要的是,我的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))
Run Code Online (Sandbox Code Playgroud)

过去的工作现在失败了:

> 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)
)
Run Code Online (Sandbox Code Playgroud)

这似乎让我更接近:

> 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"
Run Code Online (Sandbox Code Playgroud)

然而,现在我不知道如何进入子节点:

> 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"
Run Code Online (Sandbox Code Playgroud)

har*_*r07 3

不带前缀 ( ) 的命名空间定义xmlns="..."是默认命名空间。如果 XML 文档具有默认命名空间,则声明默认命名空间的元素及其所有没有前缀且没有不同默认命名空间声明的后代都被视为在上述默认命名空间中。

因此,在您的情况下,您需要在 XPath 中所有元素的开头使用为默认命名空间注册的前缀,例如:

/xmlns:doc//xmlns:b[@omegahat:status='foo']
Run Code Online (Sandbox Code Playgroud)

更新 :

实际上我不是 的用户r,但在网上查看一些参考资料可能会起作用:

getNodeSet(doc, "/ns:doc//ns:b[@omegahat:status='foo']", c(ns="http://something.org"))
Run Code Online (Sandbox Code Playgroud)