XPath 1.0 COUNT - Unique values

纵然是瞬间 提交于 2019-12-25 03:54:20

问题


I've got this test XML:

<test>
    <x a="1">
      <x a="2">
        <x>
          <y>y31</y>
          <y>y32</y>
        </x>
      </x>
    </x>
    <x a="1">
      <x a="2">
        <y>y31</y>
        <y>y32</y>
      </x>
    </x>
    <x a="1">
      <y>y11</y>
      <y>y12</y>
    </x>
    <x>
      <y>y11</y>
      <y>y11</y>
    </x>
</test>

How can I query:

1 - All values for y

y32, y32, y11, y12

2 - Number of values for y

I'm trying to use COUNT but I can't count the number of UNIQUE values y has, for example if I run query against sample XML described before I need the result to be 4.


回答1:


If all you've got to work with is XPath (and not XSLT or something of that nature), then I think this is the best you can do:

All distinct y values:

//y[not(. = preceding::y)]

Number of distinct y values:

count(//y[not(. = preceding::y)])


来源:https://stackoverflow.com/questions/25549298/xpath-1-0-count-unique-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!