Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xpath expression with multiple predicates

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:

  1. His loginname is "user1"
  2. His name is "User 1"
  3. 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

like image 302
user41767 Avatar asked Feb 20 '09 08:02

user41767


People also ask

How do you combine two XPath expressions?

selects the union of all nodes selected by expr1 and all nodes selected by expr2. The | character denotes the XPath union operator. You can use the union operator in any case when you want the union of the nodes selected by several XPath expressions to be returned.

What is the purpose of a predicate in an XPath expression?

Predicate refers to the XPath expression written in square brackets. It refers to restrict the selected nodes in a node set for some condition.

Which type of XML database is queried using the XPath expressions?

It can store large amount of XML document and data. Native XML database is queried by the XPath-expressions. Native XML database has an advantage over the XML-enabled database. It is highly capable to store, query and maintain the XML document than XML-enabled database.

What is XPath in Oracle?

XPath (XML Path Language) is a query language that can be used to query data from XML documents. In RUEI, XPath queries can be used for content scanning of XML documents. A complete specification of XPath is available at http://www.w3.org/TR/xpath .


2 Answers

The following should do what you're after:

/root/user[login='user1' and             name='User 1' and             profile='admin' and            profile='operator'] 

Having two tests for the profile value might seem odd, but as there are multiple profile nodes then the condition will be satisfied as long as at least one node matches the test.

The reason you can compare profile directly to a string, even though it actually is a node is that the string-value of an element node is the string-value of all its descendants concatenated together, which in this case is just the contents of value.

If profile contained more elements than value you'd have to use a slightly more complex predicate test to determine the existence of a matching profile node based just on the value (this should work with your updated question):

/root/user[login='user1' and             name='User 1' and             profile[value='admin'] and            profile[value='operator']] 
like image 67
Greg Beech Avatar answered Sep 22 '22 09:09

Greg Beech


Here is a more exact answer (at present Greg Beech's answer does not check for condition 3. in the problem: the user element must have exactly 2 profile children):

/*/user         [login='user1'          and                      name='User 1'          and            not(profile[3])         and                    profile/value='admin'          and                     profile/value='operator'          ] 
like image 30
Dimitre Novatchev Avatar answered Sep 24 '22 09:09

Dimitre Novatchev