Xslt distinct select / Group by

匿名 (未验证) 提交于 2019-12-03 09:14:57

问题:

What i have done so fare is to go through all distict frontendGroupIds. What i would like to do now is to do a count of all the distict caseIds for each distict frontendGroupId but i cant seem to make that work. Can someone help me here plz?

回答1:

You were close:

This results in:

 2336 - 2 2337 - 3 

EDIT - Oh, I see you want the distinct count within the group. This would be:

  

Which looks (admittedly) a bit frightening. It outputs:

 2336 - 1 2337 - 2 

The core expression:

count(   key('kStatisticItemByGroup', @frontendGroupId)[     count(       . | key('kStatisticItemByGroupAndCase', concat(@frontendGroupId, ',', @caseId))[1]     ) = 1   ] ) 

boils down to:

Count the nodes from "key('kStatisticItemByGroup', @frontendGroupId)" that fulfill the following condition: They are the first in their respective "kStatisticItemByGroupAndCase" group.

Looking closely, you will find that this is no more complicated than what you already do. :-)


EDIT: One last hint. Personally, I find this a lot more expressive than the above expressions, because it emphasizes node equality a lot more than the "count(.|something) = 1" approach:

count(   key('kStatisticItemByGroup', @frontendGroupId)[     generate-id()     =     generate-id(       key('kStatisticItemByGroupAndCase', concat(@frontendGroupId, ',', @caseId))[1]     )   ] ) 

The result is the same.



回答2:

You are attempting to sort via the dreaded 'MUENCHIAN' method - something i've found so confusing that is not worth trying - so i worked out my own method.

It uses two transformations instead of one.

The first sorts the data into the right order based on your grouping requirements -- your sample data is already in the right order so i'll leave it out of this explanation( ask if you want help here)

The second transformation does the grouping simply by comparing one node to the next:

    

With this method you can go several groups deep and still have maintainable code.



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