0

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.

Filburt
  • 17,626
  • 12
  • 64
  • 115
jaco0646
  • 15,303
  • 7
  • 59
  • 83

1 Answers1

1

How about

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

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

Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
  • That's exactly what I was looking for. I thought it would be something simple. Once I knew what to search, I quickly found: [xpath-punctuation](http://developer.marklogic.com/blog/xpath-punctuation-part-1). Thanks. – jaco0646 Dec 19 '13 at 16:37