How to select a node using XPath if sibling node has a specific value?

后端 未结 6 1105
温柔的废话
温柔的废话 2020-12-04 08:15

I have the following document:


  abc
  ccc
  
ffffd
zz
6条回答
  •  一生所求
    2020-12-04 09:09

    First off, your example is not well-formed XML. Overlooking that and that you didn't describe your intents very well (What exactly do you want to select on which condition?), I assume you want to do this:

    //cc[preceding-sibling::bb[text()="zz"]]/text()
    

    It selects

    TEXT VALUES OF ALL  ELEMENTS
    //cc                                    /text()
        THAT HAVE A PRECEDING SIBLING 
        [preceding-sibling::bb             ]
                              THAT HAS TEXT VALUE EQUAL TO "zz"
                              [text()="zz"]
    

    You could write is also as

    //bb[text()="zz"]/following-sibling::cc/text()
    

    Please look at the spec, it has some very well readable examples from which you'll learn a lot.

提交回复
热议问题