Skip to content Skip to sidebar Skip to footer

Xpath Parsing Issue With Complex Data

How can I fetch IDs with condition name='task1', value='abc' AND name='task2', value='efg' name='task5', value='nop' Expected Output: ABC-123; XYZ-987 Actual Output: XYZ-987 Query:

Solution 1:

/node1/node2/node3[count(condition/task[not(@name='task1' and @value='abc') and not(@name='task2' and @value='efg') and @operation]) = count(condition/task[@operation='or'])]/id

will return all ids where:

  • the number of tasks that have an @operation attribute and aren't matched by the conditions @name='task1' and @value='abc' and @name='task2' and @value='efg'
  • equals the number of tasks where @operation = 'or'

which is ABC-123 and XYZ-987

Solution 2:

you have to re-structure your conditions:

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

Parentheses () are optional in this case but they improve readability. note that as per the sample data you provided, LMN-543 also satisfies the query.

Returns

<id>ABC-123</id>
<id>LMN-543</id>
<id>XYZ-987</id>

I'm not very clear on your 'catch' condition, but based on what you say, it can be translated in each node having at most 2 operation="and" task. This makes and additional

and (count(condition/task[@operation='and']) < 3)

so the xpath becomes

/node1/node2/node3[(condition/task/@name='task1'andcondition/task/@value='abc') and (condition/task/@name='task2'andcondition/task/@value='efg')  and (condition/task/@name='task5'andcondition/task/@value='nop') and (count(condition/task[@operation='and']) <3)]/id

which returns:

<id>ABC-123</id>
<id>XYZ-987</id> 

Post a Comment for "Xpath Parsing Issue With Complex Data"