我在XML
中使用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>
我想在一个变量中选择其中包含</special>
标记的节点,在其他变量中选择不包含</special>
标记的节点。
是否可以使用R
来完成任何指针/答案将非常有帮助。
答案 0 :(得分: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)]")
答案 1 :(得分:1)
解析xml树,使用xpath指定节点的位置。
doc <- xmlTreeParse("test.xml", useInternalNodes = TRUE)
special_nodes <- getNodeSet(doc, "/document//special")