2014-01-23 83 views
0

繼續進行TEI探索。這是我的XML Tei文件,其中包含具有不同屬性(從,到,到)的「日期」節點。 我已經成功提取「當」屬性的值BU使用R XML從同一個節點中選擇2個屬性xmlAttrs()

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

,但現在我想提取「從」的值,並在同一時間「到」屬性和xmlGetAttr不超過一個屬性。

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

但我還是把日期節點的所有屬性

而且也並沒有工作

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

與NULL響應

有人可以 幫一把 ?由於

回答

1

你不能做這樣的事

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

對於重複的例子,我將使用XML格式的PLOS一篇文章。

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"