I am trying to build a complex xpath expression which will answer the following condition.
From the XML data below, returns the User entity which:
- His loginname is ‘user1‘
- His name is ‘User 1‘
-
He has 2 different profiles values which are ‘operator‘ and ‘admin‘ (I don’t know the exact order ahead)
<user> <login>user1</login> <name>User 1</name> <profile> <value>admin</value> <id>2</id> <description>admin users</description> </profile> <profile> <value>operator</value> <id>1</id> <description>Operator</description> </profile> </user> <user> <login>user2</login> <name>User 2</name> <profile> <value>admin</value> <id>4</id> <description>admins users</description> </profile> <profile> <value>poweruser</value> <id>5</id> <description>power users</description> </profile> </user> </root>
Can someone please supply an example for such a case?
EDIT: Added a complex profile entity
The following should do what you’re after:
Having two tests for the
profilevalue might seem odd, but as there are multipleprofilenodes then the condition will be satisfied as long as at least one node matches the test.The reason you can compare
profiledirectly to astring, even though it actually is anodeis that thestring-valueof an element node is thestring-valueof all its descendants concatenated together, which in this case is just the contents ofvalue.If
profilecontained more elements thanvalueyou’d have to use a slightly more complex predicate test to determine the existence of a matchingprofilenode based just on thevalue(this should work with your updated question):