如何使用XPATH从XML解析和获取精确结果

时间:2016-03-03 11:11:52

标签: android xml xpath

使用以下XML

(i:Int)

我希望得到ID = XYZ-987条件名=" task1",value =" abc" AND name =" task2",value =" efg" AND name =" task5",value =" nop"

我得到2个结果(ABC-123和XYZ-987)而不是1个(XYZ-987)。 使用以下XPATH表达式

<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="and" value="hij" />
            <task name="task4" operation="or" value="klm" />
            <task name="task5" operation="and" 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="and" 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个结果,并按照上述3个条件获取条目?

参考XPATH executing complex queries来发展表达。

1 个答案:

答案 0 :(得分:1)

通过在表达式中附加“and count(condition / task)= 3”得到答案。

/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