在R中选择特定的XML节点?

Shr*_*nik 3 xml r

我正在使用XMLpackage R来解析XML具有以下结构的文件.

 <document id="Something" origId="Text">
    <sentence id="Something" origId="thisorig" text="Blah Blah.">
    <special id="id.s0.i0" origId="1" e1="en1" e2="en2" type="" directed="True"/>
    </sentence>
     <sentence id="Something" origId="thisorig" text="Blah Blah.">
      </sentence>
</document>
Run Code Online (Sandbox Code Playgroud)

我想</special>在一个变量中选择具有标记的节点,</special>在其他变量中选择没有标记的节点.

有可能用R任何指针/答案来做这将是非常有帮助的.

Die*_*nne 6

我添加了一些案例来测试异常:

<document id="Something" origId="Text">
    <sentence id="Something" origId="thisorig" text="Blah Blah.">
    <special id="id.s0.i0" origId="1" e1="en1" e2="en2" type="" directed="True"/>
    </sentence>
    <sentence id="Else" origId="thatorig" text="Blu Blu.">
      <special id="id.s0.i1" origId="1" e1="en1" e2="en2" type="" directed="True"/>
    </sentence>
     <sentence id="Something" origId="thisorig" text="Blah Blah.">
       <notso id = "hallo" />
      </sentence>
     <sentence id="Something no sentence" origId="thisOther" text="Blah Blah.">
      </sentence>
</document>

library(XML)
doc = xmlInternalTreeParse("sentence.xml")
hasSentence = xpathApply(doc, "//sentence/special/..")
xpathApply(doc, "/document/sentence[not(child::special)]")
Run Code Online (Sandbox Code Playgroud)