XPATH解析复杂数据的问题 [英] XPATH parsing issue with complex data
问题描述
我该如何获取条件名称为"="task1",值="abc"和名称="task2",值="efg",名称="task5",值="nop"的ID
How can I fetch IDs with condition name="task1", value="abc" AND name="task2", value="efg" name="task5", value="nop"
预期输出:ABC-123; XYZ-987
实际输出:XYZ-987
查询:/node1/node2/node3[condition/task[@name='task1' and @value='abc'] and condition/task[@name='task2' and @value='efg'] and condition/task[@name='task5' and @value='nop'] and count(condition/task)=3]/id
Expected Output: ABC-123; XYZ-987
Actual Output: XYZ-987
Query: /node1/node2/node3[condition/task[@name='task1' and @value='abc'] and condition/task[@name='task2' and @value='efg'] and condition/task[@name='task5' and @value='nop'] and count(condition/task)=3]/id
捕获,如果任务中除满足条件的任务以外的所有操作均为"OR",则该ID也应视为满足条件.在下面的XML中,ABC-123,LMN-543和XYZ-987满足条件,但ABC-123具有所有其他操作="OR".因此,这也应包括在结果中.通过使用count,我限制了3个任务,因此不包括ABC-123.使用count> 3将获取ABC-123,LMN-543和XYZ-9876.
Catch if all the operation in tasks, other than satisfied tasks, are "OR" then that ID should also be considered as a satisfied condition. In the XML below, ABC-123, LMN-543 and XYZ-987 satisfy the condition but ABC-123 has all other operation="OR". So this should also be included in the result. By using count, I restrict to 3 tasks so ABC-123 is not included. Using count>3 will fetch ABC-123, LMN-543 and XYZ-9876.
需要输出为ABC-123; XYZ-987 在operation = or
Need Output as ABC-123; XYZ-987 using condition check on operation=or
以下是所使用的XML
Following is the XML used
<node1>
<node2>
<node3>
<id>ABC-123</id>
<condition>
<task name="task1" operation="and" value="abc" />
<task name="task2" operation="and" value="efg" />
<task name="task3" operation="or" value="hij" />
<task name="task4" operation="or" value="klm" />
<task name="task5" operation="or" value="nop" />
<task name="task6" value="uvw" />
</condition>
</node3>
<node3>
<id>LMN-543</id>
<condition>
<task name="task1" operation="and" value="abc" />
<task name="task2" operation="and" value="efg" />
<task name="task3" operation="and" value="hij" />
<task name="task4" operation="or" value="klm" />
<task name="task5" operation="or" value="nop" />
<task name="task6" value="uvw" />
</condition>
</node3>
<node3>
<id>XYZ-987</id>
<condition>
<task name="task1" operation="and" value="abc" />
<task name="task2" operation="and" value="efg" />
<task name="task5" operation="or" value="nop" />
</condition>
</node3>
<node3>
<id>RST-567</id>
<condition>
<task name="task1" operation="and" value="abc" />
<task name="task2" operation="and" value="efg" />
<task name="task8" operation="and" value="jkl" />
<task name="task9" operation="and" value="rst" />
<task name="task10" value="xyz" />
</condition>
</node3>
<node3>
<id>PQR-345</id>
<condition>
<task name="task1" operation="and" value="ijk" />
<task name="task2" operation="and" value="klm" />
<task name="task8" operation="and" value="jkl" />
<task name="task9" operation="and" value="rst" />
</condition>
</node3>
</node2>
</node1>
推荐答案
您必须重新构建条件:
/node1/node2/node3[(condition/task/@name='task1' and condition/task/@value='abc') and (condition/task/@name='task2' and condition/task/@value='efg') and (condition/task/@name='task5' and condition/task/@value='nop')]/id
在这种情况下,parentheses()是可选的,但是它们提高了可读性. 请注意,根据您提供的样本数据,LMN-543也满足查询条件.
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.
返回
<id>ABC-123</id>
<id>LMN-543</id>
<id>XYZ-987</id>
我对您的捕获"情况不是很清楚,但是根据您的说法,可以在每个节点最多具有2个operation ="and"任务的节点中进行翻译. 这使得和其他
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)
因此xpath变为
/node1/node2/node3[(condition/task/@name='task1' and condition/task/@value='abc') and (condition/task/@name='task2' and condition/task/@value='efg') and (condition/task/@name='task5' and condition/task/@value='nop') and (count(condition/task[@operation='and']) < 3)]/id
返回:
<id>ABC-123</id>
<id>XYZ-987</id>
这篇关于XPATH解析复杂数据的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!