|
![]() |
![]() 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:
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.
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.
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.
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.
The first example doubles the contents by calling
The The following example writes the 'a' nodes followed by the 'b' nodes and ignores everything else.
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.
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.
Evaluates the containing content if an expression evaluates to
true.
Imports a stylesheet.
xsl:import lets stylesheets
borrow from each other.
Creates a new element. The name can be computed using
an attribute value template.
Note: The namespace attribute is not yet implemented in Resin.
Adds an attribute to the element. The name can be computed
using an attribute value template.
Note: The namespace attribute is not yet implemented in Resin.
Defines a named attribute set. The attributes in the set
are defined by xsl:attribute elements.
Note: The xsl:use-attribute-sets is not recognized in xsl:attribute-set.
Creates a new processing instruction.
Creates a new comment. The contents of the xsl:comment element
become the contents of the comment.
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.
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.
Assignes an XSL variable. Variables can be retrieved using
the XPath variable syntax.
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.
Declares an XSL parameter.
xsl:param 's
select parameter as a default. If the variable has been
assigned, it uses the old value.
Like Java's
super , calls the overridden template.
Sorts nodes in xsl:apply-templates or
xsl:for-each.
Note: case-order is not implemented
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.
Executes
and prints it to the output.
Stylesheets can use any JavaScript expression. The following
variables are pre-defined in stylesheets.
Executes the
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:
Adds declaration code, i.e. code outside of any function.
Sets page directives
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.
|