Working with Templates

Templates define the actions that you want the XSLT processor to perform. When you apply a stylesheet to an XML source document, the XSLT processor populates the result document by instantiating a sequence of templates. This is illustrated in Understanding How the Default Templates Work.

A template can contain elements that specify literal result nodes. It can also contain elements that are XSLT instructions for creating result nodes. In a template, the template rule is the pattern that the XSLT processor matches against (compares with) selected nodes in the source document.

This section covers the following topics:

Viewing Templates

Stylus Studio provides different ways to display lists of templates, specific templates, as well as ways to see if a given template generates any output.

Viewing a List of Templates

To view a list of the templates in the stylesheet
1. Click the down arrow in the upper right corner of the XSLT editing pane.

Figure 210. Choosing Available XSLT Templates

Stylus Studio displays a drop-down list of the first five match patterns in the stylesheet. To limit the displayed list, type in the combo box to the left of the down arrow. Stylus Studio displays only those patterns that match the character(s) you typed.

Note

 

A particular template might or might not have a match pattern (template rule). Named templates do not necessarily specify match patterns.

2. Click the match pattern for the template you want to view. It does not matter whether the XSLT editor is in Full Source mode or Template mode.

Viewing a Specific Template

To view a particular template, double-click its matching element in the XML tree view, which is displayed to the right of the editing pane. If the element has more than one template, Stylus Studio displays a list of the templates. Click the one you want.

Checking if a Template Generates Output

To see if a particular template generates any output:
1. Select a template in the XSLT templates pane.
2. Click Refresh to apply the stylesheet.
3. In the XSLT Preview window, with output text displayed, scroll as necessary to find text highlighted in gray. Text with a gray background was generated by the selected template.

Using Stylus Studio Default Templates

Every stylesheet in Stylus Studio can use two built-in templates, even though they are not explicitly defined. This section covers the following topics to help you use these templates:

Contents of a New Stylesheet Created by Stylus Studio

When Stylus Studio creates a new stylesheet, the stylesheet includes the following built-in templates:

<xsl:template match="*|/">
               

              
<xsl:apply-templates/>
                   

                
</xsl:template> <xsl:template match="text()|@*">
<xsl:value-of select="."/>
                   

                
</xsl:template>

Every XSLT stylesheet contains these templates whether or not they are explicitly specified. In other words, the XSLT processor behaves as if they are there even when they are not explicitly specified in the stylesheet.

About the Root/Element Built-In Template

The first built-in template matches *|/. This means it matches every element in the source document and it matches the root node. This is the root/element built-in template.

This root/element built-in template contains only the xsl:apply-templates instruction. The xsl:apply-templates instruction does not specify a select attribute, which means that the XSLT processor operates on the children of the node for which the root/element template was instantiated.

What does the XSLT processor do when it operates on these children nodes? It searches for a template that matches each node. If there is no such template and if the node is an element, the XSLT processor instantiates the root/element built-in template. If the node is a text node and there is no matching template, the XSLT processor instantiates the text/attribute built-in template.

If the node for which the root/element built-in template is instantiated has no children, the XSLT processor does no processing for this node and proceeds to the next selected node.

The XSLT processor instantiates the root/element built-in template when it cannot find a template that explicitly matches the root node or an element in the source document. As you know, the XSLT processor always begins processing by instantiating the template that matches the root node. If you do not define such a template in your stylesheet, the XSLT processor begins processing by instantiating the root/element built-in template.

About the Text/Attribute Built-In Template

The second specified built-in template matches text()|@*. This means it matches the text contents of every text node and every attribute in the source document. This is the text/attribute template.

This template contains only the xsl:value-of instruction. Its select attribute specifies an expression for selecting an XML node. The "." expression identifies the current node, which is the node the template was instantiated for.

This template copies the text contained in the current text node or attribute to the result document.

Creating Templates

To do anything beyond copying the text from your XML document to the result document, you must create templates. You can create new templates several ways:

  • In the source tree of the XSLT editor, double-click the element or attribute for which you want to create a template. Stylus Studio creates an empty template that matches the node you clicked. This template appears at the end of the stylesheet.
  • Click New Template . Stylus Studio creates an empty template whose match pattern is NewTemplate. Replace NewTemplate with a match pattern that has meaning for your stylesheet. The new template appears at the end of the stylesheet.
  • Drag an element or node from the source tree in the XSLT editor to the Full Source pane and drop it in a location that is not in a template. Stylus Studio creates a new template that matches the node you dragged in.
Try creating a new template that matches an XML element in your document:
1. Double-click an element in the tree view of your XML document.
2. Enter the following instruction in the new template:
<b><xsl:value-of select="."/></b>
       

    
3. In another template, ensure that there is an xsl:apply-templates instruction that selects the new template's element for processing.
4. Press F5 to apply the stylesheet and refresh the current scenario in the Preview window.

Notice that the text contents of the element for which you created the template are now displayed in bold - the XSL instruction is formatted with <b> and </b>. Also, the XSLT processor does not process this element's children (if there are any) because the new template you created does not specify <xsl:apply-templates/>.

By creating additional templates to style portions of your XML document, you can completely control how the document appears.

Saving a Template

To save a template, save the stylesheet. Click Save in the Stylus Studio tool bar, or select File > Save from the Stylus Studio menu bar.

Applying Templates

The xsl:apply-templates instruction allows you to control the order of operations when you apply a stylesheet. For an in-depth description of how the XSLT processor applies templates, see How the XSLT Processor Applies a Stylesheet.

To apply a template so that you can see the output in the Preview window, you must apply the entire stylesheet. Press F5 to apply the stylesheet and refresh the output. If Stylus Studio detects any errors in the stylesheet or in the XML source document, it displays a message that indicates the cause and location of the error.

In the Preview window, in the Text view, the text with a gray background was generated by the template the cursor is in. If the editor is in Template mode, the text with the gray background was generated by the currently visible template.

Updating Templates

When you want to update a template, you can use all features that are available when you are updating a stylesheet. See Updating Stylesheets.

Deleting Templates

To delete a template:
1. Select the text for the template you want to delete.
2. Right-click in the editor to display the shortcut menu.
3. Click Cut.
 
Free Stylus Studio XML Training: