XPATH解析复杂数据的问题

时间:2016-03-04 11:33:22

标签: android xml parsing xpath

如何获取条件名称为" 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

来自How to parse and fetch exact result from XML using XPATH

的查询

Catch 如果任务中的所有操作(满足任务除外)都是" OR"那么该ID也应被视为满意条件。在下面的XML中,ABC-123,LMN-543和XYZ-987满足条件,但ABC-123具有所有其他操作=" OR"。所以这也应该包含在结果中。通过使用计数,我限制为3个任务,因此不包括ABC-123。使用count> 3将获取ABC-123,LMN-543和XYZ-9876。

需要输出为ABC-123; XYZ-987 使用条件检查操作=或

以下是使用的XML

<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>

2 个答案:

答案 0 :(得分:0)

你必须重新构建你的条件:

/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

在这种情况下,圆括号()是可选的,但它们提高了可读性。 请注意,根据您提供的示例数据,LMN-543也满足查询。

返回

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

我对你的'捕捉'条件不太清楚,但根据你所说的,它可以在每个节点中翻译,最多有2个操作=“和”任务。 这使得和其他

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> 

答案 1 :(得分:0)

/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

将返回所有ID:

  • 具有@operation属性且未与条件@name='task1' and @value='abc'@name='task2' and @value='efg'
  • 匹配的任务数量
  • 等于@operation = 'or'
  • 的任务数

ABC-123XYZ-987