In order to deviate from the default behavior of XSLT processing you may add template rules to your XSLT stylesheet. A template rule is represented by an xsl:template element. This element has two main components:
The rule pattern matches all nodes resulting from the evaluation of the XPath pattern with initial context set equal to the set of all nodes. For instance, the pattern article[@key = "V09"]//author[last()] matches all last authors that are descendant elements of an article with the key attribute value equal to V09. The XSLT processors reads the XML document from the top to the bottom and, for each node matched by the rule pattern, it instantiates and copies the rule template into the output document.
If more than one rule pattern applies to a node, the one with highest priority is chosen. The priority can be specified using the priority attribute of the xsl:template element, whose value is a number. It is an error if more than one rule pattern match a node and have the same priority; however, in this case, most XSLT processors choose the last template rule occurring in the stylesheet, rather than signaling the error.
For instance, consider the following stylesheet:
<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="dblp">This is a bibliography</xsl:template> </xsl:stylesheet>
The only rule says to replace any dblp element with the string This is a bibliography. It also prevents the descendant nodes of each dblp element from being processed. Hence, the user-defined rule overrides the built-it default one that, instead, would process the dblp element and print its string values. The built-in rule for comments is however applied when the processor visits the first comment of the document (the one outside the dblp element). Since in our bibliography XML document the root element is labelled dblp, the effect of the stylesheet is to produce the following output:
<?xml version="1.0" encoding="UTF-8?>This is a bibliography
The template can also contain markup. However, the markup must be well-formed because the stylesheet itself must be well-formed, as in the following example:
<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="inproceedings"> <conference>This is a conference paper</conference> </xsl:template> <xsl:template match="article"> <journal>This is a journal paper</journal> </xsl:template> </xsl:stylesheet>
In this case, the built-in rule for comments is applied at the first comment and the one for elements is applied at the dblp element. Finally, the user-defined rules for inproceedings and article elements are invoked. The effect is as follows:
<?xml version="1.0" encoding="UTF-8"?> <conference>This is a conference paper</conference> <journal>This is a journal paper</journal>
To show yet again the effect of the default behavior and the possibility of overriding it, consider the following example:
<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="inproceedings"> <conference>This is a conference paper</conference> </xsl:template> <xsl:template match="comment()"> <comment>There was a comment here!</comment> </xsl:template> </xsl:stylesheet>
The default rules are overridden for inproceedings elements and for comment nodes, but they can be invoked for the other nodes, in particular for the article element. The output is as follows:
<?xml version="1.0" encoding="UTF-8"?><comment>There was a comment here!</comment> <conference>This is a conference paper</conference> <comment>There was a comment here!</comment> M. Franceschet B. ten Cate Guarded fragments with constants Journal of Logic, Language and Information 14 3 281-288 2005 http://www.sci.unich.it/~francesc/pubs/jlli05.pdf