R - 使用rvest用空格替换xml标签

时间:2017-02-28 18:25:29

标签: r rvest xml2

我正在使用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 ‘</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>的每个标记上拆分,因此现在会引入额外的空格。请注意,在前两个元素中,“。dococ”中有一个空格,而在第二个元素中,“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="",则结果与原始代码相同。

1 个答案:

答案 0 :(得分: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!