2016-09-29 16 views
0

鋸山の子どもの属性を取得する方法:インやW:ワットのすべてからrsidR値使用して、次のXMLでデルのアイテムを鋸山:私はワットを取得しようとしていますノードセット

<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> 

私は次のようにDOCXファイルを解凍するためにRubyの郵便番号を使用します。

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 

生産:適切rsidR:

<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> 

私はちょうどワットにアクセスできないようです。どうすればこれを達成できますか?私はノコギリで始まり、トラブルがあります。

答えて

0

あなたは、属性の値を取得するために@を使用することができます:w:del要素の中

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

w:r要素はw:rsidDel属性w:rsidR属性を持っていません。

+0

ありがとうございました。非常に役立ちます。 – chell

1

@yenshirakと同様に、w:delタグにはw:rsidDelしかありません。

だから、私はあなただけでこれを行うことができると思います。

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

は、それらの値の配列を取得します。

あなたはそれの前にputsを追加し、鋸山は値にto_sを呼び出すので、mapを削除し、それを印刷したい場合。

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

アドリアンに感謝します。私はあなたの答えをその優れたものとして投票しました。私はYenshirakを最初に受け入れる必要があります。私を助けてくれてありがとう。 – chell

+0

問題ありません。投票に感謝します。 – Adrian

関連する問題