xpath parent attribute of selection

前端 未结 3 831
不思量自难忘°
不思量自难忘° 2021-02-06 19:26

Syntax of the xml document:


  
    Z 
    Z__2
    Z__3   
           


        
3条回答
  •  星月不相逢
    2021-02-06 19:40

    You already have a good accepted answer, but here are some other helpful expressions:

    • //z/ancestor::x/@name - Find elements anywhere, then find all the ancestor elements, and then the name="…" attributes of them.

    • //z/../../@name - Find the elements, and then find the parent node(s) of those, and then the parent node(s) of those, and then the name attribute(s) of the final set.

      • This is the same as: //z/parent::*/parent::*/@name, where the * means "an element with any name".
    • The // is useful, but inefficient. If you know that the hierarchy is x/y/z, then it is more efficient to do something like //x[y/z]/@name

提交回复
热议问题