<document>
<localtoc/>

XSLT processes the input document recursively from the top down, essentially a depth first traversal of the tree. When it examines each node, it finds the best match from all the templates. XSLT then follows the processing instructions for the matching template, usually adding text to the output.

If XSLT cannot find a matching template, it applies default rules. Text gets copied to the output. The children of elements are processed, but the elements themselves are not copied. So a completely blank XSLT stylesheet will remove all the tags and just print out the text.

When it's done with the current node, XSLT moves to the next one until the entire input tree is complete.

For example, it might process an HTML file in the following order:

1. HTML
2. HEAD
3. TITLE
4. META
5. BODY
6. P
7. TEXT
8. etc

Resin's XSLT follows the 1.0 W3C specification.

The top-level element of an XSL stylesheet.

Establishes a pattern and replacement text.

`xsl:template` registers its pattern with the XSL processing engine. When a node matches the pattern, XSL will process the contents of the template.

Pure XSL processes the contents slightly differently than XTP. XSL expects all tags to be valid XML. XTP is more forgiving. If the tag is not one of those defined by XSL, it will treat the tag as raw text.

<deftable>
attributemeaning
matchthe XPath match pattern (required)
modestring grouping templates into a special mode
nameName for later use by xsl:call-template
priorityconflict-resolving priority, an integer
</deftable>

In the following example, the template matches any 'box' tag. The contents of the box are placed in a centered table 80% of the current width.

This example is legal in XTP because the <td> and <tr> are treated as raw text. The example is illegal in XSL because those tags are missing their close tags.

<example> <xsl:template match='box'> <center> <table width='80%'> <tr><td> <xsl:apply-templates/> </td></tr> </table> </center> </xsl:template> </example> <example> <p>Here's a boxed quote,</p> <box> To be or not to be... </box> </example> <results> <p>Here's a boxed quote,</p> <center> <table width='80%'> <tr><td> To be or not to be... </table> </center> </results>

Evaluates the children of the current node.

`xsl:apply-templates` recursively processes the children. If a template has no `xsl:apply-templates`, then the children are ignored.

<deftable>
attributemeaning
selectAn XPath select pattern selecting the nodes to evaluate next. (optional)
modeonly selects templates with the given mode
</deftable>

The first example doubles the contents by calling `xsl:apply-templates` twice.

<example> <xsl:template match='double'> <xsl:apply-templates/> <xsl:apply-templates/> </xsl:template> </example> <example> <double> Some <foo/> text. </double> </example> <results> Some <foo/> text. Some <foo/> text. </results>

The `select` pattern can restrict the children to evaluate. Stylesheets can use it to select elements and to reorder them.

The following example writes the 'a' nodes followed by the 'b' nodes and ignores everything else.

<example> <xsl:template match='a-b-test'> <xsl:apply-templates select='a'/> <xsl:apply-templates select='b'/> </xsl:template> </example> <example> <a-b-test> Junk Text. <b/> <a> Good text. </a> More Junk. <b> Some B text. </b> <a> More Good text. </a> </a-b-test> </example> <results> <a> Good text. </a> <a> More Good text. </a> <b/> <b> Some B text. <b> </results>

Writes the contents to the output.

`xsl:text` is useful when you need to force spacing or special text. Usually, Resin will produce the text you expect. `xsl:text` is there for the strange cases when you need full control.

Writes a calculated value output.

<deftable>
attributemeaning
selectAn XPath expression to be printed.
</deftable>

`value-of` is particularly useful for extracting attribute values. The following example creates a JSP tag which adds two numbers.

<example> <xsl:template match='ct:sum'> <jsp:expression> <xsl:value-of select='@a'> + <xsl:value-of select='@b'> </jsp:expression> </xsl:template> </example>

Loops over child select patterns.

`xsl:foreach` gives stylesheets complete control over the actions for child nodes.

Usually, stylesheets will want to use the full pattern matching capability given by XSL. Sometimes the specific structure is known, like sections in a chapter. When generating a table of contents, it may be easier to scan over the sections.

<deftable>
AttributeMeaning
selectXPath select pattern
</deftable> <example> <xsl:template match='contents'> <ol> <xsl:for-each select='section'> <li><xsl:value-of select='@title'/></li> </xsl:for-each> </ol> </xsl:template> </example>

Evaluates the containing content if an expression evaluates to true.

<deftable>
AttributeMeaning
testXPath expression evaluating to a boolean.
</deftable>

Imports a stylesheet.

`xsl:import` lets stylesheets borrow from each other.

<deftable>
AttributeMeaning
hrefPath to the imported stylesheet
</deftable>

Control the output printing.

<deftable>
AttributeMeaning
methodxml or html or text. Select printing method
versionXML version
encodingcharacter set to print the results
omit-xml-declarationskip the XML or HTML declaration
indentpretty-print or not
media-typemime-type
disable-output-escaping'<' gets printed as '<', not '&lt;'
</deftable>

Creates a new element.

The name can be computed using an attribute value template.

<deftable>
AttributeMeaning
nameName of the new element.
</deftable> <example> <xsl:template match='a'> <xsl:element name='b{@id}'> <c/> </xsl:element> </xsl:template> </example> <results> <b3><c/></b3> </results>

Adds an attribute to the element.

The name can be computed using an attribute value template.

<deftable>
AttributeMeaning
nameName of the new attribute.
</deftable> <example> <xsl:template match='a'> <c> <xsl:attribute name='b{@id}'> <xsl:value-of select='c{@id}'/> </xsl:attribute> </c> </xsl:template> </example> <results> <c b3='c3'/> </results>

Defines a named attribute set.

The attributes in the set are defined by xsl:attribute elements.

<deftable>
AttributeMeaning
nameName of the attribute set.
</deftable> <example> <xsl:attribute-set name='font'> <xsl:attribute name='font-size'>12pt</xsl:attribute> <xsl:attribute name='font-weight'>bold</xsl:attribute> </xsl:attribute-set> <xsl:template match='a'> <c xsl:use-attribute-sets='font'/> </xsl:template> </example> <results> <c font-size='12pt' font-weight='bold'/> </results>

Creates a new processing instruction.

<deftable>
AttributeMeaning
nameProcessing instruction name.
</deftable> <example> <xsl:template match='a'> <xsl:processing-instruction name='foo'> <xsl:text>Text for the PI</xsl:text> </xsl:processing-instruction/> </xsl:template> </example> <results> <?foo Text for the PI?> </results>

Creates a new comment.

The contents of the xsl:comment element become the contents of the comment.

<example> <xsl:template match='a'> <xsl:comment> <xsl:text>Text for the comment</xsl:text> </xsl:processing-instruction/> </xsl:template> </example> <results> <!--Text for the comment--> </results>

Copies the current node, but not children or attributes, to the output.

To copy an element, a stylesheet must copy the attributes as well.

The following example is the identity stylesheet. It copies input to the output including the attributes.

<example> <xsl:template match='@*|node()'> <xsl:copy> <xsl:apply-templates select='@*|node()'/> </xsl:copy> </xsl:template> </example>

Copies a sub-tree into the output.

`copy-of` resembles `value-of`. `value-of` always converts the value to a string. `copy-of` will copy subtrees.

<deftable>
attributemeaning
selectAn XPath expression to be copied.
</deftable>

Assignes an XSL variable.

Variables can be retrieved using the XPath variable syntax.

<deftable>
AttributeMeaning
namevariable name
selectvariable value
</deftable> <example> <xsl:variable name='foo' select='1+1'/> <xsl:template match='a'> <xsl:value-of select='$foo'/> </xsl:template> </example> <results> 2 </results>

Calls a named template with the current node.

`xsl:call-template` lets stylesheets reuse common code, like functions. It works like `xsl:apply-templates select='.'` except that it calls based on a template name.

<deftable>
AttributeMeaning
nametemplate name to call
modetemplate mode
</deftable>

Declares an XSL parameter.

`xsl:param`'s `select` parameter as a default. If the variable has been assigned, it uses the old value.

<deftable>
AttributeMeaning
namevariable name
selectvariable value
</deftable> <example> <xsl:template name='fun'> <xsl:param name='foo' select='15'/> <xsl:value-of select='$foo'/> </xsl:template> <xsl:template match='a'> <xsl:call-template name='foo'> <xsl:with-param name='foo' select='1+2'/> </xsl:call-template> </xsl:template> </example> <results> 3 </results>

Like Java's `super`, calls the overridden template.

Sorts nodes in xsl:apply-templates or xsl:for-each.

<deftable>
AttributeMeaning
selectvalue to sort on (default = '.')
orderascending or descending (default = ascending)
data-typetext or number (default = text)
</deftable> <note>case-order and lang attributes are not implemented</note>

Implements an if-elsif-else block.

The `xsl:when` statements are tested in order. The first matching one is executed. If none match, the `xsl:otherwise` block is executed.

<deftable>
AttributeMeaning
testXPath expression evaluating to a boolean.
</deftable> <example> <xsl:template match='a'> <xsl:choose> <xsl:when test='@color="red"'> <xsl:text>stop</xsl:text> </xsl:when> <xsl:when test='@color="green"'> <xsl:text>go</xsl:text> </xsl:when> <xsl:otherwise> <xsl:text>yield</xsl:text> </xsl:otherwise> </xsl:choose> </xsl:template> </example>

Executes expression and prints it to the output.

Stylesheets can use any JavaScript expression. The following variables are pre-defined in stylesheets.

<deftable>
VariableMeaning
nodeThe current org.w3c.dom.Node.
outThe com.caucho.xsl.XslWriter.
</deftable>

In addition, the out variable gives access to the servlet PageContext with the page property.

<example> <xsl:template match='welcome-user'> <xsl:text>Welcome back, </xsl:text> <xtp:expression> out.page.session.value.user <xtp:expression> </xsl:template> </example>

Executes the statement_list scriptlet.

The JavaScript code can be any statement list. The same implicit variables are allowed in scriptlets as in expressions.

The following example creates a number of stars:

<example> <@# page language='javascript' #> <xsl:template match='ct:stars'> <xtp:scriptlet> for (var i = 0; i < node.attribute.count; i++) out.write('*'); </xtp:scriptlet> </xsl:template> </example> <example> 1 = <ct:stars count='1'/> 9 = <ct:stars count='9'/> </example> <results> 1 = * 9 = ********* </results>

Adds declaration code, i.e. code outside of any function.

<example> <xtp:declaration> function dist(x1, y1, x2, y2) { return Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); } </xtp:declaration> </example>

Sets page directives

<deftable>
namemeaning
languagescript language, default Java
sessionuse sessions, default false
errorPagepage to display for errors
errorPagepage to display for errors
importimports Java packages
contentTypecontent-type of the generated page
</deftable>
</document>