How does Muenchian grouping work in details?
I have a simple XML document generated from a database:
<CLIENTS>
<CLIENT>
<NAME>John</NAME>
<ACCOUNT_NUMBER>1424763562761</ACCOUNT_NUMBER>
<LAST_USED>2012-10-03</LAST_USED>
<AMOUNT>5000</AMOUNT>
</CLIENT>
<CLIENT>
<NAME>John</NAME>
<ACCOUNT_NUMBER>543667543732</ACCOUNT_NUMBER>
<LAST_USED>2012-10-02</LAST_USED>
<AMOUNT>10000</AMOUNT>
</CLIENT>
...
I'd like to group by the name node. How can I the desired ouput to be the following?
<ClIENTS>
<CLIENT>
<NAME>John</NAME>
<ACCOUNT>
<ACCOUNT_NUMBER>1424763562761</ACCOUNT_NUMBER>
<LAST_USED>2012-10-03</LAST_USED>
<AMOUNT>5000</AMOUNT>
</ACCOUNT>
<ACCOUNT>
<ACCOUNT_NUMBER>543667543732</ACCOUNT_NUMBER>
<LAST_USED>2012-10-03</LAST_USED>
<AMOUNT>10000</AMOUNT>
</ACCOUNT>
....
</CLIENTS>
In a previous comment (under @Martin's answer), the OP asked if XSLT 2.0's
for-each-group
element could be used to solve this problem.When this XSLT 2.0 solution:
...is applied to the OP's original XML:
...the wanted result is produced:
Explanation:
As you have already correctly surmised, XSLT 2.0 introduced the
for-each-group
element (and its associated partners, such ascurrent-group()
) in order to do away with amazing/impressive, yet potentially confusing grouping methodologies like the Muenchian Method.Jeni Tennison breaks the steps required to perform Muenchian grouping here:
http://www.jenitennison.com/xslt/grouping/muenchian.html
Essentially, use XSLT to assign a key to the node, this key can be repeated if there's identical nodes in the document. The XSLT then goes through each key, and allows you to output the nodes with matching keys.
So, in Martin's answer, this line is creating a key for each CLIENT based on the content of the NAME node (remember if the NAME is the same for multiple clients, so will the key):
You then want to go through all the keys and find the first instance of each (again using Martin's answer)
You then want to find all of the CLIENTS that match the key to be able to output their detail (again, Martins)
From here you'd need another template to output the CLIENT details
Muenchian Grouping (as per @Martin's answer) eliminates the redundancy that a more traditional grouping strategy has whilst grouping.
Without Muenchian Grouping, templates usually used
preceding-sibling
orfollowing-sibling
to determine the first candidate instance of each group, and then would require a second query to lookup all nodes matching the group, as follows:Read www.jenitennison.com/xslt/grouping/muenchian.xml, for a help with the code define a key
then use templates as
[edit] If you want to use XSLT 2.0 then of course you don't need Muenchian grouping, instead you use