Home > Online Product Documentation > Table of Contents > 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:
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.
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. |
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.
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:
When Stylus Studio creates a new stylesheet, the stylesheet includes the following built-in templates:
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.
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.
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.
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:
xsl:apply-templates instruction that selects the new template's element for processing.
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.
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.
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.
When you want to update a template, you can use all features that are available when you are updating a stylesheet. See Updating Stylesheets.