在R xml2库中,我不明白xml_find_all和xml_find_first是如何工作的

qv7cva1a  于 2023-06-19  发布在  其他
关注(0)|答案(1)|浏览(95)

我试图模仿一个简单的例子,用xml_find_first()和xml_find_all()函数检索命名节点。这个简单的例子效果很好:

library(xml2)
x <- read_xml("<foo><bar><baz/></bar><baz/></foo>")
xml_find_all(x, ".//baz")
xml_find_all(x, ".//bar")
xml_find_first(x, ".//bar")

正如预期的那样,这三种情况的输出是:

{xml_nodeset (2)}
[1] <baz/>
[2] <baz/>

{xml_nodeset (1)}
[1] <bar>\n  <baz/>\n</bar>

{xml_node}
<bar>
[1] <baz/>

现在,在更复杂的 * 生产示例 * 中,这两个函数的行为似乎不同

library(xml2)
yy <- read_xml(
  '<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
      <fileVersion appName="xl" lastEdited="3" lowestEdited="5" rupBuild="9302"/>
      <workbookPr/>
      <workbookProtection/>
      <bookViews>
          <workbookView windowWidth="27090" windowHeight="8700" tabRatio="500" activeTab="1"/>
      </bookViews>
      <sheets>
          <sheet name="PARTICIPANTES" sheetId="1" r:id="rId1"/>
          <sheet name="ORDENADOS" sheetId="2" r:id="rId2"/>
      </sheets>
      <calcPr calcId="144525"/>
  </workbook>'
)

xml_find_first(yy, ".//sheets")
xml_find_first(yy, "//sheets")
xml_find_all(yy, "//sheets")

在所有情况下,答案都是缺少节点:

{xml_missing}
<NA>

{xml_missing}
<NA>

{xml_nodeset (0)}

这些功能中是否有我遗漏的内容?

mwyxok5s

mwyxok5s1#

考虑使用xml_ns_rename来重命名默认名称空间,由xmlns="..."标识,它不同于前缀名称空间xmlns:r="..."。重命名允许您在任何XPath表达式中使用临时前缀。

ns <- xml_ns_rename(xml_ns(yy), d1 = "doc")

xml_find_first(yy, ".//doc:sheets", ns)
xml_find_first(yy, "//doc:sheets", ns)
xml_find_all(yy, "//doc:sheets", ns)

相关问题