XPATH Multiple Element Filters

筅森魡賤 提交于 2019-12-17 17:37:00

问题


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']/..

回答1:


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 (=, !=, >, >=, &lt; and &lt;=).

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']




回答2:


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



回答3:


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.



来源:https://stackoverflow.com/questions/472966/xpath-multiple-element-filters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!