Grouping Data

The ability to group data from one or more data sources is a common requirement for many reports. For example, given the videos.xml file, you might want to create a list of actors that shows all the movies in which he or she has starred.

Stylus Studio facilitates grouping using a feature that allows you to create a relationship between different data sources (between books.xml and catalog.xml, for example), or between different data islands within the same source (between two nodes in videos.xml, for example).

This section describes the relationship feature in XML Publisher and how to use it to perform grouping.

What is a Relationship?

A relationship is a link between two nodes in one or more data sources that allows you to compare the values of those nodes. For example, in videos.xml, you might want compare the value of the id attribute of the actor element with the value of actorRef element, and then perform some action when those values are equal.

Comparison operations you can define for a relationship are

When you create a relationship in XML Publisher, you are defining the inner and outer loops of the for-each statements in your XSLT or XQuery code that will be used to perform the grouping (xsl:for-each in XSLT; FLWOR instructions in XQuery). The order of the nodes you select determines the order in which the outer and inner loops are created:

Creating a Relationship

Once you have added one or more data sources to the data sources panel, you can create a relationship between nodes within the same data source, or across data sources.

To create a relationship:
2. Select the node you want to use to define the outer loop in the XQuery or XSLT that will be used to create the report.

The Add Relationship button becomes active, as shown here:

The Create Relationship dialog box appears. The tree for the document appears in the Link From field. The document that appears in the Link To field depends on how many data sources you added in step 1.

By default, Stylus Studio sets the node's context using the first repeating element in the selected node's hierarchy - including the selected node itself. In this example, we selected the id attribute of the actor element, so the actor repeating element is used to set the context for this loop.

4. Optionally, change the key node and context.

In this example, we selected videos/video/actorRef repeating element.

7. Choose the comparison operator you want to use to define this relationship from the Operator drop-down list.
8. Click OK.

The relationship you just defined appears in the data sources pane of the XML Publisher Editor.

If you expand the relationship node, you see the graphic representation of the join formed between the actors/actor/id node and the videos/video/actorRef node.

You can now use this relationship as a data source in XML Publisher. See Example - Using a Relationship in a Report for more information.

Example - Using a Relationship in a Report

This example describes how to build a simple report in XML Publisher, shown here, that lists actors and the movies they have appeared in. The information for this report is based on the data in videos.xml, in the VideoCenter folder in the Stylus Studio examples project. Specifically, it matches the id attribute in the actors/actor element with the videos/video/actorRef element.

To create the example report:
1. Click File > New > XML Report to open the XML Publisher Editor.
2. Drag videos.xml from the VideoCenter folder in the Stylus Studio examples project and drop it on the data sources panel in the XML Publisher Editor.

Stylus Studio displays a tree representing the videos.xml document.

3. Expand the actors node by selecting it and pressing the * key on your number pad.
4. Select the id attribute.
5. Click the Add Relationship button.

The Create Relationship dialog box appears.

7. Since we want the loop on the video element (to locate all movies with a matching actorRef and id), change the value in the Context field to video (also a repeating element).

The data source defined by the relationship we just created between the actors and videos nodes appears in the data sources pane.

9. Drag the newly defined data source from the data sources pane and drop it on the XML Publisher canvas.

Stylus Studio creates two loops.

If you place the mouse over the outer loop, the tool tip displays the XPath - /result/actors/actor; similarly the XPath for the inner loop is /result/videos/video[$actor/@id./actorRef].

Now that the context for the loops has been defined, we next need to specify the data we want to display.

10. From the data sources panel, drag the videos/video/title element and drop it in the inner loop.
11. From the data sources panel, drag the actors/actor element and drop it in the outer loop. Select Insert Value from the pop-up menu.

When you have finished, your XML Publisher canvas should look like this:

Before formatting, the report looks like this:

All the information is there, but the report is hard to read.

13. Select the .(actor) glyph in the XML Publisher canvas and click the Bold button on the XML Publisher tool bar. Add a carriage return (press Enter) after the glyph.
14. Use the space bar to indent the title glyph. Add a carriage return (press Enter) after the glyph.
15. Preview the report again.

We now have a report that resembles the one shown in Figure 481.

Deleting a Relationship

You can delete the relationships you have defined as data sources for XML Publisher reports just as you would any other data source.

To delete a relationship:
1. Select the relationship you want to deletein the data sources pane.
2. Click the Remove Relationship button.

The relationship is removed from the XML Publisher.

 
Free Stylus Studio XML Training: