Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgresql xpath Searching for attribute and element combination

I was able to use Postgresql(9.4.x) xpath searching to match against an xml attribute or element. Is it possible to search the combination of both attribute and element value?

<name>
  <firstname>test</firstname>
  <lastname>user</lastname>
  <role num="10">admin</role>
  <role num="8">readonly</role>
</name>

I was trying to match admin role with number 10 in the above sample xml and below query does return TRUE for 8-admin combination as well.

select xpath('//role/@num="8" and //role/text()="admin"', '<above xml>');

Please suggest if there is any better way to matching exact index combination.

Thanks.

like image 673
Prasad Avatar asked Jun 30 '26 06:06

Prasad


1 Answers

I was trying to match admin role with number 10

Try:

'//role[@num="10" and text()="admin"]'

Or if the name element for the admin is requested:

'//name[role[@num="10" and text()="admin"]]'
like image 158
hr_117 Avatar answered Jul 02 '26 19:07

hr_117



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!