是否有一个DRYer XPath表达式用于联合?

Chr*_*Noe 5 union xpath expression dry

这很适合查找类似按钮的HTML元素,(故意简化):

  //button[text()='Buy']
| //input[@type='submit' and @value='Buy']
| //a/img[@title='Buy']
Run Code Online (Sandbox Code Playgroud)

现在我需要将其限制在一个上下文中.例如,出现在标签框内的"购买"按钮:

//legend[text()='Flubber']
Run Code Online (Sandbox Code Playgroud)

这工作,(..让我们到包含的字段集):

  //legend[text()='Flubber']/..//button[text()='Buy']
| //legend[text()='Flubber']/..//input[@type='submit' and @value='Buy']
| //legend[text()='Flubber']/..//a/img[@title='Buy']
Run Code Online (Sandbox Code Playgroud)

但有没有办法简化这个?可悲的是,这种事情不起作用:

//legend[text()='Flubber']/..//(
  button[text()='Buy']
| input[@type='submit' and @value='Buy']
| a/img[@title='Buy'])
Run Code Online (Sandbox Code Playgroud)

(请注意,这是针对浏览器中的XPath,因此XSLT解决方案无济于事.)

小智 2

来自评论:

稍微调整以获得 A 而不是 IMG: self::a[img[@title='Buy']]。(现在如果可以减少“购买”

使用以下 XPath 1.0 表达式:

//legend[text() = 'Flubber']/..
   //*[
      self::button/text()
    | self::input[@type = 'submit']/@value
    | self::a/img/@title
    = 'Buy'
   ]
Run Code Online (Sandbox Code Playgroud)

编辑:我没有看到父访问器。仅在一个方向上的另一种方式:

//*[legend[text() = 'Flubber']]
   //*[
      self::button/text()
    | self::input[@type = 'submit']/@value
    | self::a/img/@title
    = 'Buy'
   ]
Run Code Online (Sandbox Code Playgroud)