Вопрос

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.

Это было полезно?

Решение

How about

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

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top