XPath select all elements between two specific elements

前端 未结 4 603
离开以前
离开以前 2020-11-27 05:17

I have a following xml:


    
    

text

text

text

te

相关标签:
4条回答
  • 2020-11-27 05:22

    I think there's a much simpler and probably faster solution: you want all preceding siblings of the second divider that have at least one preceding sibling divider:

    /doc/divider[2]/preceding-sibling::p[preceding-sibling::divider]
    

    It gets a bit more complex, of course, if you want to find the paras between the second and third dividers: then you want something more like Daniel Haley's solution.

    0 讨论(0)
  • 2020-11-27 05:34

    Same concept as bytebuster, but a different xpath:

    /*/p[count(preceding-sibling::divider)=1]
    
    0 讨论(0)
  • 2020-11-27 05:37

    What about selecting all p having exactly one element divider as preceding-sibling ?

    //doc/p[preceding-sibling::divider[1] and not (preceding-sibling::divider[2])]
    
    0 讨论(0)
  • 2020-11-27 05:49

    Here is a general XPath expression:

    /*/divider[$k]
        /following-sibling::p
           [count(.|/*/divider[$k+1]/preceding-sibling::p)
           =
            count(/*/divider[$k+1]/preceding-sibling::p)
           ]
    

    If you substitute $k with 1 then exactly the wanted p nodes are selected.

    if you substitute $k with 2 then all p elements between the 2nd and 3rd divider , ..., etc.

    Explanation:

    This is a simple application of the Kayessian XPath 1.0 formula for node-set intersection:

    $ns1[count(.|$ns2) = count($ns2)]
    

    selects all the nodes that belong both to the nodesets $ns1 and $ns2.

    In this specific case we substitute $ns1 with:

    /*/divider[$k]/following-sibling::p
    

    and we substitute $ns2 with:

    /*/divider[$k+1]/preceding-sibling::p
    
    0 讨论(0)
提交回复
热议问题