Using the XSLT Mapper - Getting Started

This section helps you get started using the XSLT Mapper to create stylesheets that aggregate data and transform XML. The sample files used in this section are in the Stylus Studio examples\simpleMappings directory. If you follow the procedures in this section, you create the BooksToCatalog.xsl stylesheet. A sample version of this stylesheet, sampleBooksToCatalog.xsl, is also in the examples\simpleMappings directory of your Stylus Studio installation directory.

How this section is organized

Each of the topics in this section contains instructions for working with sample XML documents that you can use to familiarize yourself with the XSLT Mapper. You should perform the steps in each topic before you move on to the next topic - after the first topic, some steps depend on actions you performed in a previous topic.

This section covers the following topics:

Other sources of information

In addition to the topics described in this section, the Stylus Studio 6 User Guide contains other sources of information on XSLT:

Before you begin

To get started, you will need to start Stylus Studio if you haven't already. See Starting Stylus Studio if you need help with this step.

Opening the XSLT Mapper

This procedure describes how to open the XSLT Mapper and select the files you want to use for the drag-and-drop operations that will define your XSLT stylesheet.

To open the XSLT Mapper:
1. From the Stylus Studio menu bar, select File > New > XSLT: Mapper.

Stylus Studio displays XSLT editor with the Mapper tab selected. The source pane beneath the mapper panes appears by default, allowing you to see how the mappings of XML document elements are rendered as XSLT. The source pane is fully editable and synchronized with the XSLT Mapper. Of course, you can always click the XSLT Source tab for a full-screen view of your XSLT code.

Figure 47. XSLT Editor Mapper Tab for a New Stylesheet

Tip

 

The Project window also appears if it was open the last time Stylus Studio was closed. You can close it.

2. Click the Add Source Document button at the top of the mapper's left pane.

Stylus Studio displays the Open dialog box.

3. For this example, navigate to the examples\simpleMappings directory in the Stylus Studio installation directory.
4. Double-click books.xml.
5. Click the Set Target Document button at the top of the Mapper's right pane.

Stylus Studio displays the Open dialog box.

6. For this example, navigate to the examples\simpleMappings directory in the Stylus Studio installation directory.
7. Double-click catalog.xml.

Stylus Studio displays tree diagrams of these XML documents. The default XSLT source code has not been altered at this point.

Figure 48. XSLT Mapper Tab with Source and Target Documents

Mapping Nodes in Sample Files

This topic is part of a sequence that starts with Opening the XSLT Mapper.

To define links and examine the stylesheet Stylus Studio creates:
1. In the Mapper tab, expand the tree for both books.xml and catalog.xml.
Tip

 

You can display an entire tree using the asterisk key ( *) on your keyboard's number pad.

2. In books.xml, place the pointer over the book repeating element.
3. Press and hold the left mouse button, and drag from book to the Book repeating element in catalog.xml.

Stylus Studio draws a line as you drag.

4. Release the mouse button to create the link between book and Book.

Stylus Studio creates an xsl:for-each block that links the book and Book repeating elements. (If you mouse over the block, xsl:for-each appears in a pop-up to indicate the XSLT operation represented by the link.)

Tip

 

If you prefer, you can render xsl:for-each as a simple line. You might want to do this to simplify the appearance of the mapper canvas. Select Tools > Options from the menu, and then navigate to Module Settings > XSLT Editor > Mapper.

Figure 49. xsl:for-each Block Displayed by the XSLT Mapper

Also notice that the complete xsl:for-each instruction has been added to the XSLT source, which appears in the XSLT source pane under the XSLT Mapper canvas. The backmapping pointer identifies the line of XSLT that was just added to stylesheet.

The template contains an xsl:for-each instruction that selects the book element, which is the node you selected in step 2. The output from this template is an empty Book element, which is the node that was the target of the link. Stylus Studio created the Catalog element automatically, to provide the document structure necessary to support the Book element.

Tip

 

By default, Stylus Studio creates an xsl:value-of instruction when you link one element to another; Stylus Studio creates an xsl:for-each instruction if you link two repeating elements. You can also create other types of instructions graphically, including xsl:if, xsl:choose, and xsl:apply-template.

5. Click the Params/Other tab.

In the Output method: field, display the drop-down list and click xml. Note, however, that the output of a stylesheet generated by the XSLT Mapper is always XML - even if the setting for Output method is unspecified, Stylus Studio still generates XML.

6. Click the Mapper tab.

The xsl:output instruction is added to the XSLT source:

<xsl:output method="html"/>
               

            

7. Create another link from the title element to the Title element.
Note

 

When you map, you always map from the source document to the destination document.

8. Click the XSLT Source tab to see the new instructions in the template. (If you prefer, you can simply adjust the splitter between the XSLT source pane and the XSLT Mapper canvas.

Figure 50. Stylus Studio Builds XSLT Based on the Mapper Links

For each link you define, Stylus Studio adds instructions to the template that matches the root node. In the XSLT you have composed so far, the XSLT inserts a Book element for each book element it finds in the source document. In the Book element, the stylesheet selects the title elements. For each title element, it inserts a Title element. Finally, in each Title element, the stylesheet extracts the value of the current context node, which is the title node.

Why does the stylesheet extract the value of the title nodes but not the book nodes? The title node has only a text node as its child. In this situation, the default is that the XSLT Mapper inserts an xsl:value-of instruction.

Saving the Stylesheet and Previewing the Result

This topic is part of a sequence that starts with Opening the XSLT Mapper.

To save the stylesheet and preview the result:
1. Click Save . Stylus Studio displays the Save As dialog box.
2. In the URL: field, type BooksToCatalog.xsl.
3. Click the Save button.

This saves the stylesheet that Stylus Studio has generated. It does not matter that you have not finished mapping all nodes.

4. In the upper left corner of the XSLT Mapper, click Preview Result .

Tip

 

When you create a stylesheet using the XSLT Mapper, Stylus Studio automatically creates a scenario for you, using the source document you specify as the source document for the scenario. Scenarios and their value in the application development process are described earlier in this chapter. See XSLT Scenarios.

Stylus Studio displays the result of processing books.xml with the stylesheet you created in the XSLT Mapper in the Preview window.

Figure 51. Result of Applying XSLT to books.xml

The result document uses the same schema as the target document, catalog.xml in this example. Because not all nodes have been mapped yet, the result document does not contain all nodes found in books.xml ( author and subject nodes, for example).

5. You can confirm that the result document is incomplete by viewing books.xml. Click Open XML From Scenario , which is at the top of the Mapper tab.

Stylus Studio displays the books.xml document in the Stylus Studio XML Editor.

6. Review the XML document, and then click the document tab for the BooksToCatalog.xsl stylesheet to re-display the XSLT Editor.

Deleting Links in Sample Files

This topic is part of a sequence that starts with Opening the XSLT Mapper.

To delete links:
1. Click the Mapper tab if it is not already selected.
2. Click the title to Title link to select it.

Figure 52. Click a Link to Select It

3. Press the Delete key, or
a. Right-click the selected link. This displays a shortcut menu.
b. Click Delete to delete the selected link.

Tip

 

In addition to Delete, the shortcut menu displays the following options:

  • Go To Source displays the line of XSLT code represented by the link you select in the XML Editor.
  • Carry Value allows you to create <xsl:value-of select="."/> statements. This option is available for links representing xsl:for-each instructions only.

Defining Additional Processing in Sample Files

The stylesheet that the XSLT Mapper creates is not limited to the instructions that Stylus Studio adds. You can edit the template as you would any template. Stylus Studio automatically incorporates any changes you make to the template and displays them in the Mapper tab, if it is appropriate to do so.

In addition, you can perform external processing by, for example, defining Java functions and incorporating those functions in your XSLT stylesheet. Like standard supported XSLT functions, user-defined Java functions can be created graphically in the XSLT Mapper - just right click on the mapper canvas, select Java Functions from the shortcut menu, and select any registered Java function you want to use.

See Processing Source Nodes.

 
Free Stylus Studio XML Training: