'OR' operator in XPath predicate?

后端 未结 2 574
半阙折子戏
半阙折子戏 2020-12-30 21:39

What is the XPath expression to select elements with type=\"application/rss+xml\" OR type=\"application/atom+xm

2条回答
  •  既然无缘
    2020-12-30 22:14

    if you want to get fancy and use XPath 2.0, it is more elegant (but potentially confusing, depending who might be reading the code) to write it like this:

    link[@rel='alternate'][@type = ('application/rss+xml', 'application/atom+xml')] 
    

    the reason for this is that XPath 2.0 redefines '=' to apply to sequences, which means that the above comparison returns true if there is one match when comparing items from the left-hand sequence with comparing items from the right-hand sequence. this can be very useful if the list of things you want to compare with is dynamic.

提交回复
热议问题