caucho
Resin
FAQ
Reference Guide
JavaDoc
Demo
Tutorial

Getting Started
Configuration
EJB
Topics
JSP
XTP/XSL

XPath
XPath Fun
XSL
StyleScript
XTP
 StyleScript

XSL
XTP/XSL
XTP

StyleScript Core
$apply-templates(select); Evaluates the children of the current node.
$attribute(name) <<...>> Adds an attribute to the element.
$copy() <<...>> Copies the current node, but not children or attributes, to the output.
$if (test) <<... >> Executes the template if test is true.
$output(...); Controls the output printing.
$template(match) << ... >> Establishes a pattern and replacement text.
$value-of(select); Writes a calculated value output.

StyleScript Elements
$apply-imports(); Like Java's super, calls the overridden template.
$attribute-set(name) <<...>> Defines a named attribute set.
$call-template(name) <<...>> Calls a named template with the current node.
$choose(test) <<...>> Implements a select block.
$comment() <<...>> Creates a new comment.
$copy-of(select); Copies a sub-tree into the output.
$element(name) <<...>> Creates a new element.
$for-each(select) << ... >> Loops over child select patterns.
$import(href); Imports a stylesheet.
$param(name); Declares an XSL parameter.
$processing-instruction(name) <<...>> Creates a new processing instruction.
$sort(...) <<...>> Sorts nodes in $apply-templates or $for-each.
$text() <<...>> Writes the contents to the output.
$variable(name); Assignes an XSL variable.

Resin extensions
$expr(expr); Prints the value of the Java expression, expr.
$scriptlet() << statement_list >> Executes the statement_list scriptlet.
$declaration Adds declaration code, i.e. code outside of any function.
$directive.page(attributes); Sets page directives
$directive.cache(attributes); Caches the generated JSP file by default.

Abbreviations
$(expr) $value-of shortcut prints the value of the XPath expression.
<#= expression #> $expression shortcut prints the value of expression using the page's scripting language.
<# statement-list #> $scriptlet shortcut executes the statements using the page's scripting language.
<#! declaration #> $declaration shortcut defines functions and variables.

StyleScript Core

$apply-templates(select);

Evaluates the children of the current node. $apply-templates recursively processes the children. If a template has no $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 following example writes the 'a' nodes followed by the 'b' nodes and ignores everything else.

$template(double) <<
  $apply-templates(a);
  $apply-templates(b);
>>
<double>
  <b>b1</b>
  <a>a1</a>
  <b>b2</b>
  <a>a2</a>
</double>
a1
a2
b1
b2

$attribute(name) <<...>>

Adds an attribute to the element. The name can be computed using an attribute value template.

Attribute Meaning
name Name of the new attribute.

$template(a) <<
  <c>
    $attribute (b{@id}) <<
      $value-of(c{@id});
    >>
  </c>
>>
<c b3='c3'/>

$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.

$template(@*|node()) <<
  $copy() <<
    $apply-templates(@*|node());
  >>
>>

$if (test) <<... >>

Executes the template if test is true.

$template(signal) <<
  $if (@color="red") <<stop>>
  $else if (@color="green") <<go>>
  $else <<yield>>
>>
<signal color="green"/>
<signal color="ultramaroon"/>
<signal color="red"/>
go
yield
stop

$output(...);

Controls the output printing.

Attribute Meaning
method xml or html or text. Select printing method
version XML version
encoding character set to print the results
omit-xml-declaration skip the XML or HTML declaration
indent pretty-print or not
media-type mime-type
disable-output-escaping '<' gets printed as '<', not '&lt;'

$template(match) << ... >>

Establishes a pattern and replacement text. $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 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.

$template(box) <<
  <center>
  <table width='80%'>
  <tr><td>

    $apply-templates();
  </td></tr>
  </table>
  </center>

>>
<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...
</td></tr>
</table>
</center>

$value-of(select);

Writes a calculated value output.

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

$template(ct:sum) <<
<jsp:expression>
$value-of(@a); + $value-of(@b);
</jsp:expression>
>>

StyleScript Elements

$apply-imports();

Like Java's super, calls the overridden template.

$attribute-set(name) <<...>>

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

Attribute Meaning
name Name of the attribute set.

$attribute-set(font) <<
  $attribute(font-size) <<12pt>>
  $attribute(font-weight) <<bold>>
>>

$template(a) <<
  <c xsl:use-attribute-sets='font'/>
>>
<c font-size='12pt' font-weight='bold'/>

$call-template(name) <<...>>

Calls a named template with the current node. $call-template lets stylesheets reuse common code, like functions. It works like $apply-templates(.); except that it calls based on a template name.

Attribute Meaning
name template name to call
mode template mode

$choose(test) <<...>>

Implements a select block. The $when statements are tested in order. The first matching one is executed. If none match, the $otherwise block is executed.

Attribute Meaning
test XPath expression evaluating to a boolean.

$template(a) <<
  $choose() <<
    $when(@color="red") <<stop>>
    $when(@color="green") <<go>>
    $otherwise() <<yield>>
  >>
>>

$comment() <<...>>

Creates a new comment. The contents of the $comment() element become the contents of the comment.

$template(a) <<
  $comment() << Text for the comment. >>
>>
<!-- Text for the comment. -->

$copy-of(select);

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.

$element(name) <<...>>

Creates a new element. The name can be computed using an attribute value template.

Attribute Meaning
name Name of the new element.

$template(a) <<
  $element(b{@id}) <<
    <c/>
  >>
>>
<a id="3"/>
<b3><c/></b3>

$for-each(select) << ... >>

Loops over child select patterns. $for-each() 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.

$template(contents) <<
  <ol>
  $for-each(section) <<
    <li>$value-of(@title);</li>
  >>
  </ol>
>>

$import(href);

Imports a stylesheet. $import lets stylesheets borrow from each other.

Attribute Meaning
href Path to the imported stylesheet

$param(name);

Declares an XSL parameter. $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

$template(name=>fun) <<
  $param(foo, select=>15);
  $value-of($foo);
>>

$template(a) <<
  $call-template(fun) <<
    $with-param(foo, select=>1+2);
  >>
>>

3

$processing-instruction(name) <<...>>

Creates a new processing instruction.

Attribute Meaning
name Processing instruction name.

$template(a) <<
  $processing-instruction(foo) << Text for the PI. >>
>>
<?foo Text for the PI?>

$sort(...) <<...>>

Sorts nodes in $apply-templates or $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

$text() <<...>>

Writes the contents to the output. $text is useful when you need to force spacing or special text. Usually, StyleScript will produce the text you expect. $text is there for the strange cases when you need full control.

$variable(name);

Assignes an XSL variable. Variables can be retrieved using the XPath variable syntax.

Attribute Meaning
name variable name
select variable value

$variable(foo, select=>1+1);

$template(a) <<
  $value-of($foo);
>>
2

Resin extensions

$expr(expr);

Resin 1.2

Prints the value of the Java expression, expr. Stylesheets can use any Java expression. The following variables are pre-defined in stylesheets.

Variable Meaning
node The current node.
out The XslWriter.
In addition, the out variable gives access to the servlet PageContext with the page property.

$template(welcome-user) <<
  $text() <<Welcome back, >>
  $scriptlet() <<
    String user = "Harry";
  >>
  $expr(user);
>>

$scriptlet() << statement_list >>

Resin 1.2

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:

$template(ct:stars) <<
  $scriptlet() <<
    int count = Integer.intValue(node.getAttribute("count"));
    for (int i = 0; i < count; i++)
      out.print('*');
  >>
>>
1 = <ct:stars count='1'/>

9 = <ct:stars count='9'/>

1 = *
9 = *********

$declaration

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

$declaration <<
double dist(double x1, double y1, double x2, double y2)
{
  return Math.sqrt((x1 - x2) * (x1 - x2) +
                   (y1 - y2) * (y1 - y2));
}
>>

$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

$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.

Abbreviations

$(expr)

$value-of shortcut prints the value of the XPath expression.

The value-of syntax is equivalent to:

$value-of(expr);

<#= expression #>

$expression shortcut prints the value of expression using the page's scripting language.

The expression syntax is equivalent to:

$expr(expression);
For example, to print the request URL using JavaScript:

ct:url <<
url: <#= out.page.request.requestURL #>
>>
url: /test/url.xtp

<# statement-list #>

$scriptlet shortcut executes the statements using the page's scripting language.

The scriptlet is any statement list in the language, e.g. Java.

The scriptlet syntax is equivalent to

$scriptlet() <<
scriptlet
>>
For example, to print all headers in JavaScript:

ct:headers <<
Headers: <# 
  for (var header in out.page.request.header) {
    out.println(header, ":", out.page.request.header[header]);
  }
#>
>>

<#! declaration #>

$declaration shortcut defines functions and variables.

The declaration syntax is equivalent to:

$declaration() <<
declaration
>>

<#!
function dist(x1, y1, x2, y2)
{
  return Math.sqrt((x1 - x2) * (x1 - x2) +
                   (y1 - y2) * (y1 - y2));
}
#>

ct:dist <<
($(@x1),$(@y1)) to ($(@x2),$(@y2)) = <#=
dist(node.attribute.x1, node.attribute.y1,
     node.attribute.x2, node.attribute.y2)
#>

>>
<ct:dist x1='0' y1='0' x2='5' y2='12'/>
(0,0) to (5,12) = 13

XSL
XTP/XSL
XTP
Copyright © 1998-2001 Caucho Technology. All rights reserved.
Copyright © 1998-2001 Caucho Technology. All rights reserved.