R XML 从同一节点 xmlAttrs() 中选择 2 个属性 [英] R XML select 2 attributes from a same node xmlAttrs()

查看:35
本文介绍了R XML 从同一节点 xmlAttrs() 中选择 2 个属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 TEI 探索中走得更远.这是我的 XML Tei 文件,其中包含具有不同属性(何时、从、到)的日期"节点.我已经成功地使用

Going Further in TEI exploration. This is my XML Tei file containing "date" nodes with different attributes (when, from, to). I have sucessfully extracted the value of "when" attribute bu using

dateWhen<-unlist(xpathApply(doc, '//date', xmlGetAttr,"when"))

但现在我想同时提取from"和to"属性的值,而xmlGetAttr不超过一个属性.

but now I want to extract the values of "from" and "to" attributes at the same time and xmlGetAttr doesn't more than one attribute.

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="parser.xsl" type="text/xsl"?>
<TEI xmlns="http://www.tei-c.org/ns/1.0">
   <teiHeader>
      <fileDesc>
      </fileDesc>
   </teiHeader>
   <text>
       <body>
           <p><date when="1715-01-07">Du 7e  Janvier.</date> Un ambassadeur extraordinaire du roi.</p>
           <p><date from="1715-12-13" to="1715-12-27">Dudit mois de décembre</date> Quelque temps avant la fin du mois</p>
       </body>
   </text>
</TEI>

我试过了

dateFromTo<-unlist(xpathApply(doc, '//date', xmlAttrs,c("from","to")))

但我获取了日期节点的所有属性

but I took all attributes of date nodes

而且这也不起作用

frames<-getNodeSet(doc, '//date')
dateFromTo<-xmlAttrs(frames[[1]]) [c('from','to')]

NULL 响应

有人可以帮忙吗?谢谢

推荐答案

你不能这样做吗

sapply(c("when","from"), function(x) xpathSApply(doc, '//date', xmlGetAttr, x))

对于可重复的示例,我将使用 xml 格式的 PLOS One 文章.

For a reproducible example, I'll use a PLOS One article in xml format.

library(XML)
doc <- xmlParse("http://www.plosone.org/article/fetchObjectAttachment.action?uri=info%3Adoi%2F10.1371%2Fjournal.pone.0084312&representation=XML")
sapply(c("contrib-type","xlink:type"), function(x) xpathSApply(doc, '//contrib', xmlGetAttr, x))

    contrib-type xlink:type
[1,] "author"     "simple"  
[2,] "author"     "simple"  
[3,] "author"     "simple"  
[4,] "author"     "simple"  
[5,] "author"     "simple"  
[6,] "editor"     "simple"

这篇关于R XML 从同一节点 xmlAttrs() 中选择 2 个属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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