XPath谓词中的'OR'运算符?

abe*_*ier 24 rss xpath atom-feed

OR选择元素的XPath表达式是什么(RSS和Atom提要)<link>type="application/rss+xml" type="application/atom+xml"

  • link[@rel='alternate'][@type='application/rss+xml'] 选择RSS提要
  • link[@rel='alternate'][@type='application/atom+xml'] 选择Atom提要

但是选择它们的单个XPath表达式是什么?

谢谢.

Jon*_*and 50

使用:

link[@rel='alternate'][@type='application/rss+xml' or @type='application/atom+xml'] 
Run Code Online (Sandbox Code Playgroud)

http://www.w3.org/TR/xpath/#NT-OrExpr

您也可以使用union来完成此任务

link[@rel='alternate'][@type='application/rss+xml']|link[@rel='alternate'][@type='application/atom+xml']
Run Code Online (Sandbox Code Playgroud)

or会做.


小智 12

如果你想得到花哨并使用XPath 2.0,它更优雅(但可能令人困惑,取决于谁可能正在阅读代码)写这样:

link[@rel='alternate'][@type = ('application/rss+xml', 'application/atom+xml')] 
Run Code Online (Sandbox Code Playgroud)

这样做的原因是XPath 2.0重新定义'='以应用于序列,这意味着如果在比较左手序列中的项目与比较右手序列中的项目时存在一个匹配,则上述比较返回true.如果要与之比较的事物列表是动态的,这可能非常有用.