A global template can be defined for any node or type in the schema, or for a node specified in an XPath pattern.
A global template specifies instructions for the selected node or type, and it is invoked by a call from the main template, design fragments, or other global templates. The processing model is similar to that of declarative programming languages, in that a single template is defined and invoked multiple times. In this way a single definition can be re-used multiple times. Global templates are invoked in two situations:
•When a node or type in the main template has been set to reference its global template (done by right-clicking the component in the design and selecting Make Global Template).
•When a (contents) or (rest-of-contents) is inserted within an element or type in a local template, and the rest of the content of that element or type includes a node or type for which a global template exists.
Global templates are useful if a node (or type) occurs within various elements or in various locations, and a single set of instructions is required for all occurrences. For example, assume that a para element must be formatted the same no matter whether it occurs in a chapter, section, appendix, or blockquote element. An effective approach would be to define a global template for para and then ensure, that in the main template the global template for the para element is processed wherever required (for example, by including //chapter/para in the main template and specifying that para reference its global template; or by including //chapter/title and then including (contents) or (rest-of-contents) so that the rest of the content of the chapter element is processed with the available global templates and default templates). Also, a global template can be defined for a complex type (for example, one that defines an address model) or even for a simple type (for example, xs:decimal). In such cases, all occurrences of the type (complex or simple) that invoke the global template for that type will be processed according to the rules in the global template.
Global templates can be created for any node or type in the schema, or for a node specified in an XPath pattern., and are created from the Schema Tree sidebar (screenshot below).
A global template can be created in any of the following ways:
•Click the Add New Global Template button located at the right of the Global Templates item in the Schema Tree (see screenshot above). This pops up the Add New Global Template dialog (screenshot below). You can select an element, an attribute, or a type from the schema tree shown in the dialog, or you can enter an XPath pattern. (Note that global templates created for nodes selected with an XPath pattern are not supported in Authentic.) This selects the node that must be created as the global template. Click OK to finish. The template will be created and appended to the already existing templates in Design View and can then be edited. In the Schema Tree, the schema node or type will be marked with a plus sign icon in front of it.
•Right-click the schema node or type component in the Schema Tree (under Root Elements, All Global Elements, or All Global Types, as appropriate), and select the command Make/Remove Global Template. The template will be created and appended to the already existing templates in Design View and can then be edited. In the Schema Tree, the schema node or type will be marked with a plus sign icon in front of it.
•Global templates can also be created from templates in the main template in Design View. Right-click the template (either in Design View or the Schema Tree sidebar) and select the command Make Global Template. A global template is created from the selected template (it is appended to the templates in Design View) and the template in the main template is automatically defined to use this global template (see below for an explanation of how global templates are used).
A global template is located in Design View below the main template. It is indicated by a mauve bar containing the name of the node for which the global template has been created, followed by its type: (simple) or (complex). A global template is shown in the screenshot below.
Note that the processing of the global template is user-defined and could include both static and dynamic components, as well as the whole range of processing options available for processing of the main template.
After a global template has been created, it can be used when a node having the same qualified name is inserted into the document (When the node is dropped in the design, select the command Use Global Template from the menu that pops up.) by dropping . Alternatively, if a local template is present in the design and a global template exists for a node having the same qualified name, then the global template can be used instead of the local template. To use a global template for a local template, right-click the local template in Design View and select the command Use Global Template. When a global template is used, its processing instructions are called and used by the local template at runtime.
Wherever a global template is used in the design, an XPath pattern can be created on the global template to filter the nodeset it addresses. To create such a filter, right-click the global template tag in the design, and select Edit XPath Filter in the context menu that appears. This pops up the Edit XPath Expression dialog, in which the required expression can be entered.
Global templates can be recursive, that is, a global template can call itself. However, to guard against an endless loop in Authentic View, a property to limit the call-depth can be set. This property, the Maximum Template-Call-Depth property, is available in the Authentic tab of the Properties dialog of the SPS (File | Properties). It specifies the maximum number of template calls that may be made recursively when processing for the Authentic View output. If the number of template calls exceeds the number specified in the Maximum Template-Call-Depth property, an error is returned.
After a global template has been created, its processing instructions can be copied directly to a template of the same qualified name in the main template. To do this, right-click the local template and select the command Copy Global Template Locally. Copying the global template locally is different than using the global template (at runtime) in that the processing instructions are merely copied in a one-time action. The global template has no further influence on the local template. Either, or both, the global template and local template can subsequently be modified independently of each other, without affecting the other. On the other hand, if it is specified that a global template should be used (at runtime) by a local template, then any modifications to the global template will be reflected in the local template at runtime.
A global template can be activated by checking its entry in the global templates listing in the Schema Tree sidebar. It can be deactivated by unchecking the entry. If a global template has been activated (the default setting when the global template was created ), it is generated in the XSLT stylesheet. If it has been deactivated, it is not generated in the XSLT stylesheet but is still saved in the SPS design.
Any local template that uses a deactivated global template will then—since it is not able to reference the missing global template—fall back on the default templates of XSLT, which have the collective effect of outputting the contents of descendant text nodes.
The advantages of the activation/deactivation feature are: (i) Global templates do not have to be deleted if they are temporarily not required; they can be reactivated later when they are required; (ii) If there are name conflicts with templates from imported stylesheets, then the global template that is not required can be temporarily deactivated.
To remove a global template, right-click the global template to be removed, either in Design View or the Schema Tree sidebar, and select the command Make/Remove Global Template.
Global templates are of two types: simple and complex. Complex global templates are available for reasons of backward-compatibility. If a global template in an SPS created with a version of StyleVision prior to version 2006 contains a table or list, then that global template will typically be opened in StyleVision 2006 and later versions as a complex global template.
A complex global template is different than a simple global template in the way the node for which the global template was created is processed. When the first instance of the node is encountered in the document, the complex global template processes all subsequent instances of that node immediately afterwards. A simple global template, on the other hand, processes each node instance only when that node instance is individually encountered.
It is important to note that a simple global template will be automatically converted to a complex global template if a predefined format or newline is created around the element node for which the global template was created. This will result in the processing behaviour for complex global templates (described in the previous list item). To revert to the simple global template, the predefined format should be removed (by dragging the node outside the predefined format and then deleting the predefined format), or the newline should be removed (by deleting the item in the Design Tree sidebar), as the case may be. To avoid the automatic conversion from simple global template to complex global template, make sure that the predefined format or newline is added within the node tags of the element for which the simple global template was created.
When an SPS module is added to another SPS module, the global templates in the added module are available for use within the referring SPS. For more information about using modular SPSs, see the section Modular SPSs.