XPath : select all following siblings until another sibling

前端 未结 3 1164
没有蜡笔的小新
没有蜡笔的小新 2020-11-27 05:54

Here is an excerpt of my xml :



content





        
3条回答
  •  自闭症患者
    2020-11-27 06:18

    Simpler than the accepted answer:

    //node[@id='1']/following-sibling::node[following::node[@id='2']]
    
    • Find a node anywhere whose id is '1'
    • Now find all the following sibling node elements
    • ...but only if those elements also have a node with id="2" somewhere after them.

    Shown in action with a more clear test document (and legal id values):

    xml = '
    
    content
    
    content
    
    '
    
    # A Ruby library that uses libxml2; http://nokogiri.org
    require 'nokogiri'; doc = Nokogiri::XML(xml)
    
    expression = "//node[@id='c']/following-sibling::node[following::node[@id='g']]"
    puts doc.xpath(expression)
    #=> 
    #=> 
    #=> 
    

提交回复
热议问题