我在R中使用 XML包来解析具有以下结构的 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"/ /
<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>
我想选择具有< / special>的节点在一个变量和没有< / special>的节点中标记它们标记在其他变量中.
是否可以用R做任何指针/答案将非常有帮助.
我添加了一些案例来测试异常:<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)]")
