Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
XPath2.0:在XPath表达式的另一部分中引用前面的上下文_Xpath_Count_Distinct Values - Fatal编程技术网

XPath2.0:在XPath表达式的另一部分中引用前面的上下文

XPath2.0:在XPath表达式的另一部分中引用前面的上下文,xpath,count,distinct-values,Xpath,Count,Distinct Values,在XPath中,我想重点介绍某些元素并分析它们: ... <field>aaa</field> ... <field>bbb</field> ... <field>aaa (1)</field> ... <field>aaa (2)</field> ... <field>ccc</field> ... <field>ddd (7)</field> 现在

在XPath中,我想重点介绍某些元素并分析它们:

...
<field>aaa</field>
...
<field>bbb</field>
...
<field>aaa (1)</field>
...
<field>aaa (2)</field>
...
<field>ccc</field>
...
<field>ddd (7)</field>
现在我想扩展这个范围,并对所有不同的值执行另一个XPath,这将是计算有多少字段以它们中的任何一个开头,并检索计数大于1的字段

这可能是一个等于该特定值的字段内容,或者它以该值开头,后面跟着“(”。问题是,在XPath的第二部分中,我必须同时引用该部分本身的上下文和前一部分的上下文

在以下XPath中,我将使用c_外部和c_内部,而不是使用“.”作为上下文:

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

我并没有看到过这样的尝试,但我相信您可以使用XPath 2.0和两个连续的for循环来完成,就像这样:对于不同值中的$s(//field[匹配(规范化空间(.),“\([0-9]\)$”)]/子字符串在(,,,,,,,,,,,,,,,,//字段中的$y[(,=$s)或以(,,concat(,,,,,,,,,,,,,,,,,,,,,,,,,,)开头]返回$s[计数($y)>1]
  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