The grouping functionality is available in XSLT 2.0 and 3.0 SPSs and for HTML, RTF, PDF, and Word 2007+ output. Grouping is not supported for Authentic View output.
Grouping enables items (typically nodes) to be processed in groups. For example, consider an inventory of cars, in which the details of each car is held under a car element. If, for example, the car element has a brand attribute, then cars can be grouped by brand. This can be useful for a variety of reasons. For example:
•All cars of a single brand can be presented together in the output, under the heading of its brand name.
•Operations can be carried out within a group and the results of that operation presented separately for each group. For example, the number of models available for each brand can be listed.
Additionally, a group can be further processed in sub-groups. For example, within each brand, cars can be grouped by model and then by year.
Items can be grouped using two general criteria: (i) a grouping key, which typically tests the value of a node, and (ii) the relative position of items. The following specific grouping criteria are available:
•group-by, which groups items on the basis of an XPath-defined key. For example, car elements can be grouped on the basis of their brand attributes. The grouping is set on the car element, and an XPath expression selects the brand attribute.
•group-adjacent uses a combination of grouping-key and position criteria. All adjacent items that have the same value for the grouping key are included in one group. If the grouping-key value of an item is different from that of the previous item, then this item starts a new group.
•group-starting-with starts a new group when a node matches a defined XPath pattern. If a node does not match the defined XPath pattern, then it is assigned to the current group.
•group-ending-with ends a group when a node matches a defined XPath pattern; the matching node is the last in that group. The next node starts a new group. If a node subsequent to that which starts a group does not match the defined XPath pattern it is assigned to the current group.
Groups can be created on either a node or a current-group template via the context menu. To create a group, right-click the node or current-group template, and in the context menu that appears, select the Group by command. This pops up the Define Output Grouping dialog (screenshot below).
In the dialog, check the Enable Grouping check box, then select the required Grouping Type and, in the Match text box, enter the XPath expression that defines the grouping key (for the group-by and group-adjacent options) or the desired match pattern (for the group-starting-with and group-ending-with options). When you click OK, a dialog pops up asking whether you wish to sort the group-set alphabetically (in ascending order). You can always sort group-sets subsequently or remove such sorting subsequently. The screenshot below shows nodes and current-group templates which have had grouping added to them.
In the screenshot above, the person node has been grouped and the resulting groups sorted. For example if the person elements have been grouped by department, then the various departments can be sorted in alphabetically ascending order. The groups thus created have been further grouped by creating grouping on the current-group() template. In this way person elements can be grouped, say, first by department, and then by employment grade.
After confirming a grouping definition, a pop-up asks you to confirm whether the groups should be sorted in ascending order or not. You can set sorting subsequently at any time, or modify or delete, at any time, the sorting set at this stage.
To set, modify, or delete sorting subsequently, right-click the required grouping template and select Sort by. This pops up the Define Output Sort Order dialog. How to use this dialog is described in the section Sorting. The important point to note is that to sort groups on the basis of their grouping-key, you must select the XPath function current-grouping-key() as the sorting key. For examples, see the files described in the following sections.
Viewing and editing grouping and sorting settings
To view and edit the grouping and sorting settings on a template, right-click the template and select Group by or Sort by, respectively. This pops up the respective dialog, in which the settings can be viewed or modified.
User-defined templates are templates that are applied to items selected by an XPath expression you specify. The nodes selected by the XPath expression of a user-defined template can also be grouped. In this case, the grouping is applied on the user-defined template.