Extract value of attribute node via XPath

后端 未结 7 1086
醉酒成梦
醉酒成梦 2020-11-28 01:59

How can I extract the value of an attribute node via XPath?

A sample XML file is:


  

        
7条回答
  •  情话喂你
    2020-11-28 02:18

    As answered above:

    //Parent[@id='1']/Children/child/@name 
    

    will only output the name attribute of the 4 child nodes belonging to the Parent specified by its predicate [@id=1]. You'll then need to change the predicate to [@id=2] to get the set of child nodes for the next Parent.

    However, if you ignore the Parent node altogether and use:

    //child/@name
    

    you can select name attribute of all child nodes in one go.

    name="Child_2"
    name="Child_4"
    name="Child_1"
    name="Child_3"
    name="Child_1"
    name="Child_2"
    name="Child_4"
    name="Child_3"
    

提交回复
热议问题