|
Home >Online Product Documentation >Table of Contents >Grouping Data 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
l Equal
l Not equal
l Less than
l Greater than
l Less than or equal to
l Greater than or equal to
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:
l The first node you select is used to define the outer loop
l The second node you select is used to define the inner loop
Creating a RelationshipOnce 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:
1. Add the data source(s) you require for your report. See Adding a Data Source if you need help with this step.
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:
3. Click the Add Relationship button.
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.
5. In the Link To field, select the node you want to use to define the inner loop in the XQuery or XSLT that will be used to create the report. If you defined more than one data source in Step 1, you can change the data source in the Data Source drop-down list.
In this example, we selected videos/video/actorRef repeating element.
6. Optionally, change the context of the node you selected in Step 5.
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 ReportThis 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.
6. In the Link To field, expand the videos node and select the actorRef repeating element.
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).
8. Click OK.
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:
12. Click the Preview button (
), and save the file when prompted.
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 519.
Deleting a RelationshipYou 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.
|