Domanda

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.

È stato utile?

Soluzione

How about

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

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top