xpath select elements between two nodes

前端 未结 1 1544
既然无缘
既然无缘 2021-01-24 02:38

This is my HTML:

相关标签:
1条回答
  • 2021-01-24 02:56

    The xpath is going to be a long one so brace yourself:

    count(//tr[preceding-sibling::tr/td[@class = 'd2']][count(.|//tr[following-sibling::tr/td[@class = 'd2']])=count(//tr[following-sibling::tr/td[@class = 'd2']])])
    

    To select the actual nodes and not have just the count, simply remove the first count :

    //tr[preceding-sibling::tr/td[@class = 'd2']][count(.|//tr[following-sibling::tr/td[@class = 'd2']])=count(//tr[following-sibling::tr/td[@class = 'd2']])]
    

    There are various things happening here notably:

    • Select start node by selecting preceding sibling's child node tr/td with id='2'
    • Select end node by selecting following sibling's child node tr/td with id='2'
    • Use kaycian method : http://www.dpawson.co.uk/xsl/sect2/muench.html#d9940e108 to get the intersection between the two nodes.
    0 讨论(0)
提交回复
热议问题