2016-12-14 3 views
0
<w:p> 
    <w:r> 
     <w:t>The table predicted, with </w:t> 
    </w:r> 
    <w:ins w:author="RKH RKH" w:date="2016-11-06T17:53:00Z" w:id="0"> 
     <w:r> 
      <w:t>impressive</w:t> 
     </w:r> 
    </w:ins> 
    <w:del w:author="RKH RKH" w:date="2016-11-06T17:53:00Z" w:id="1"> 
     <w:r w:rsidDel="001F31B2" w:rsidRPr="001F31B2"> 
      <w:delText>stunning</w:delText> 
     </w:r> 
    </w:del> 
</w:p> 
<w:p> 
    <w:r> 
     <w:t>The man started </w:t> 
    </w:r> 
    <w:ins w:author="RKH RKH" w:date="2016-11-06T17:53:00Z" w:id="0"> 
     <w:r> 
      <w:t>to run.</w:t> 
     </w:r> 
    </w:ins> 
    <w:del w:author="RKH RKH" w:date="2016-11-06T17:53:00Z" w:id="1"> 
     <w:r w:rsidDel="001F31B2" w:rsidRPr="001F31B2"> 
      <w:delText>to hike.</w:delText> 
     </w:r> 
    </w:del> 
</w:p> 

내가 단락을 얻을 요소 및 루프에서 텍스트를 포함하지 않고 단락에서 텍스트를 얻는 방법.그들을 통해 일부 아동은 다음과 같이 노코 기리가

@all_paragraph_nodes = @file.xpath('//w:p') 

@all_paragraph_nodes.each_with_index do |p, index| 
... 

은 내가 <w:del> 요소의 내부 텍스트를 제외한 루프에서 각 단락에서 텍스트를 얻을합니다.

Nokogiri를 사용하여 어떻게 할 수 있습니까?

답변

1

w:p의 하위 요소를 모두 선택한 다음 not()name()이라는 조건자를 사용하여 w:del 요소를 필터링 할 수 있습니다.

@all_paragraph_nodes.each_with_index do |p, index| 
    text_nodes = p.xpath("*[not(name(.)='w:del')]//text()") 
    # ... process however you want 
end 

아마도 빈 텍스트 노드를 제거해야 할 것입니다. Ruby에서이 작업을 수행하거나 XPath에서 필터링 할 수 있습니다. normalize-space()

p.xpath("*[not(name(.)='w:del')]//text()[normalize-space()]") 
+0

매트 감사합니다. 완벽하게 작동합니다. nokogiri와 함께 일하는 법을 배웠습니까? noobs에 대한 튜토리얼을 많이 찾지 못하는 것 같습니다. – chell