3

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.

1 Answer 1

6

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"]]'
Sign up to request clarification or add additional context in comments.

1 Comment

That worked. Thank much for your help.I need to tweak my query little bit as I was using this in where clause and my previous query returned true/false where as this query returned value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.