XPATH 多元素过滤器
我有以下示例 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”节点,其中两个元素值都是 yes,如下面的 XPATH 示例,但我想要做的是 or 元素值比较???
/SavingAccounts/SavingAccount/ServiceOnline[text()='yes']/../ServiceViaPhone[text()='yes']/..
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>
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.
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']/..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个非常基本的 XPath 功能:使用逻辑运算符组合多个条件<代码>和,<代码>或< /a> 和函数
not()
.和
的优先级高于or
,并且两个运算符的优先级均低于关系 和 相等 运算符 (=
,!=
、>
、>=
、<
和<=
)。因此,这样写是安全的:
A = B 和 C = D
一些最常见的错误:
人们这样写
与
和/或或
。 请记住,XPath 区分大小写。人们使用
|
(联合)运算符而不是or
> 最后,这是我的解决方案:
This is a very fundamental XPath feature: composing a number of conditions with the logical operators
and
,or
, and the functionnot()
.and
has a higher priority thanor
and both operators have lower priority than the relational and equality operators (=
,!=
,>
,>=
,<
and<=
).So, it is safe to write:
A = B and C = D
Some most frequent mistakes made:
People write
AND
and/orOR
. Remember, XPath is case-sensitive.People use the
|
(union) operator instead ofor
Lastly, here is my solution:
会
成功吗?
我目前手头没有 XPath 评估器。
编辑:
如果我没记错的话,你不需要 text() ,所以
应该足够了,而且更具可读性。
编辑:
是的,当然,对于谓词表达式来说,“或”是我的错。
Will
do the trick?
I have no XPath evaluator handy at the moment.
EDIT:
If I remember correctly, you don't need the text(), so
should be sufficient, and more readable.
EDIT:
Yes, of course, 'or' for predicate expressions, my bad.