Pregunta

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.

¿Fue útil?

Solución

How about

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

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top