Overview of the XSLT Mapper

The XSLT mapper helps you compose XSLT that aggregates data from one or more source documents, regardless of their origin or XML. For example, an inventory application might use information from multiple vendors, each of whom organizes invoices in a different way. You can use the XSLT mapper to identify source documents, map the relevant nodes from each to a target document, and in doing that define any required XSLT instructions, XPath or Java functions, and logical operators graphically.

To use the XSLT mapper to create an XSLT stylesheet, you start by specifying one or more source documents and one target document.

Figure 214. Example of XSLT Mapper

The Mapper tab consists of these areas:

  • Source document pane, in which you add one or more source documents.
  • Target structure pane, in which you specify the structure of the result you want the XSLT to return.
  • Mapper canvas, on which you can define conditions, functions, and operations for source document nodes to filter return values that are then mapped to the target node.
  • Source code pane (not shown in Figure 214). The source code pane allows you to view the source code while using the mapper. This is a great way to see how changes to the mapper affect the source, without the need to switch to the XSLT Source tab. Of course, the XSLT Source tab is available if you prefer working with the source using a full-page view. All views - Mapper tab, XSLT Source tab, and the source pane - are synchronized. When displayed, the source pane spans the width of the XSLT editor.

As you link elements and define XSLT instruction and function blocks in the mapper, Stylus Studio composes XSLT for you, which is visible (and editable) any time you click the XSLT editor's XSLT Source tab. When you have finished mapping, you can apply the stylesheet to XML documents that have the same schema as the source document. The result document also has the same schema as the destination document.

As with the XSLT Source tab, you can preview XSLT results from the Mapper tab by clicking the Preview Result button ( ). Debugging, however, can be performed from the XSLT Source tab only.

This section covers the following topics:

Example

Suppose you open the XML mapper and select books.xml as the source document and catalog.xml as the target document. You then map elements in the books.xml document or structure to elements in the catalog.xml document or structure. The result is a stylesheet that you can apply to books.xml and to other files that have a structure similar to that of books.xml. When you apply this stylesheet, the result is an XML document whose structure is consistent with that of catalog.xml.

Now suppose you want to apply a stylesheet to catalog.xml and output an XML file that has a structure similar to books.xml. To do this, you must use the XSLT mapper to create a second stylesheet. This time, catalog.xml is the source document and books.xml is the destination document. The result of this mapping is a stylesheet that you can apply to documents that have a structure similar to that of catalog.xml.

Graphical Support for Common XSLT Instructions and Expressions

The XSLT mapper has graphical support for

  • XSLT instructions
  • XPath functions
  • Logical operators
  • Java Functions

Using special symbols, called blocks, you can quickly and easily create complex XSLT without writing any code, as shown in Figure 215:

Figure 215. XSLT Operation, Function, and Logical Operator Blocks

Blocks can be created

  • Automatically, when you link one node to another. For example, if you link repeating elements in the source and target documents, Stylus Studio automatically creates an xsl:for-each instruction block in the mapper.
  • Manually, by selecting the instruction or expression you want to create from the shortcut menu on the mapper canvas (right click on the mapper canvas to display this menu).
  • By reverse-engineering the XSLT that you write on the XSLT Source tab - when you click the Mapper tab, XSLT that can be represented graphically is displayed on the mapper canvas.

See Working with XSLT Instructions in XSLT Mapper and Processing Source Nodes to learn more about working with blocks in the XSLT mapper.

Setting Options for the XSLT Mapper

There are a few options you can set that affect the XSLT stylesheets generated by the XSLT mapper. To display the Options dialog box, in the Stylus Studio tool bar, select Tools > Options.

Under Module Settings > XSLT Editor, click Mapper. The mapper has an option that determines whether Stylus Studio creates empty elements for unlinked nodes when the associated schema specifies that the elements are required. You might want to select this option to help ensure that your XSLT generates valid XML by ensuring that all required elements are accounted for.

Among other options under the XSLT Editor heading, consider clicking XSLT Settings and specifying whether or not you want Stylus Studio to save scenario metainformation in the stylesheet. Scenario metainformation includes anything specified in the Scenario Properties dialog box - source and output URLs, parameter values, post-processing options, and so on.

Note

 

If you select this option, Stylus Studio also saves mapper metainformation in the stylesheet; mapper metainformation includes the names of source files, node mapping information, and so on.

If you choose not to save scenario metainformation in the stylesheet, and if the stylesheet belongs to a project, Stylus Studio saves mapper metainformation in the project. If the stylesheet does not belong to a project, and you choose not to save metainformation in the stylesheet, mapping metainformation is not saved.

Ensuring That Stylesheets Output Valid XML

Stylus Studio cannot automatically generate a stylesheet that will always generate a valid XML document. As defined by the W3C, an XML document is considered to be valid if it conforms to the DTD with which it is associated.

For example, consider a stylesheet that has required attributes. In order to specify meaningful values for them, you need to have insight as to the semantics of the operation the stylesheet is performing, and it is difficult for any application to infer this type of information. Always check to see that the stylesheets you create using the XSLT mapper generate valid input.

Steps for Mapping XML to XML

To create an XSLT stylesheet using the XSLT mapper:
1. From the Stylus Studio menu bar, select File > New > XSLT: Mapper.

Stylus Studio displays the XSLT editor with the Mapper tab selected.

2. Select one or more source documents and a target document.
3. In both the source and the target panes, click the root element and then press the asterisk key ( *) in the numeric key pad to expand the schema tree.
4. Map nodes in the source documents to nodes in the target document, define XSLT instructions and functions, and create named and matched templates using the mapper's graphical tools.
Tip

 

Consider working through the source document in document order as you map source and target elements.

5. Check the XSLT Source view from time to time. This allows you to confirm that the stylesheet is doing what you expect it to do (and it is also a good way to teach yourself XSLT). Changes you make directly to the source are reflected on the Mapper tab, and vice versa.

Each of these steps is described in greater detail in the following sections.

 
Free Stylus Studio XML Training: