首页 > 解决方案 > Xpath java - query with multiple conditions

问题描述

I am trying to extract from xml file names of all the people from "XYZ" group who voted AGAINST resolution with signature "A9-309". I have tried several queries by I have no idea how to combine these three conditions in one. Here is the structure of the file:

<Vote.Result Id="56" Date = "2021-3-21">
    <Vote.text>
        <a href="resolution-9-309-8">A9-309</a>recommendation 01
    </Vote.text>
    <Results.For>
        <Group Identifier="ABC">
            <Group.member id="102">George</Group.member>
            <Group.member id="12">Diana</Group.member>
            <Group.member id="27">Anna</Group.member>
            <Group.member id="32">James</Group.member>
        </Group>
        <Group Identifier="XYZ">
            <Group.member id="11">Frank</Group.member>
        </Group>
    </Results.For>
    <Results.Against>
        <Group Identifier="ABC">
                <Group.member id="2">Gregory</Group.member>
                <Group.member id="3">William</Group.member>
            </Group>
            <Group Identifier="XYZ">
                <Group.member id="7">Theresa</Group.member>
            </Group>
        </Group>
    </Results.Against>
</Vote.Result>
<Vote.Result Id = "89" Date = "2021-3-21">

.... etc.

e.g. this //Vote.Result/Vote.text/a[contains(., 'A9-309')]/text(); would give me the vote, or //Vote.Result/Results.Against/[@Identifier='XYZ']/Group.member/text(); would give me all members of XYZ group who voted AGAINST any vote.

How to combine these three? obviously, there are many <Vote.Results> in the file, so this is why there is a problem.

Thank you for any help!

标签: javaxpath

解决方案


Try:

//Vote.Result[Vote.text/a[contains(., 'A9-309')]]/Results.Against/Group[@Identifier="XYZ"]/Group.member/text()

to return one text node: Theresa. Introduce a predicate (expression in square brackets) whenever you want to filter that particular axis of the expression. Predicates can be nested as shown in the example.


推荐阅读