Question

Given this simple xml:

<root>  
    <child id="foo" />  
    <child id="bar" />  
</root>

I want to select the attribute with value = foo. This xpath works: root/child[@id='foo']/@id but it seems there should be a way to accomplish the same without redundantly specifying the attribute name. Is it possible?

I've seen many discussions on finding an element using an attribute value; but in this case I want to return the attribute itself.

Was it helpful?

Solution

How about

root/child/@id[. = 'foo']

A dot means the current node, it doesn't necessarily have to be an element.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top