XPATH多元素过滤器

时间:2009-01-23 13:58:41

标签: xpath multiple-conditions

我有以下示例XML结构:

<SavingAccounts>
    <SavingAccount>
       <ServiceOnline>yes</ServiceOnline>
       <ServiceViaPhone>no</ServiceViaPhone>
    </SavingAccount>
    <SavingAccount>
       <ServiceOnline>no</ServiceOnline>
       <ServiceViaPhone>yes</ServiceViaPhone>
    </SavingAccount>
</SavingAccounts>

我需要做的是使用XPATH过滤'SavingAccount'节点,其中'ServiceOnline'的值为'yes'或'ServiceViaPhone'的值为yes。

XPATH应该给我两行!!我可以过滤'SavingAccount'节点,其中两个元素值都是,如下面的XPATH示例,但我想要做的是一个或元素值比较???

/SavingAccounts/SavingAccount/ServiceOnline[text()='yes']/../ServiceViaPhone[text()='yes']/..

3 个答案:

答案 0 :(得分:54)

这是一个非常基础的XPath功能:使用逻辑运算符andor和函数not()组合了许多条件。

and 的优先级高于 or ,且两个运营商的优先级均低于relational和{{3}运算符(=!=>>=&lt;&lt;=)。

因此,编写A = B and C = D

是安全的

最常犯的错误

  1. 人们撰写AND和/或OR请记住,XPath区分大小写

  2. 人们使用|(联合)运算符代替or

  3. 最后,这是我的解决方案

    <强>

    /SavingAccounts/SavingAccount
               [ServiceOnLine='yes' or ServiceViaPhone='yes']
    

答案 1 :(得分:10)

/SavingAccounts/SavingAccount[(ServiceOnLine='yes') or (ServiceViaPhone='yes')]

答案 2 :(得分:4)

威尔

/SavingAccounts/SavingAccount[ServiceOnline/text()='yes' or ServiceViaPhone/text()='yes']

诀窍?

我目前没有XPath评估器。

修改:
如果我没记错的话,你不需要text(),所以

[ServiceOnline='yes' or ServiceViaPhone='yes']

应该足够,而且更具可读性。

修改:
是的,当然,'或'对于谓词表达,我的不好。