【发布时间】:2020-07-11 12:56:01
【问题描述】:
我正在尝试使用 xml2 包来检索和过滤 R 中的 XML 节点。
我有一个带有结构的 XML 文件...
...
<entry>
<feature type="x">123</feature>
<feature type="y">456</feature>
<feature type="y">789</feature>
</entry>
...
...我试图在单个语句中检索只是类型为“y”的第一个“特征”。
目前我正在这样做:
# Return all <feature> nodes
xmlNodes <- xml_find_all(inputXml, ".//entry/feature")
# ...filter by type="y"...
xmlNodes <- xmlNodes[xml_attr(xmlNodes, "type")=="y"]
# ...and then return the first node
xmlNode <- xmlNodes[1]
是否有更简单的方法可以在单个语句中实现这一点,也许使用带有“type”==“y”条件的 xml_find_first() 函数,假设第一个特征节点可能不一定是“type” =“y”?
可能是这样的:
xmlNode <- xml_find_first(inputXml, ".//entry/feature" & xml_attr(inputXml, "type")=="chain")
我觉得这是一个非常简单的问题,但我是 R 新手,对所有语法都不太熟悉...非常感谢!
【问题讨论】: