xml2 包 (R) 中的 xml_find_all 函数未找到相关节点 [英] xml_find_all function from xml2 package (R) does not find relevant nodes

查看:21
本文介绍了xml2 包 (R) 中的 xml_find_all 函数未找到相关节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 R 中使用 xml2 包来访问 xml 数据,发现它在不同的 xml_documents 上表现不同.

I am using the xml2 package in R to access xml data, and found that it behaves different on different xml_documents.

关于这个宠物的例子

library(xml2)
doc <- read_xml( "<MEMBERS>
                      <CUSTOMER>
                         <ID>178</ID>
                         <FIRST.NAME>Alvaro</FIRST.NAME>
                         <LAST.NAME>Juarez</LAST.NAME>
                         <ADDRESS>123 Park Ave</ADDRESS>
                         <ZIP>57701</ZIP>
                      </CUSTOMER>
                      <CUSTOMER>
                         <ID>934</ID>
                         <FIRST.NAME>Janette</FIRST.NAME>
                         <LAST.NAME>Johnson</LAST.NAME>
                         <ADDRESS>456 Candy Ln</ADDRESS>
                         <ZIP>57701</ZIP>
                      </CUSTOMER>  
                   </MEMBERS>")
doc
{xml_document}
<MEMBERS>
[1] <CUSTOMER>\n  <ID>178</ID>\n  <FIRST.NAME>Alvaro</FIRST.NAME>\n  <LAST.NAME>Juarez</LAST.NAME>\n  <ADDRESS>12 ...
[2] <CUSTOMER>\n  <ID>934</ID>\n  <FIRST.NAME>Janette</FIRST.NAME>\n  <LAST.NAME>Johnson</LAST.NAME>\n  <ADDRESS> ...

我可以运行以下代码

xml_find_all(doc, "//FIRST.NAME")
{xml_nodeset (2)}
[1] <FIRST.NAME>Alvaro</FIRST.NAME>
[2] <FIRST.NAME>Janette</FIRST.NAME>

给我预期的输出(找到所有带有FIRST.NAME"标签的节点).

giving me the expected output (finding all nodes with 'FIRST.NAME' tags).

但是,如果我对这个执行相同的操作xml文件:

However, if I perform the same action on this xml file:

example <- read_xml(file.path("~/Downloads", "uniprot_subset.xml"))
> example
{xml_document}
<uniprot>
 [1] <entry xmlns="http://uniprot.org/uniprot" dataset="Swiss-Prot" created="2011-06-28" modified="2019-01-16" version="35">\n  <accession>Q6GZX4</accession>\n  <name>001R_FRG3G</name>\n  <protein>\n    <recommendedName>\n      <fullName>Putative tr ...
 [2] <entry xmlns="http://uniprot.org/uniprot" dataset="Swiss-Prot" created="2011-06-28" modified="2019-01-16" version="36">\n  <accession>Q6GZX3</accession>\n  <name>002L_FRG3G</name>\n  <protein>\n    <recommendedName>\n      <fullName>Uncharacter ...
 [3] <entry xmlns="http://uniprot.org/uniprot" dataset="Swiss-Prot" created="2009-06-16" modified="2018-06-20" version="22">\n  <accession>Q197F8</accession>\n  <name>002R_IIV3</name>\n  <protein>\n    <recommendedName>\n      <fullName>Uncharacteri ...
 [4] <entry xmlns="http://uniprot.org/uniprot" dataset="Swiss-Prot" created="2009-06-16" modified="2017-09-27" version="18">\n  <accession>Q197F7</accession>\n  <name>003L_IIV3</name>\n  <protein>\n    <recommendedName>\n      <fullName>Uncharacteri ...
 [5] <entry xmlns="http://uniprot.org/uniprot" dataset="Swiss-Prot" created="2011-06-28" modified="2019-01-16" version="31">\n  <accession>Q6GZX2</accession>\n  <name>003R_FRG3G</name>\n  <protein>\n    <recommendedName>\n      <fullName>Uncharacter ...
 [6] <entry xmlns="http://uniprot.org/uniprot" dataset="Swiss-Prot" created="2011-06-28" modified="2017-09-27" version="29">\n  <accession>Q6GZX1</accession>\n  <name>004R_FRG3G</name>\n  <protein>\n    <recommendedName>\n      <fullName>Uncharacter ...
 [7] <entry xmlns="http://uniprot.org/uniprot" dataset="Swiss-Prot" created="2009-06-16" modified="2017-09-27" version="24">\n  <accession>Q197F5</accession>\n  <name>005L_IIV3</name>\n  <protein>\n    <recommendedName>\n      <fullName>Uncharacteri ...
 [8] <entry xmlns="http://uniprot.org/uniprot" dataset="Swiss-Prot" created="2011-06-28" modified="2019-01-16" version="38">\n  <accession>Q6GZX0</accession>\n  <name>005R_FRG3G</name>\n  <protein>\n    <recommendedName>\n      <fullName>Uncharacter ...
 [9] <entry xmlns="http://uniprot.org/uniprot" dataset="Swiss-Prot" created="2009-06-16" modified="2019-01-16" version="44">\n  <accession>Q91G88</accession>\n  <name>006L_IIV6</name>\n  <protein>\n    <recommendedName>\n      <fullName>Putative Kil ...
[10] <entry xmlns="http://uniprot.org/uniprot" dataset="Swiss-Prot" created="2011-06-28" modified="2017-09-27" version="27">\n  <accession>Q6GZW9</accession>\n  <name>006R_FRG3G</name>\n  <protein>\n    <recommendedName>\n      <fullName>Uncharacter ...

行为不同

xml_find_all(example, "//accession")
{xml_nodeset (0)}

基本上,它不会找到任何带有 'accession' 标签的节点,即使它们存在并且可以被不同的函数访问,例如使用

Basically, it will not find any nodes with the 'accession' tag, even though they exist and can be accessed by different functions, for instance using

xml_children(xml_children(example)[1])[1]
{xml_nodeset (1)}
[1] <accession>Q6GZX4</accession>

谁能告诉我为什么 xml_find_all 函数在后一个例子中找不到任何节点?

Can anyone tell me why the xml_find_all function does not find any nodes in the latter example?

推荐答案

发生这种情况是因为您的宠物示例不包含命名空间,但第二个 XML 文件包含.

This happens because your pet example does not contain namespaces, but the second XML file does.

example %>% xml_ns()

d1  <-> http://uniprot.org/uniprot
d2  <-> http://uniprot.org/uniprot
d3  <-> http://uniprot.org/uniprot
d4  <-> http://uniprot.org/uniprot
d5  <-> http://uniprot.org/uniprot
d6  <-> http://uniprot.org/uniprot
d7  <-> http://uniprot.org/uniprot
d8  <-> http://uniprot.org/uniprot
d9  <-> http://uniprot.org/uniprot
d10 <-> http://uniprot.org/uniprot

由于每个条目具有相同的命名空间,在这种情况下,最简单的方法可能是剥离(删除)命名空间:

Since each entry has the same namespace, in this case the simplest approach is probably to strip (remove) the namespaces:

example %>% xml_ns_strip()

并且 xml_find_all 现在应该可以正常工作了:

And xml_find_all should now work as expected:

example %>% xml_find_all("//accession")

{xml_nodeset (10)}
 [1] <accession>Q6GZX4</accession>
 [2] <accession>Q6GZX3</accession>
 [3] <accession>Q197F8</accession>
 [4] <accession>Q197F7</accession>
 [5] <accession>Q6GZX2</accession>
 [6] <accession>Q6GZX1</accession>
 [7] <accession>Q197F5</accession>
 [8] <accession>Q6GZX0</accession>
 [9] <accession>Q91G88</accession>
[10] <accession>Q6GZW9</accession>

如果你想保留命名空间,你可以像这样访问访问:

If you wanted to retain the namespaces, you could access accessions like so:

example %>% xml_find_all("//d1:accession")

在这种情况下有效,因为为第一个条目的命名空间赋予的默认名称 d1 映射到所有条目的相同命名空间.

which works in this case because the default name d1 given to the namespace for the first entry maps to the same namespace for all entries.

这篇关于xml2 包 (R) 中的 xml_find_all 函数未找到相关节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆