Skip to content Skip to sidebar Skip to footer

Xpath Executing Complex Queries

How can I execute following queries on the given xml: if(task1=cde && task2=abc) I would get id=XYZ-987 if((task1=abc && task2=efg) || task5=nop) I would get id=AB

Solution 1:

The reason why your xpath doesn't work is because your predicate [(@name='task1' and @value='cde') and (@name='task2' and @value='abc')] executes on the same task node, and if @name='task1' then @name can never be = 'task2'.

Instead you should create predicates that will operate on/check all the relevant nodes like this:

1.

if(task1=cde && task2=abc) I would get id=XYZ-987

/node1/node2/node3[condition/task[@name='task1' and @value='cde'] and condition/task[@name='task2' and @value='abc']]/id

2.

if((task1=abc && task2=efg) || task5=nop) I would get id=ABC-123,XYZ-987,RST-567

/node1/node2/node3[condition/task[@name='task1' and @value='abc'] and condition/task[@name='task2' and @value='efg'] or condition/task[@name='task5' and @value='nop']]/id

you can include your brackets if you want, but in boolean operations, and arguments are always evaluated before or arguments, so they are unnecessary in this case: /node1/node2/node3[(condition/task[@name='task1' and @value='abc'] and condition/task[@name='task2' and @value='efg']) or condition/task[@name='task5' and @value='nop']]/id

Post a Comment for "Xpath Executing Complex Queries"