XPATH 多元素过滤器 [英] XPATH Multiple Element Filters

查看:40
本文介绍了XPATH 多元素过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下示例 XML 结构:

I have the following sample XML structure:

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

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

What I need to do is filter the 'SavingAccount' nodes using XPATH where the value of 'ServiceOnline' is 'yes' or the value of 'ServiceViaPhone' is yes.

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

The XPATH should return me two rows!! I can filter 'SavingAccount' nodes where both of the element values are yes like the following XPATH sample, but what I want to do is an or element value comparison???

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

推荐答案

这是一个非常基本的 XPath 特性:用逻辑运算符 and, or,以及函数 not().

and 的优先级高于 or 并且两个运算符的优先级都低于 关系相等 运算符(=!=>>=&lt;&lt;=).

所以,可以安全地写:A = B and C = D

一些最常见的错误:

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

人们使用 |(联合)运算符而不是 or

People use the | (union) operator instead of or

最后,这是我的解决方案:


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

这篇关于XPATH 多元素过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆