Extract value of attribute node via XPath Extract value of attribute node via XPath xml xml

Extract value of attribute node via XPath


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

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


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.


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.