检查相同的文本是否出现在2个不同的xpath下

时间:2017-01-04 08:15:40

标签: java xml xpath

我有2个xpaths - //xpath1/text()//xpath2/text()我想形成一个单独的xpath条件表达式,它将验证两个文本是否相同。

这可能吗?我可以调整xpath。

修改

..
    <test id="1">
        <testcase attr1="xyz">Jane</partyId>
        <testcase attr2="abc">Doe</partyId>
    </test>

    <test id="2">
        <testcase attr1="xyz">Jane</partyId>
        <testcase attr2="abc">Does</partyId>
    </test>
..

我想验证//test[@id="1"]/testcase[@attr1="xyz"]//test[@id="2"]/testcase[@attr1="xyz"]下的文字是否相同。

1 个答案:

答案 0 :(得分:1)

<root xmlns:foo="http://www.foo.org/" xmlns:bar="http://www.bar.org">
    <actors>
        <actor id="1">Christian Bale</actor>
        <actor id="2">Liam Neeson</actor>
        <actor id="3">Michael Caine</actor>
    </actors>
    <foo:singers>
        <foo:singer id="4">Tom Waits</foo:singer>
        <foo:singer id="5">B.B. King</foo:singer>
        <foo:singer id="6">Ray Charles</foo:singer>
    </foo:singers>
</root>

XPATH:

//actors[string(.)=string(//foo:singers)]

OUT:

NO MATCH!

<强> ATTATION:

string()函数通过返回节点集中第一个节点的字符串值将节点集转换为字符串,这在某些情况下可能会产生意外结果。

如果希望string()函数连接所有子文本,则必须传递单个节点而不是节点集。

string Function