我有以下示例 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 应该返回两行!!我可以过滤两个元素值都是 yes 的“SavingAccount”节点,如下面的 XPATH 示例,但我想要做的是 or 元素值比较???
/SavingAccounts/SavingAccount/ServiceOnline[text()='yes']/../ServiceViaPhone[text()='yes']/..
最佳答案
这是一个非常基本的 XPath 功能:使用逻辑运算符 and
组合多个条件。 , or
,以及函数not()
.
and
的优先级高于 or
,并且两个运算符的优先级均低于 relational和 equality运算符(=
、!=
、>
、>=
、<
和<=
)。
因此,可以安全地编写:A = B 且 C = D
一些最常见的错误:
人们会写
AND
和/或OR
。 请记住,XPath 区分大小写。人们使用
|
(联合)运算符而不是或
最后,这是我的解决方案:
/SavingAccounts/SavingAccount [ServiceOnLine='yes' or ServiceViaPhone='yes']
关于XPATH 多元素过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/472966/