2016-09-29 64 views
0

我试图让W的属性:从所有W的rsidR值:插件和W:使用德尔项下面的XML引入nokogiri:如何获得一个引入nokogiri的孩子节点集

<w:document mc:Ignorable="w14 w15 wp14" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mo="http://schemas.microsoft.com/office/mac/office/2008/main" xmlns:mv="urn:schemas-microsoft-com:mac:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape"> 
    <w:body> 
     <w:p w14:paraId="56037BEC" w14:textId="1188FA30" w:rsidR="001665B3" w:rsidRDefault="008B4AC6"> 
      <w:r> 
       <w:t xml:space="preserve">This is the story of a man who </w:t> 
      </w:r> 
      <w:ins w:author="Mitchell Gould" w:date="2016-09-28T09:15:00Z" w:id="0"> 
       <w:r w:rsidR="003566BF"> 
        <w:t>went</w:t> 
       </w:r> 
      </w:ins> 
      <w:del w:author="Mitchell Gould" w:date="2016-09-28T09:15:00Z" w:id="1"> 
       <w:r w:rsidDel="003566BF"> 
        <w:delText>goes</w:delText> 
       </w:r> 
      </w:del> 
      <w:r> 
       <w:t xml:space="preserve">to the store to </w:t> 
      </w:r> 
      <w:ins w:author="Mitchell Gould" w:date="2016-09-28T09:15:00Z" w:id="2"> 
       <w:r w:rsidR="003566BF"> 
        <w:t>purchase</w:t> 
       </w:r> 
      </w:ins> 
      ... 
     </w:p> 
    </w:body> 
</w:document> 

我使用Ruby邮编解压缩的docx文件如下:

zip = Zip::File.open("test.docx") 
doc = zip.find_entry("word/document.xml") 
file = Nokogiri::XML.parse(doc.get_input_stream) 

到目前为止,我有以下几点:

file.xpath('//w:ins').each do |n| 
    puts n.children 
    puts n.children.attr('w:rsidR') 
end 

哪个产生:

<w:r w:rsidR="003566BF"> 
    <w:t>went</w:t> 
</w:r> 

<w:r w:rsidR="003566BF"> 
    <w:t>purchase</w:t> 
</w:r> 

<w:r w:rsidR="008C3761"> 
    <w:t>replace</w:t> 
</w:r> 

<w:r w:rsidR="009D3E86"> 
    <w:t>place</w:t> 
</w:r> 

<w:r w:rsidR="00F633DF"> 
    <w:t xml:space="preserve">was </w:t> 
</w:r> 

<w:r w:rsidR="00D46E57"> 
    <w:t>was</w:t> 
</w:r> 

<w:r w:rsidR="00F56399"> 
    <w:t xml:space="preserve"> sat</w:t> 
</w:r> 

我似乎无法正常访问w:rsidR。我怎样才能做到这一点?我只是开始与Nokogiri和麻烦。

回答

0

您可以使用@得到一个属性的值:

file.xpath('//w:ins/w:r/@w:rsidR|//w:del/w:r/@w:rsidDel').each do |id| 
    puts id 
end 

w:del元素中的w:r元素没有一个w:rsidR属性只有w:rsidDel属性。

+0

非常感谢yenshirak。很有帮助。 – chell

1

就像@yenshirak说的那样,w:del标签中只有w:rsidDel

所以,我认为你可以只是这样做:

file.xpath('//w:ins//@w:rsidR|//w:del//@w:rsidDel').map(&:value) 

得到他们的值的数组。

如果要打印它,只需在其前面添加puts并删除map,因为Nokogiri在值上调用to_s

puts file.xpath('//w:ins//@w:rsidR|//w:del//@w:rsidDel') 
+0

谢谢阿德里安。我已经把你的答案评为优秀。我必须接受Yenshirak's,因为它是第一个。谢谢你的协助。 – chell

+0

没问题。感谢您的投票。 – Adrian

相关问题