【问题标题】:Parsing non-nested XML tags in R在 R 中解析非嵌套的 XML 标签
【发布时间】:2016-12-16 12:12:49
【问题描述】:

我正在尝试使用出色的 xml2 R 库解析大量文档。例如,考虑以下 XML 文件:

pg <- read_xml("https://www.theyworkforyou.com/pwdata/scrapedxml/westminhall/westminster2001-01-24a.xml")

其中包含许多 &lt;speech&gt; 标签,这些标签虽然没有嵌套在许多 &lt;minor-heading&gt;&lt;major-heading&gt; 标签中,但它们是分开的。我想将此文档处理为具有以下结构的data.frame

     major_heading_id  speech_text
     heading_id_1       text1
     heading_id_1       text2
     heading_id_2       text3
     heading_id_2       text4

不幸的是,因为标签没有嵌套,我不知道该怎么做!我有成功恢复相关信息的代码(见下文),但将语音标签与其各自的主要标题匹配是我无法做到的。

我的直觉是最好在标题标签处拆分 XML 文档,然后将每个文档作为单独的文档进行处理,但我在 xml2 包中找不到可以让我这样做的函数这个!

任何帮助都会很棒。

到目前为止我已经到达的地方:

speech_recs <- xml_find_all(pg, "//speech")
speech_text <- trimws(xml_text(speech_recs))

heading_recs <- xml_find_all(pg, "//major-heading")
major_heading_id <- xml_attr(heading_recs, "id")

【问题讨论】:

    标签: r xml xml2


    【解决方案1】:

    你可以这样做:

    require(xml2)
    require(tidyverse)
    doc <- read_xml("https://www.theyworkforyou.com/pwdata/scrapedxml/westminhall/westminster2001-01-24a.xml")
    
    # Get the headings
    heading_recs <- xml_find_all(doc, "//major-heading")
    
    # path creates the structure you want
    # so the speech nodes that have exactly n headings above them.
    path <- sprintf("//speech[count(preceding-sibling::major-heading)=%d]", 
                    seq_along(heading_recs))
    
    # Get the text of the speech nodes
    map(path, ~xml_text(xml_find_all(doc, .x))) %>% 
    # Combine it with the id of the headings
      map2_df(xml_attr(heading_recs, "id"), 
              ~tibble(major_heading_id = .y, speech_text = .x))
    

    这会导致:

    【讨论】:

      猜你喜欢
      • 2011-09-24
      • 2011-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多