Resin 1.1
FAQ
Reference
JavaDoc
Demo
Java Tutorial
Getting Started
Configuration
Servlet
JSP
XTP/XSL
JavaScript
JS Library
XTP intro
XTP to JSP
XPath
XPath Fun
XSL
XSL lite
XSL lite examples
Extensible Stylesheet Language
The extensible stylesheet language is a language for transforming XML documents to new XML documents. It's based on matching elements in the XML tree and replacing the element with new XML. For example, replacing a 'definition' tag with an HTML table with a silver background. XTP (XML Template Pages) uses XSL to transform HTML into new HTML. XTP treats unknown tags as raw text. Pure XML requires XSL stylesheets to be valid XML. XTP can be configured to follow strict XSLT syntax, by setting the servlet init-param 'xsl-strict' to true. XSL 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. XSL then follows the processing instructions for the matching template, usually adding text to the output. If XSL 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 XSL stylesheet will remove all the tags and just print out the text. When it's done with the current node, XSL moves to the next one until the entire input tree is complete. For example, it might process an HTML file in the following order: HTML HEAD TITLE META BODY P TEXT etc
XSLT core
<xsl:stylesheet> | The top-level element of an XSL stylesheet.
<xsl:template ... > ... | Establishes a pattern and replacement text.
<xsl:apply-templates ... > ... | Evaluates the children of the current node.
<xsl:text> ... | Writes the contents to the output.
<xsl:value-of .../> | Writes a calculated value output.
<xsl:for-each ...> ... | Loops over child select patterns.
<xsl:if ...> ... | Evaluates the containing content if an expression evaluates to true.
<xsl:import .../> | Imports a stylesheet.
XSLT
<xsl:element> | Creates a new element.
<xsl:attribute> | Adds an attribute to the element.
<xsl:attribute-set> | Defines a named attribute set.
<xsl:processing-instruction> | Creates a new processing instruction.
<xsl:comment> | Creates a new comment.
<xsl:copy> | Copies the current node, but not children or attributes, to the output.
<xsl:copy-of .../> | Copies a sub-tree into the output.
<xsl:variable> | Assignes an XSL variable.
<xsl:call-template> | Calls a named template with the current node.
<xsl:param> | Declares an XSL parameter.
<xsl:apply-imports> | Like Java's `super`, calls the overridden template.
<xsl:sort> | Sorts nodes in xsl:apply-templates or xsl:for-each.
<xsl:choose ...> ... | Implements an if-elsif-else block.
Resin extensions
<xtp:expression>expression ... | Executes expression and prints it to the output.
<xtp:scriptlet> statement_list | Executes the statement_list scriptlet.
<xtp:declaration> | Adds declaration code, i.e. code outside of any function.
<xtp:directive.page attributes /> | Sets page directives
<xtp:directive.cache attributes/> | Caches the generated JSP file by default.
XSLT core
### <xsl:stylesheet>
The top-level element of an XSL stylesheet. Resin adds a single attribute to xsl:stylesheet, parsed-content. If parsed-content is set to false, xsl elements are treated as text. By default, the normal XSL behaviour is assumed.
parsed-content | false if non-xsl elements should be treated as raw text.
### <xsl:template ... > ...
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.
attribute | meaning
--- | ---
match | the XPath match pattern (required)
mode | string grouping templates into a special mode
name | Name for later use by xsl:call-template
priority | conflict-resolving priority, an integer
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.
<xsl:template match='box'> <center> <table width='80%'> <tr><td> <xsl:apply-templates/> </table> </center> </xsl:template>
<p>Here's a boxed quote,</p> <box> To be or not to be... </box>
<p>Here's a boxed quote,</p> <center> <table width='80%'> <tr><td> To be or not to be... </table> </center>
### <xsl:apply-templates ... > ...
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.
attribute | meaning
--- | ---
select | An XPath select pattern selecting the nodes to evaluate next. (optional)
mode | only selects templates with the given mode
The first example doubles the contents by calling `xsl:apply-templates` twice.
<xsl:template match='double'> <xsl:apply-templates/> <xsl:apply-templates/> </xsl:template>
<double> Some <foo/> text. </double>
Some <foo/> text. Some <foo/> text.
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.
<xsl:template match='a-b-test'> <xsl:apply-templates select='a'/> <xsl:apply-templates select='b'/> </xsl:template>
<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>
<a> Good text. </a> <a> More Good text. </a> <b/> <b> Some B text. <b>
### <xsl:text> ...
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.
### <xsl:value-of .../>
Writes a calculated value output.
attribute | meaning
--- | ---
select | An XPath expression to be printed.
`value-of` is particularly useful for extracting attribute values. The following example creates a JSP tag which adds two numbers.
<xsl:template match='ct:sum'> <jsp:expression> <xsl:value-of select='@a'> + <xsl:value-of select='@b'> </jsp:expression> </xsl:template>
### <xsl:for-each ...> ...
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.
Attribute | Meaning
--- | ---
select | XPath select pattern
<xsl:template match='contents'> <ol> <xsl:for-each select='section'> <li><xsl:value-of select='@title'/> </xsl:for-each> </ol> </xsl:template>
### <xsl:if ...> ...
Evaluates the containing content if an expression evaluates to true.
Attribute | Meaning
--- | ---
test | XPath expression evaluating to a boolean.
### <xsl:import .../>
Imports a stylesheet. `xsl:import` lets stylesheets borrow from each other.
Attribute | Meaning
--- | ---
href | Path to the imported stylesheet
XSLT
### <xsl:element>
Creates a new element. The name can be computed using an attribute value template.
Attribute | Meaning
--- | ---
name | Name of the new element.
<xsl:template match='a'> <xsl:element name='b{@id}'> <c/> </xsl:element> </xsl:template>
<b3><c/></b3>
Note: The namespace attribute is not yet implemented in Resin.
### <xsl:attribute>
Adds an attribute to the element. The name can be computed using an attribute value template.
Attribute | Meaning
--- | ---
name | Name of the new attribute.
<xsl:template match='a'> <c> <xsl:attribute name='b{@id}'> <xsl:value-of select='c{@id}'/> </xsl:attribute> </c> </xsl:template>
<c b3='c3'/>
Note: The namespace attribute is not yet implemented in Resin.
### <xsl:attribute-set>
Defines a named attribute set. The attributes in the set are defined by xsl:attribute elements.
Attribute | Meaning
--- | ---
name | Name of the attribute set.
<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>
<c font-size='12pt' font-weight='bold'/>
Note: The xsl:use-attribute-sets is not recognized in xsl:attribute-set.
### <xsl:processing-instruction>
Creates a new processing instruction.
Attribute | Meaning
--- | ---
name | Processing instruction name.
<xsl:template match='a'> <xsl:processing-instruction name='foo'> <xsl:text>Text for the PI</xsl:text> </xsl:processing-instruction/> </xsl:template>
<?foo Text for the PI?>
### <xsl:comment>
Creates a new comment. The contents of the xsl:comment element become the contents of the comment.
<xsl:template match='a'> <xsl:comment> <xsl:text>Text for the comment</xsl:text> </xsl:processing-instruction/> </xsl:template>
<!--Text for the comment-->
### <xsl:copy>
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.
<xsl:template match='@*|node()'> <xsl:copy> <xsl:apply-templates select='@*|node()'/> </xsl:copy> </xsl:template>
### <xsl:copy-of .../>
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.
attribute | meaning
--- | ---
select | An XPath expression to be copied.
### <xsl:variable>
Assignes an XSL variable. Variables can be retrieved using the XPath variable syntax.
Attribute | Meaning
--- | ---
name | variable name
select | variable value
<xsl:variable name='foo' select='1+1'/> <xsl:template match='a'> <xsl:value-of select='$foo"/> </xsl:template>
2
### <xsl:call-template>
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.
Attribute | Meaning
--- | ---
name | template name to call
mode | template mode
### <xsl:param>
Declares an XSL parameter. `xsl:param`'s `select` parameter as a default. If the variable has been assigned, it uses the old value.
Attribute | Meaning
--- | ---
name | variable name
select | variable value
<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>
3
### <xsl:apply-imports>
Like Java's `super`, calls the overridden template.
### <xsl:sort>
Sorts nodes in xsl:apply-templates or xsl:for-each.
Attribute | Meaning
--- | ---
select | value to sort on (default = '.')
order | ascending or descending (default = ascending)
data-type | text or number (default = text)
Note: case-order is not implemented
### <xsl:choose ...> ...
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.
Attribute | Meaning
--- | ---
test | XPath expression evaluating to a boolean.
<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>
Resin extensions
### <xtp:expression>expression ... | ### Resin 1.0
Executes expression and prints it to the output. Stylesheets can use any JavaScript expression. The following variables are pre-defined in stylesheets.
Variable | Meaning
--- | ---
node | The current node.
out | The output stream.
In addition, the out variable gives access to the servlet PageContext with the page property.
<xsl:template match='welcome-user'> <xsl:text>Welcome back, </xsl:text> <xtp:expression> out.page.session.value.user <xtp:expression> </xsl:template>
### <xtp:scriptlet> statement_list | ### Resin 1.0
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:
<@# 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>
1 = <ct:stars count='1'/> 9 = <ct:stars count='9'/>
1 = * 9 = *********
### <xtp:declaration>
Adds declaration code, i.e. code outside of any function.
<xtp:declaration> function dist(x1, y1, x2, y2) { return Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); } </xtp:declaration>
### <xtp:directive.page attributes />
Sets page directives
name | meaning
--- | ---
language | script language, default Java
session | use sessions, default false
errorPage | page to display for errors
errorPage | page to display for errors
import | imports Java packages
contentType | content-type of the generated page
### <xtp:directive.cache attributes/>
Caches the generated JSP file by default. Caching for XSL is more complicated than for JSP because only some templates may be used in a page. Caching is based on the generated page, not simply on the stylesheet. A page that just uses static templates is automatically cached. Pages that use scripts just for simple calculation can also be cached. But pages that use scripts based on the request cannot be cached.
name | meaning
--- | ---
file | the JSP file depends on file.
no-cache | do not cache the generated JSP.
XPath Fun |  | XSL lite
Copyright © 1998-2000 Caucho Technology. All rights reserved. Last modified: Wed, 15 Dec 1999 07:12:06 -0800 (PST)