# Using the Group by Key Fields Option of the Constraints Tab

You can use the Group by Key Fields option of the mapper functionality to group data based on a key value, as shown in the following figure:

Figure : Merging Data Using a Key Value

In the example shown in the preceding figure, the in-warehouse-id element is the key field that is used to group the output. Both the first and third instances of the in-line-item repeating element in the source document contain the same value of the in-warehouse-id element (Warehouse1), so these elements are grouped together in the target (output) document.

This section describes how to group data by a key field using the Group by Key Fields option. This section shows how to group the example XML data shown in the preceding figure.

To Group Sets of Data Based on a Key Field

1. Create a Transformation file and a method in the Transformation file. For instructions, see Creating a Transformation File and a Transformation Method.
2. Import the XSD file that contains the XML Schema for the source and target types of the transformation. For instructions, see Selecting Source and Target Types.

For this example, import the files: GroupKeyFldIn.xsd and GroupKeyFldOut.xsd. If you installed WebLogic Platform in the c:\bea directory, import these files from the c:\bea\weblogic81\workshop\help\doc\en\integration\reffiles\transform\groupKeyFields directory.

Importing schemas files triggers a build of the current Schemas project folder. Wait until the current Schemas folder is built before proceeding to the next step. (The representations of the schemas will not be available in Available Source Types and Available Target Type panes until build is complete.)

3. Select the source type(s) for the transformation:

1. In the Available Source Types pane, expand the schema and element folders, until you find the desired element.
2. In the Available Source Types pane, select the desired element.
3. Click Add.

The elements and attributes that make up the selected element are displayed in the Selected Source Types pane.

For this example, complete this step for the GroupKeyFldIn.xsd/in-warehouse-inventory element.

4. Select the target type for the transformation:

1. In the Available Target Type pane, expand the schema and element folders, until you find the desired element.

For this example, expand the GroupKeyFldOut.xsd schema folder.

2. In the Available Source Types pane, select the desired element.

For this example, select the GroupKeyFldOut.xsd/out-inventory element.

3. Click Add.

The elements and attributes that make up the selected element are displayed in the Selected Target Type pane.

5. Click Create Transformation.

The Design View of the XQ file is displayed.

6. Create all the data links.

For this example, make the following links:

- From the Source pane, drag the $in_warehouse_inventoryDoc/in-line-item/in-warehouse-id element to the out-inventory/out-warehouse-inventory/out-warehouse-id repeating element in the Target pane.
- From the Source pane, drag the $in_warehouse_inventoryDoc/in-line-item/in-location-desc element to the out-inventory/out-warehouse-inventory/out-location-desc element in the Target pane.
- From the Source pane, drag the $in_warehouse_inventoryDoc/in-line-item/in-part-no element to the out-inventory/out-warehouse-inventory/out-line-item/out-part-no element in the Target pane.
- From the Source pane, drag the $in_warehouse_inventoryDoc/in-line-item/in-quality element to the out-inventory/out-warehouse-inventory/out-line-item/out-quantity element in the Target pane.

7. Create a link between source repeating element and the inner-most target repeating element. (See Figure: Merging Data Using a Key Value for an example of an inner-most and outer-most repeating elements.) In the Source pane, drag the source repeating element to the inner-most target repeating element in the Target pane.

For this example, link the $in_warehouse_inventoryDoc/in-line-item repeating element to the out-inventory/out-line-item repeating element.

A dashed line linking the two repeating elements is displayed. The dashed line with short dashes represents a structural link—a link between two parent structures that does not map data directly. The dashed-line representation for a structural link is shown in the following figure:

To learn more about links, see Link Representations.

8. In the Source pane, drag the source repeating element that contains the key field(s) to the outer-most target repeating element that will contain the key field(s) in the Target pane.

For this example, link the $in_warehouse_inventoryDoc/in-line-item repeating element to the out-inventory/out-warehouse-inventory repeating element.

A dashed line linking the two repeating elements is displayed. The dashed line with short dashes represents a structural link—a link between two parent structures that does not map data directly. The dashed-line representation for a structural link is shown in the following figure:

To learn more about links, see Link Representations.

At this point, in the Constraint tab, the Constraint Type is Repeatability/Join but in a preceding step, the Constraint Type will be set to Group by Key Fields)

Keep this link selected for the next step.

9. In the bottom half of the Design View for the XQ file, in the Constraints tab select the Group by Key Fields option.
10. In the Select Group Key pane, select the in-warehouse-id node and click Add.

The following is displayed in the Design View as shown in the following figure:

11. Select the Test View tab.
12. Import XML or non-XML files as source data for the transformation. For more information, see Creating Maps.

For this example, in the Source Data pane, select the $in_warehouse_inventoryDoc node and import the file: GroupKeyFldIn.xml. If you installed WebLogic Platform in the c:\bea directory, import this file from the c:\bea\weblogic81\workshop\help\doc\en\integration\reffiles\transform\groupKeyFields\XML directory.

13. In the Result Data pane, click Test.

If not currently running, the WebLogic Server for the current application will be started. In order for a query to run, the WebLogic Server for the current application must be running.

In the Result Data pane, a graphical representation of the target data is displayed. The XML result for this example is also displayed in Figure: Merging Data Using a Key Value.

14. If the resulting data is XML data, in the Result Data pane you can view the resulting data as an XML document by selecting the XML Source View tab.
15. If desired, you can validate the result data against the associated schema. In the the Result Data pane of the Test View, click Validate.
16. Save the DTF and the XQ file. From the menu bar, choose File —> Save All.