delaye
delaye

Reputation: 1399

R XML select 2 attributes from a same node xmlAttrs()

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"))

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>

I'v tried this

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

but I took all attributes of date nodes

And also this didn't work

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

with NULL respond

Can someone give a hand ? Thanks

Upvotes: 0

Views: 1325

Answers (1)

sckott
sckott

Reputation: 5893

Couldn't you do something like this

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

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"

Upvotes: 1

Related Questions