0

我正在使用 xml2 和 rvest 在 R 中读取 XML 文件。XML 具有以下结构(不包括标题)。我想提取两者之间的所有文本,<w:p></w:p>但首先我想将所有文本都转换<w:br/>为空格。

<w:p><w:r><w:t>First bit of text</w:t></w:r><w:r><w:br/><w:t>Thank you!</w:t></w:r></w:p>

当我使用以下代码时(使用完全合法的 xml)

 xml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>   
   <w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex" xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex" xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex" xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex" xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 w16se wp14">
   <w:body><w:p w:rsidR="00C87F35" w:rsidRDefault="008836BC" w:rsidP="008836BC"><w:pPr>
   <w:pStyle w:val="Heading1"/></w:pPr>  
   <w:r><w:t>Example .</w:t></w:r>
   <w:proofErr w:type="spellStart"/><w:r><w:t>docx</w:t></w:r><w:proofErr w:type="spellEnd"/>
   <w:r><w:t xml:space="preserve"> file</w:t></w:r></w:p>
   <w:p w:rsidR="008836BC" w:rsidRDefault="008836BC" w:rsidP="008836BC">
   <w:r><w:t>This is an example .</w:t></w:r>
   <w:proofErr w:type="spellStart"/>
   <w:r><w:t>docx</w:t></w:r><w:proofErr w:type="spellEnd"/>
   <w:r><w:t xml:space="preserve"> file included with the ‘&lt;/w:t></w:r>  
   <w:proofErr w:type="spellStart"/><w:r>
   <w:t>readOffice</w:t></w:r>
   <w:proofErr w:type="spellEnd"/> 
   <w:r><w:t>’ package to demonstrate functionality.</w:t></w:r></w:p>
   <w:p w:rsidR="008836BC" w:rsidRPr="008836BC" w:rsidRDefault="008836BC" w:rsidP="008836BC">
   <w:r><w:t>There is nothing exciting in this file!</w:t></w:r>
   <w:r><w:br/><w:t>Thank you!</w:t></w:r>
   <w:bookmarkStart w:id="0" w:name="_GoBack"/>
   <w:bookmarkEnd w:id="0"/></w:p>
   <w:sectPr w:rsidR="008836BC" w:rsidRPr="008836BC">
   <w:pgSz w:w="12240" w:h="15840"/>
   <w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720" w:gutter="0"/>
   <w:cols w:space="720"/>
   <w:docGrid w:linePitch="360"/></w:sectPr>
   </w:body></w:document>'


    xml2::read_xml(xml) %>% 
      rvest::xml_nodes('w\\:p') %>% 
      xml2::xml_text()

结果是:

[1] "Example .docx file"                                                                                                 
[2] "This is an example .docx file included with the \u0091readOffice\u0092 package to demonstrate functionality."       
[3] "There is nothing exciting in this file!Thank you!"

但是换行符<w:br/>刚刚消失,最后的感叹号和谢谢这个词之间没有空格。

在实际应用程序中,我正在读取 XML 文件,而不是字符串(使用该read_xml函数),因此这不是gsub我正在寻找的简单解决方案。或者也许是因为这是唯一的解决办法。但我想知道的是,如何使用 rvest 和 xml2 将特定标签转换为空白?

更新

因此,建议normalize-space在另一个答案中将该功能用作 xpath。

paragraphs = xml2::read_xml(xml) %>% 
  rvest::xml_nodes('w\\:p')
purrr::map(paragraphs,function(x){
  paste(xml2::xml_text(rvest::xml_nodes(x,xpath=".//text()[normalize-space()]")),collapse=" ")
})

但是,这不会产生所需的结果,因为文本在每个标签上都被拆分<w:r><w:t>因此现在引入了额外的空格。请注意,在前两个元素中,“.docx”中有一个空格,第二个元素中,“readOffice”中引入了空格。

[[1]]
[1] "Example . docx  file"

[[2]]
[1] "This is an example . docx  file included with the ‘ readOffice ’ package to demonstrate functionality."

[[3]]
[1] "There is nothing exciting in this file, but if you’re reading it, it means you installed my package! Thank you!"

我知道空格是由于我使用的,collapse=" "但如果我使用collapse="",那么结果与原始代码没有变化。

4

1 回答 1

1

这可能不再需要,但您可以用w:br换行符替换每个节点的(空)文本,然后提取整个文本:

library(rvest)
library(purrr)

read_xml(xml) %>% 
    xml_nodes('w\\:p') %>% 
    map(~{
        xml_nodes(.x, 'w\\:br') %>% `xml_text<-`('\n')

        xml_text(.x)
    }) -> r

cat(r[[3]])
#> There is nothing exciting in this file!
#> Thank you!
于 2017-06-19T09:06:35.637 回答