<document>
  <header>
    <product>resin</product>
    <title>XSLT Extensible Stylesheet Language</title>
    <keywords>
      <keyword>xslt</keyword>
    </keywords>
    <description>
      <p>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.</p>
    </description>
  </header>

  <body>
    <localtoc/>

<s1 title="Introduction">
<p>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 <a
href="#template">templates</a>.  XSLT then follows the processing
instructions for the matching template, usually adding text to the
output.</p>

<p>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.</p>

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

<p>
For example, it might process an HTML file in the following order:
</p>
<ol>
<li>HTML
</li><li>HEAD
</li><li>TITLE
</li><li>META
</li><li>BODY
</li><li>P
</li><li>TEXT
</li><li>etc
</li></ol>

<p>
      Resin's XSLT follows the 1.0 W3C specification.
      </p>
</s1>


<s1 name="xslt-core" title="XSLT core">

<s2 name="template" title="&lt;xsl:stylesheet&gt;" type="defun">
<p>The top-level element of an XSL stylesheet.</p>
</s2>

<s2 name="template" title="&lt;xsl:template ... &gt; ..." type="defun">
<p>Establishes a pattern and replacement text.</p>
<p>
<code>xsl:template</code> registers its pattern with the XSL processing
engine.  When a node matches the pattern, XSL will process the
contents of the template.
</p>

<p>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.</p>

<deftable>
<tr><th>attribute</th><th>meaning
</th></tr><tr><td>match</td><td>the XPath match pattern (required)
</td></tr><tr><td>mode</td><td>string grouping templates into a special mode
</td></tr><tr><td>name</td><td>Name for later use by xsl:call-template
</td></tr><tr><td>priority</td><td>conflict-resolving priority, an integer
</td></tr></deftable>

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

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

<example>
&lt;xsl:template match='box'&gt;
  &lt;center&gt;
  &lt;table width='80%'&gt;
  &lt;tr&gt;&lt;td&gt;

    &lt;xsl:apply-templates/&gt;

  &lt;/td&gt;&lt;/tr&gt;
  &lt;/table&gt;
  &lt;/center&gt;

&lt;/xsl:template&gt;
</example>

<example>
&lt;p&gt;Here's a boxed quote,&lt;/p&gt;

&lt;box&gt;
To be or not to be...
&lt;/box&gt;
</example>

<results>
&lt;p&gt;Here's a boxed quote,&lt;/p&gt;

&lt;center&gt;
&lt;table width='80%'&gt;
&lt;tr&gt;&lt;td&gt;

  To be or not to be...

&lt;/table&gt;
&lt;/center&gt;
</results>

</s2>

<s2 name="apply-templates" title="&lt;xsl:apply-templates ... &gt; ..." type="defun">
<p>Evaluates the children of the current node.</p>
<p>
<code>xsl:apply-templates</code> recursively processes the children.  If a
template has no <code>xsl:apply-templates</code>, then the children are
ignored.
</p>

<deftable>
<tr><th>attribute</th><th>meaning
</th></tr><tr><td>select</td><td>An XPath select pattern selecting the nodes to
evaluate next. (optional)
</td></tr><tr><td>mode</td><td>only selects templates with the given <var>mode</var>
</td></tr></deftable>

<p>The first example doubles the contents by calling
<code>xsl:apply-templates</code> twice.</p>

<example>
&lt;xsl:template match='double'&gt;
  &lt;xsl:apply-templates/&gt;
  &lt;xsl:apply-templates/&gt;
&lt;/xsl:template&gt;
</example>

<example>
&lt;double&gt;
Some &lt;foo/&gt; text.
&lt;/double&gt;
</example>

<results>
Some &lt;foo/&gt; text.
Some &lt;foo/&gt; text.
</results>

<p>The <code>select</code> pattern can restrict the children to evaluate.
Stylesheets can use it to select elements and to reorder them.

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


<example>
&lt;xsl:template match='a-b-test'&gt;
  &lt;xsl:apply-templates select='a'/&gt;
  &lt;xsl:apply-templates select='b'/&gt;
&lt;/xsl:template&gt;
</example>

<example>
&lt;a-b-test&gt;
  Junk Text.
  &lt;b/&gt;
  &lt;a&gt;
    Good text.
  &lt;/a&gt;
  More Junk.
  &lt;b&gt;
    Some B text.
  &lt;/b&gt;
  &lt;a&gt;
    More Good text.
  &lt;/a&gt;
&lt;/a-b-test&gt;
</example>

<results>
&lt;a&gt;
  Good text.
&lt;/a&gt;
&lt;a&gt;
  More Good text.
&lt;/a&gt;
&lt;b/&gt;
&lt;b&gt;
  Some B text.
&lt;b&gt;
</results>

</s2>

<s2 name="text" title="&lt;xsl:text&gt; ..." type="defun">
<p>Writes the contents to the output.</p>  
<p><code>xsl:text</code> is
useful when you need to force spacing or special text.  Usually,
Resin will produce the text you expect.  <code>xsl:text</code> is there
for the strange cases when you need full control.
</p>
</s2>

<s2 name="value-of" title="&lt;xsl:value-of .../&gt;" type="defun">
<p>Writes a calculated value output.</p>  

<deftable>
<tr><th>attribute</th><th>meaning
</th></tr><tr><td>select</td><td>An XPath expression to be printed.
</td></tr></deftable>

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

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

<s2 name="for-each" title="&lt;xsl:for-each ...&gt; ..." type="defun">
<p>Loops over child select patterns.</p>  
<p><code>xsl:foreach</code>
gives stylesheets complete control over the actions for child nodes.
</p>

<p>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.</p>

<deftable>
<tr><th>Attribute</th><th>Meaning
</th></tr><tr><td>select</td><td>XPath select pattern
</td></tr></deftable>

<example>
&lt;xsl:template match='contents'&gt;
  &lt;ol&gt;
  &lt;xsl:for-each select='section'&gt;
    &lt;li&gt;&lt;xsl:value-of select='@title'/&gt;&lt;/li&gt;
  &lt;/xsl:for-each&gt;
  &lt;/ol&gt;
&lt;/xsl:template&gt;
</example>

</s2>

<s2 name="if" title="&lt;xsl:if ...&gt; ..." type="defun">
<p>Evaluates the containing content if an expression evaluates to
true.</p>

<deftable>
<tr><th>Attribute</th><th>Meaning
</th></tr><tr><td>test</td><td>XPath expression evaluating to a boolean.
</td></tr></deftable>
</s2>

<s2 name="import" title="&lt;xsl:import .../&gt;" type="defun">
<p>Imports a stylesheet.</p>  <p><code>xsl:import</code> lets stylesheets
borrow from each other.</p>

<deftable>
<tr><th>Attribute</th><th>Meaning
</th></tr><tr><td>href</td><td>Path to the imported stylesheet
</td></tr></deftable>

</s2>

<s2 name="output" title="&lt;xsl:output .../&gt;" type="defun">
<p>Control the output printing.</p>

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

</s2>

</s1>

<s1 name="xslt" title="XSLT">

<s2 name="element" title="&lt;xsl:element&gt;" type="defun">
<p>Creates a new element.</p>  

<p>The name can be computed using an attribute value template.</p>

<deftable>
<tr><th>Attribute</th><th>Meaning
</th></tr><tr><td>name</td><td>Name of the new element.
</td></tr></deftable>

<example>
&lt;xsl:template match='a'&gt;
  &lt;xsl:element name='b{@id}'&gt;
    &lt;c/&gt;
  &lt;/xsl:element&gt;
&lt;/xsl:template&gt;
</example>
<results>
&lt;b3&gt;&lt;c/&gt;&lt;/b3&gt;
</results>
</s2>

<s2 name="attribute" title="&lt;xsl:attribute&gt;" type="defun">
<p>Adds an attribute to the element.</p>  

<p>The name can be computed using an attribute value template.</p>

<deftable>
<tr><th>Attribute</th><th>Meaning
</th></tr><tr><td>name</td><td>Name of the new attribute.
</td></tr></deftable>

<example>
&lt;xsl:template match='a'&gt;
  &lt;c&gt;
    &lt;xsl:attribute name='b{@id}'&gt;
    &lt;xsl:value-of select='c{@id}'/&gt;
    &lt;/xsl:attribute&gt;
  &lt;/c&gt;
&lt;/xsl:template&gt;
</example>
<results>
&lt;c b3='c3'/&gt;
</results>
</s2>

<s2 name="attribute-set" title="&lt;xsl:attribute-set&gt;" type="defun">
<p>Defines a named attribute set.</p>  

<p>
The attributes in the set are defined by xsl:attribute elements.
</p>

<deftable>
<tr><th>Attribute</th><th>Meaning
</th></tr><tr><td>name</td><td>Name of the attribute set.
</td></tr></deftable>

<example>
&lt;xsl:attribute-set name='font'&gt;
  &lt;xsl:attribute name='font-size'&gt;12pt&lt;/xsl:attribute&gt;
  &lt;xsl:attribute name='font-weight'&gt;bold&lt;/xsl:attribute&gt;
&lt;/xsl:attribute-set&gt;

&lt;xsl:template match='a'&gt;
  &lt;c xsl:use-attribute-sets='font'/&gt;
&lt;/xsl:template&gt;
</example>
<results>
&lt;c font-size='12pt' font-weight='bold'/&gt;
</results>

</s2>

<s2 name="pi" title="&lt;xsl:processing-instruction&gt;" type="defun">
<p>Creates a new processing instruction.</p>

<deftable>
<tr><th>Attribute</th><th>Meaning
</th></tr><tr><td>name</td><td>Processing instruction name.
</td></tr></deftable>

<example>
&lt;xsl:template match='a'&gt;
  &lt;xsl:processing-instruction name='foo'&gt;
  &lt;xsl:text&gt;Text for the PI&lt;/xsl:text&gt;
  &lt;/xsl:processing-instruction/&gt;
&lt;/xsl:template&gt;
</example>
<results>
&lt;?foo Text for the PI?&gt;
</results>
</s2>

<s2 name="comment" title="&lt;xsl:comment&gt;" type="defun">
<p>Creates a new comment.</p>  

<p>
The contents of the xsl:comment element
become the contents of the comment.
</p>

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

<s2 name="copy" title="&lt;xsl:copy&gt;" type="defun">
<p>Copies the current node, but not children or attributes, to the
output.</p> 

<p>To copy an element, a stylesheet must copy the attributes as well.</p>

<p>The following example is the identity stylesheet. It copies input
to the output including the attributes.
</p>

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

<s2 name="copy-of" title="&lt;xsl:copy-of .../&gt;" type="defun">
<p>Copies a sub-tree into the output.</p>  

<p>
<code>copy-of</code> resembles
<code>value-of</code>.  <code>value-of</code> always converts the value to a
string.  <code>copy-of</code> will copy subtrees. 
</p>

<deftable>
<tr><th>attribute</th><th>meaning
</th></tr><tr><td>select</td><td>An XPath expression to be copied.
</td></tr></deftable>
</s2>

<s2 name="variable" title="&lt;xsl:variable&gt;" type="defun">
<p>Assignes an XSL variable.</p>  

<p>
Variables can be retrieved using
the XPath variable syntax.
</p>

<deftable>
<tr><th>Attribute</th><th>Meaning
</th></tr><tr><td>name</td><td>variable name
</td></tr><tr><td>select</td><td>variable value
</td></tr></deftable>

<example>
&lt;xsl:variable name='foo' select='1+1'/&gt;

&lt;xsl:template match='a'&gt;
  &lt;xsl:value-of select='$foo'/&gt;
&lt;/xsl:template&gt;
</example>
<results>
2
</results>
</s2>

<s2 name="call-template" title="&lt;xsl:call-template&gt;" type="defun">
<p>Calls a named template with the current node.</p>
<p>
<code>xsl:call-template</code> lets stylesheets reuse common code, like
functions.  It works like <code>xsl:apply-templates select='.'</code>
except that it calls based on a template name.
</p>

<deftable>
<tr><th>Attribute</th><th>Meaning
</th></tr><tr><td>name</td><td>template name to call
</td></tr><tr><td>mode</td><td>template mode
</td></tr></deftable>
</s2>

<s2 name="param" title="&lt;xsl:param&gt;" type="defun">
<p>Declares an XSL parameter.</p>  

<p>
<code>xsl:param</code>'s
<code>select</code> parameter as a default.  If the variable has been
assigned, it uses the old value.
</p>

<deftable>
<tr><th>Attribute</th><th>Meaning
</th></tr><tr><td>name</td><td>variable name
</td></tr><tr><td>select</td><td>variable value
</td></tr></deftable>

<example>
&lt;xsl:template name='fun'&gt;
  &lt;xsl:param name='foo' select='15'/&gt;
  &lt;xsl:value-of select='$foo'/&gt;
&lt;/xsl:template&gt;

&lt;xsl:template match='a'&gt;
  &lt;xsl:call-template name='foo'&gt;
    &lt;xsl:with-param name='foo' select='1+2'/&gt;
  &lt;/xsl:call-template&gt;
&lt;/xsl:template&gt;
</example>
<results>
3
</results>
</s2>

<s2 name="apply-imports" title="&lt;xsl:apply-imports&gt;" type="defun">
<p>Like Java's <code>super</code>, calls the overridden template.</p>
</s2>

<s2 name="sort" title="&lt;xsl:sort&gt;" type="defun">
<p>Sorts nodes in xsl:apply-templates or
xsl:for-each.</p>

<deftable>
<tr><th>Attribute</th><th>Meaning
</th></tr><tr><td>select</td><td>value to sort on (default = '.')
</td></tr><tr><td>order</td><td>ascending or descending  (default = ascending)
</td></tr><tr><td>data-type</td><td>text or number (default = text)
</td></tr></deftable>

<note>case-order and lang attributes are not implemented</note>
</s2>

<s2 name="choose" title="&lt;xsl:choose ...&gt; ..." type="defun">
<p>Implements an if-elsif-else block.</p>  

<p>
The <code>xsl:when</code>
statements are tested in order.  The first matching one is executed.
If none match, the <code>xsl:otherwise</code> block is executed.
</p>

<deftable>
<tr><th>Attribute</th><th>Meaning
</th></tr><tr><td>test</td><td>XPath expression evaluating to a boolean.
</td></tr></deftable>

<example>
&lt;xsl:template match='a'&gt;
  &lt;xsl:choose&gt;
  &lt;xsl:when test='@color="red"'&gt;
    &lt;xsl:text&gt;stop&lt;/xsl:text&gt;
  &lt;/xsl:when&gt;
  &lt;xsl:when test='@color="green"'&gt;
    &lt;xsl:text&gt;go&lt;/xsl:text&gt;
  &lt;/xsl:when&gt;
  &lt;xsl:otherwise&gt;
    &lt;xsl:text&gt;yield&lt;/xsl:text&gt;
  &lt;/xsl:otherwise&gt;
  &lt;/xsl:choose&gt;
&lt;/xsl:template&gt;
</example>

</s2>
</s1>

<s1 name="xsl" title="Resin extensions">
<!--
<defun name=copy-element title='&lt;xtp:copy-element&gt;' version='resin1.0'>
<p>Copies the element to the output.</p>

<p><code/xtp:copy-element/> is extremely useful as a default rule.
With <code/xtp:copy-element/> as the default, the input gets copied to
the output.  Using the usual default, all tags get stripped.
</p>

<p>The standard default rule produces the following,</p>

<example>
&lt;a&gt;A text&lt;/a&gt;
&lt;b&gt;B text&lt;/b&gt;
</example>
<results>
A text
B text
</results>

<p>Using <code/xtp:copy-element/>, XSL stylesheets can copy elements
it doesn't understand.</p>

<example>
&lt;xsl:template match='*'&gt;
  &lt;xtp:copy-element&gt;
    &lt;xsl:apply-templates/&gt;
  &lt;/xtp:copy-element&gt;
&lt;/xsl:template&gt;
</example>

<example>
&lt;a&gt;A text&lt;/a&gt;
&lt;b&gt;B text&lt;/b&gt;
</example>
<results>
&lt;a&gt;A text&lt;/a&gt;
&lt;b&gt;B text&lt;/b&gt;
</results>


</defun>
-->
<s2 name="expression" title="&lt;xtp:expression&gt;expression ..." version="resin1.0" type="defun">
<p>Executes <var>expression</var> and prints it to the output.</p>
<p>
 Stylesheets can use any JavaScript expression.  The following
variables are pre-defined in stylesheets.
</p>

<deftable>
<tr><th>Variable</th><th>Meaning
</th></tr><tr><td>node</td><td>The current org.w3c.dom.Node.
</td></tr><tr><td>out</td><td>The com.caucho.xsl.XslWriter.
</td></tr></deftable>

<p>
In addition, the <var>out</var> variable gives access to the servlet
PageContext with the <var>page</var> property.
</p>

<example>
&lt;xsl:template match='welcome-user'&gt;
  &lt;xsl:text&gt;Welcome back, &lt;/xsl:text&gt;
  &lt;xtp:expression&gt;
    out.page.session.value.user
  &lt;xtp:expression&gt;
&lt;/xsl:template&gt;
</example>
</s2>
<s2 name="scriptlet" title="&lt;xtp:scriptlet&gt; statement_list" version="resin1.0" type="defun">
<p>Executes the <var>statement_list</var> scriptlet.</p>  

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

<p>The following example creates a number of stars:</p>

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

<example>
1 = &lt;ct:stars count='1'/&gt;
9 = &lt;ct:stars count='9'/&gt;
</example>
<results>
1 = *
9 = *********
</results>
</s2>

<s2 name="declaration" title="&lt;xtp:declaration&gt;" index="declaration" type="defun">
<p>Adds declaration code, i.e. code outside of any function.</p>

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

</example>

</s2>

<s2 name="page" title="&lt;xtp:directive.page attributes /&gt;" type="defun">
<p>Sets page directives</p>
<deftable>
<tr><th>name</th><th>meaning
</th></tr><tr><td>language</td><td>script language, default Java
</td></tr><tr><td>session</td><td>use sessions, default false
</td></tr><tr><td>errorPage</td><td>page to display for errors
</td></tr><tr><td>errorPage</td><td>page to display for errors
</td></tr><tr><td>import</td><td>imports Java packages 
</td></tr><tr><td>contentType</td><td>content-type of the generated page
</td></tr></deftable>

</s2>
<!--
<defun name=cache title='&lt;xtp:directive.cache attributes/>'>

<p>Caches the generated JSP file by default.</p>

<p>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.</p>

<p>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.
</p>

<deftable>
<tr><th>name<th>meaning
<tr><td>file<td>the JSP file depends on <var/file/>.
<tr><td>no-cache<td>do not cache the generated JSP.
</deftable>

</defun>
-->

</s1>
  </body>
</document>
