2017-02-28 100 views
0

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

<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将特定标记转换为空格?

UPDATE

于是有人建议使用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=""那么结果是从原来的代码不变。

+1

可能的复制](http://stackoverflow.com/questions/42003932/adding-whitespace-to-text-elements) - 你可以使用我在那里提到的相同功能。如果您不想使用rvest,只需使用xml_ *替换html_ *函数 – Rentrop

+0

@ Floo0您在函数中使用的xpath将打碎所有标记上的文本,而不仅仅是指定的标记,因此它会将文本分解得太多。 – Mark

回答

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!