首页 > 解决方案 > R中的xml,删除段落但保留xml类

问题描述

我正在尝试从 R 中的 XML 文档中删除一些段落,但我想保留 XML 结构/类。这是一些示例文本和我失败的尝试:

library(xml2)
text = read_xml("<paper> <caption><p>The main title</p> <p>A sub title</p></caption> <p>The opening paragraph.</p> </paper>")
xml_find_all(text, './/caption//p') %>% xml_remove() # deletes text
xml_find_all(text, './/caption//p') %>% xml_text() # removes paragraphs but also XML structure

这是我想要结束的内容(只是删除了标题中的段落):

ideal_text = read_xml("<paper> <caption>The main title A sub title</caption> <p>The opening paragraph.</p> </paper>")
ideal_text

标签: rxmlxml2

解决方案


看起来这需要多个步骤。找到节点,复制文本,删除节点的内容,然后更新。

library(xml2)
library(magrittr)

text = read_xml("<paper> <caption><p>The main title</p> <p>A sub title</p></caption> <p>The opening paragraph.</p> </paper>")

# find the caption
caption <- xml_find_all(text, './/caption')

#store existing text
replacemement<- caption %>% xml_find_all( './/p') %>% xml_text() %>% paste(collapse = " ")

#remove the desired text
caption %>% xml_find_all( './/p') %>% xml_remove()

#replace the caption
xml_text(caption) <- replacemement
text  #test
    
{xml_document}
<paper>
   [1] <caption>The main title A sub title</caption>
   [2] <p>The opening paragraph.</p>

您很可能需要获取字幕节点的向量/列表,然后通过循环逐个遍历它们。


推荐阅读