XPATH执行复杂查询

时间:2016-02-25 07:12:27

标签: android xml xpath xml-parsing

如何在给定的xml上执行以下查询:

  1. 如果(task1 = cde&& task2 = abc)我会得到id = XYZ-987
  2. if((task1 = abc&& task2 = efg)|| task5 = nop)我会得到id = ABC-123,XYZ-987,RST-567
  3. 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="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="cde" />
                <task name="task2" operation="and" value="abc" />
                <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>    
    

    我尝试了以下规则,但结果是空的

    String expression = "/node1/node2/node3/id[following-sibling::condition/task[(@name='task1' and @value='cde') and (@name='task2' and @value='abc')]]"
    

    我从上面得到了上述表达 How to parse XML using XPATH

    上述两种情况的表达方式是什么?

1 个答案:

答案 0 :(得分:1)

你的xpath不起作用的原因是你的谓词[(@name='task1' and @value='cde') and (@name='task2' and @value='abc')]在同一个任务节点上执行,如果@name = 'task1'那么@name可以永远不会= 'task2'

相反,您应该创建将在/检查所有相关节点的谓词,如下所示:

1

  

如果(task1 = cde&amp;&amp; task2 = abc)我会得到id = XYZ-987

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

2

  

如果((task1 = abc&amp;&amp; task2 = efg)|| task5 = nop)我会得到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

如果需要,可以包括括号,但在布尔运算中,and参数始终在or参数之前进行求值,因此在这种情况下它们是不必要的: /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