XPath 2.0:在XPath表达式的另一部分引用先前的上下文



在XPath中,我想关注某些元素并对它们进行分析:

...
<field>aaa</field>
...
<field>bbb</field>
...
<field>aaa (1)</field>
...
<field>aaa (2)</field>
...
<field>ccc</field>
...
<field>ddd (7)</field>

我想找到文本内容(除了可能的枚举)是唯一的元素。在上面的示例中,将是bbb, ccc和ddd。

下面的XPath给出了唯一的值:

distinct-values(//field[matches(normalize-space(.), ' ([0-9])$')]/substring-before(., '(')))

现在我想扩展它,并对所有不同的值执行另一个XPath,即计算有多少字段以它们中的任何一个开头,并检索计数大于1的字段。

可以是等于该特定值的字段内容,或者以该值开头并后跟"(")。问题是,在XPath的第二部分中,我将同时引用该部分本身的上下文和前一部分的上下文。

在下面的XPath中,我将使用c_outer和c_inner来代替"。"作为上下文:

distinct-values(//field[matches(normalize-space(.), ' ([0-9])$')]/substring-before(., '(')))[count(//field[(c_inner = c_outer) or starts-with(c_inner, concat(c_outer, ' ('))]) > 1]

由于显而易见的原因,我不能同时使用"。"。但是,如何在内部表达式中从外部表达式引用特定的或当前不同的值呢?

这可能吗?

XQuery可以这样做,例如

  for $s 
  in distinct-values(
    //field[matches(normalize-space(.), ' ([0-9])$')]/substring-before(., '(')))
  where count(//field[(. = $s) or starts-with(., concat($s, ' ('))]) > 1
  return $s

最新更新