xml - Extract value of attribute node via XPath

ID : 10285

viewed : 27

Tags : xmlxpathxml

Top 5 Answer for xml - Extract value of attribute node via XPath

vote vote

96

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

Your original child[@name] means an element child which has an attribute name. You want child/@name.

vote vote

85

To get just the value (without attribute names), use string():

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

The fn:string() fucntion will return the value of its argument as xs:string. In case its argument is an attribute, it will therefore return the attribute's value as xs:string.

vote vote

79

You should use //Parent[@id='1']/Children/child/data(@name)

The attributes can not be serialized so you can't return them in an xml looking result. What you need to do is obtain the data from the attribute using data() function.

vote vote

66

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" 
vote vote

55

//Parent/Children[@  Attribute='value']/@Attribute 

This is the case which can be used where element is having 2 attribute and we can get the one attribute with the help of another one.

Top 3 video Explaining xml - Extract value of attribute node via XPath

Related QUESTION?