Exercise 5.7 – Conditions and Predicates
Purpose:
To practice using conditions and predicates in XSLT in the Data Mapper.
References:
Create a Route
- From the eiConsole File Management screen, select the Add Route button.
- In the dialog that appears, enter the name Conditions and Predicates and select OK.
- The Route should now appear in the table on the screen. Double-click it to open it.
Document Source System
- Select the Source System stage in the Route Editing Grid. The configuration panel will appear at the bottom.
- Change the System Name to People XML File.
- Select the Choose Source Icon button, and scroll down to select one of the XML icons.
Configure the Listener
- Select the Listener stage in the Route Editing Grid. The configuration panel will appear at the bottom.
- Select Directory/File from the Listener Type selection box. Optionally, click the … button to open the Module Search Dialog, which provides a faster way to find the module you’re looking for.
- Assign a polling directory, using the … button to open the file selector. The polling directory should NOT be the directory with all the sample files, since files will be moved/deleted by the listening process.
- Go to the Post-Process tab and select Delete from Postprocess Operation.
Add Source Format and Open Data Mapper
- Select the Source Transform stage in the Route Editing Grid. The configuration panel will appear at the bottom.
- Click the Add Format button. In the dialog that appears, name the format People to Robots 5, and click OK. The transformation configuration panel will appear below.
- On the right-hand side, in the XSLT To XML configuration panel, uncheck the Use Direct Relay button. The options in that panel will now be enabled.
- Click the New button beneath the checkbox to open the Data Mapper.
Load Source and Target Formats
- Use the Open Source Format button above the Source Format panel to open the Select Format dialog. Choose XML from the Format Reader selection box.
- Click the Add button and in the File Selector, navigate to where the the sample files are located. Choose the file called people.xml. Click OK, and then click Read Format.
- Use the Open Target Format button above the Target Format panel to re-open the dialog. From the XML Format Reader, re-open the File Selector and select the file people_by_gender.xml. Click OK, and then click Read Format.
- The Source and Target Format panels should now be populated.
Map the Root Nodes
- Select the People Node that is the root of the tree in the Source panel to the left. Drag it onto the center panel, and drop it on the stylesheet node that’s already there.
- Select the People node that is the root of the tree in the Target panel to the right. Drag it onto the center panel, and drop it on the People node that was just dropped there. This completes the implicit default Template for the XSLT document.
Use a Predicate to Build the Males Section
- Drag the Males node from the right-hand Target tree panel onto the center mapping panel, and drop it onto the People node that is there.
- In the extensions panel, type for-each into the Filter by Pattern field. Drag the for-each node that appears on top of the Target Males node in the center mapping panel.
- Drag the Person node from the left-hand Source tree panel onto the center mapping panel, and drop it on the @select node that is a child of the for-each.
- Double-click on the [@select]Person node in the center mapping panel to edit the mapping. A text field should appear on top of it.
- Add a predicate to the expression to test for the presence of Gender = Male. The Predicate should read: Person[Gender = ‘Male’]. Press Enter to save the changes.
- Drag the Name node from the right-hand Target tree panel onto the center mapping panel, and drop it on top of the for-each node.
- Drag the First Name node from the left-hand Source tree panel onto the center mapping panel, and drop it on top of the Target Name node.
Use a Condition to Build the Females Section
- Drag the Females node from the right-hand Target tree panel onto the center mapping panel, and drop it onto the People node that is there.
- In the extensions panel, drag the for-each node down to the center mapping panel again, and drop it on the Target Females node. There should now be two for-each Nodes in the center mapping panel.
- Drag the Person node from the left-hand Source tree panel onto the center mapping panel, and drop it on the @select node that is a child of the for-each. This time, though, we will NOT be adding a predicate.
- Back in the extensions panel, type If into the Filter by Pattern field and drag the If node that appears down over the center mapping panel. Drop it on top of the second for-each node.
- Under If will be a node called @test. Double-click it to open its text field. Enter Gender = ‘Female’ as the test, and press Enter to save the changes.
- Drag the Name node from the right-hand Target tree panel onto the center mapping panel, and drop it on top of the if node.
- Drag the First Name node from the left-hand Source tree panel onto the center mapping panel, and drop it on top of the Target Name node.
- Now that the mapping is complete, click the Save icon in the toolbar above the tree. When prompted for a name, use the default, transform.
- Click the X button in the top right corner to close the Data Mapper and return to the Route Editing Grid.
Configure the Transport
- Select the Transport stage in the Route Editing Grid. The configuration panel will appear at the bottom.
- Select Directory/File from the Transport Type selection box. Optionally, click the … button to open the Module Search Dialog, which provides a faster way to find the module you’re looking for.
- Select the Target Directory using the … button to open the file selector.
- Set people-by-gender-output as the Target File Name.
- Set xml as the Target File Extension.
Prepare to Test
- In the menu bar at the top, select Mode -> Testing Mode. Make sure the changes to the Route are saved.
- Copy the file called people.xml from the Samples directory provided into the directory chosen as the polling directory.
Perform the Test
- At the top of the Testing Mode screen, select the Execute Test button.
- If the Route was configured successfully, all the Stages should light up with green checkmarks.
- In the OS File Explorer, open the directory selected as the Target Directory. The file people-by-gender-output.xml should be there, with the transformed contents produced by the XSLT.
Output Files
- Compare the results from the exercise with the following example output files to see if they are correct.