仅当下一个元素是特定类型时如何使用 Nokogiri select 一个元素

How to select an element only if the next element is of a specific type using Nokogiri

给出以下 XML:

<w:p>
   <w:pPr>
      <w:lang w:val="en-CA"/>
   </w:pPr>
   <w:ins>
        <w:t>sections</w:t>
   </w:ins>
   <w:pPr>
      <w:lang w:val="en-CA"/>
   </w:pPr>
   <w:r>
     <w:t>I am</w:t>
   </w:r>
</w:p>

我想 select w:pPr 元素后跟 w:insw:del 元素。

我试过了:

doc.xpath("w:pPr[following-sibling::w:ins[1] or following-sibling::w:del[1]]")

这仍然是 returns 第二个 w:pPr 元素,后面跟着一个 w:r 元素,所以这不是我要找的。

我该怎么做?

试试这个 XPath 表达式:

//w:pPr[following-sibling::*[position()=1][name()='w:del' or name()='w:ins']]