Reputation: 21
Consider an XML element as
<Person>
<contact phone="123" email="abc" address="abcde">Sid</contact>
<contact phone="987" email="cba" address="qwerty">Zid</contact>
</Person>
I want an xpath which will print the contact node which has the first attribute="123" regardless of the attribute name. something like /Person/contact[@*[position()=1]="123"]. In this case, it should print "Sid".
Upvotes: 2
Views: 653
Reputation: 167696
If you use /contact/@*[1]
you might get what you want. Remember however that attributes are not ordered (respectively "The relative order of attribute nodes is implementation-dependent.") so different implementations might return a different attribute for a positional predicate [1]
on @*
.
The XPath /Person/contact[@* = '123']
might address your edited requirement, as it selects a contact
element which has any attribute with value '123'
. As already pointed out, you can also use a positional predicate /Person/contact[@*[1] = '123']
but the result can be implementation dependent.
Upvotes: 2
Reputation: 89305
Assuming that you're aware of possible problem caused by relying your logic on order of attribute as mentioned by Martin Honnen, but still want to go this way, you can try the following :
/Person/contact[@*[1][.='123']]
above XPath select <contact>
elements having first attribute value equals 123
, regardless of the attribute name.
Upvotes: 0