XPATH 多元素过滤器

发布于 2024-07-12 05:28:18 字数 734 浏览 15 评论 0原文

我有以下示例 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

白昼 2024-07-19 05:28:18

这是一个非常基本的 XPath 功能:使用逻辑运算符组合多个条件<代码>和<代码>或< /a> 和函数 not() .

的优先级高于or,并且两个运算符的优先级均低于关系相等 运算符 (=, !=>>=<<=)。

因此,这样写是安全的A = B 和 C = D

一些最常见的错误

  1. 人们这样写和/或请记住,XPath 区分大小写

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

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

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

This is a very fundamental XPath feature: composing a number of conditions with the logical operators and, or, and the function not().

and has a higher priority than or 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:

  1. People write AND and/or OR. Remember, XPath is case-sensitive.

  2. People use the | (union) operator instead of or

Lastly, here is my solution:

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

爱你是孤单的心事 2024-07-19 05:28:18
/SavingAccounts/SavingAccount[(ServiceOnLine='yes') or (ServiceViaPhone='yes')]
/SavingAccounts/SavingAccount[(ServiceOnLine='yes') or (ServiceViaPhone='yes')]
嗼ふ静 2024-07-19 05:28:18

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

成功吗?

我目前手头没有 XPath 评估器。

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

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

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

编辑:
是的,当然,对于谓词表达式来说,“或”是我的错。

Will

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

do the trick?

I have no XPath evaluator handy at the moment.

EDIT:
If I remember correctly, you don't need the text(), so

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

should be sufficient, and more readable.

EDIT:
Yes, of course, 'or' for predicate expressions, my bad.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文