René Nyffenegger's collection of things on the web | |
René Nyffenegger on Oracle - Most wanted - Feedback
- Follow @renenyffenegger
|
XSLT | ||
XSLT is a transformation language for XML, that is, it can transform documents of one XML Application into documents of another XML Application.
There is also XSLF (F standing for formatting objects) [also called XSL-FO)
XSLT originates from css and DSSSL (= Document Style Semantics and Specification Language) (pronounced:
dessel)
XSLT is a declarative language: we don't specify how we want to transform something, but how it must look like when transformed.
Transformation needs those three things: an XML document, a tranformation stylesheet and a software
that generates a new (transformed) XML document. The stylesheet is written
in XSLT.
A minimal stylesheet<?xml version="1.0" ?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <!-- further declarations --> </xsl:stylesheet>
Note: the xsl namespace prefix is needed to distinguish between elements that are part of the stylesheet and those used
for the result tree.
XML document as a tree
Seven node types:
Rules
A rule does three things:
Template rules
In a template rule, the resulting subtree is explicitley stated in a template element.
<?xml version="1.0" ?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" > <xsl:template match="/"> <html> <head><title>Some Title</title></head> <body>Hello world</body> </html> </xsl:template> </xsl:stylesheet>
This example produces the same html file regardless of the input file.
The transformation process
The tranformation process works like this:
XSLT specific tagsxsl:template
rules: if the template is not a named templates it states a rule about processing.
named templates:
A named template is the equivalent of a function in a programming language. xsl:call-template is used to call a template.
Parameters for named parameters are passed through the xsl:param instruction. A template becomes a named parameter by setting its name attribute. xsl:apply-templates
If present, this instruction prccesses all of the children of the current node. The nodes processed can be limited by setting the
select attribute.
xsl:apply-importsxsl:call-template
Used to call a xsl:template
xsl:elementxsl:attributexsl:value-of
select is a required attribute. The value of the select's XPath expression will be written into the output stream.
xsl:ifxsl:messagexsl:text
Writes text out. Useful when outputting non nested tags. See here.
xsl:choose
The xsl:choose ... xsl:when .... xsl:when.... xsl:otherwise construct is like
a switch ... case statement in c.
xsl:when<xsl:when test="system-property('xsl:version') >= 1.1"> <!-- .... !> </xsl:when> <xsl:otherwise> </xsl:otherwise>
See example coloring every other row differently.
xsl:otherwise
See xsl:when
See example coloring every other row differently.
xsl:importxsl:include
Used to include other stylesheets.
xsl:strip-spacexsl:preserve-spacexsl:output
Attributes:
xsl:number
The attribute format can be:
See Enumerating with roman numbers for an example.
xsl:keyxsl:for-each
See Enumerating chapters for an example.
xsl:sort<xsl:apply-templates select="item"> <xsl:sort select="name" order="descending" data-type="number"/> </xsl:apply-templates>
See also sorting example and
here as well.
xsl:decimal-formatxsl:namespace-aliasxsl:attribute-setxsl:variablexsl:with-param
Used to pass parameters in a xsl:call-template call.
xsl:param
Provides parameters for xsl:template.
xsl:copy-ofxsl:stylesheet
See also xsl:transform.
Must have a version attribute.
xsl:transform
xsl:transform is a synonym for xsl:stylesheet
XSLT specific attributesmatch
The value of match is a pattern.
select
The value of select is a XPath expression.
Patterns
A pattern looks like an XPath expression but it is treated differently.
A pattern is used for matching nodes in the tree against a specified criteria.
Referencing a stylesheet within an XML document
A processing instruction can be used to reference a stylesheet from within an XML document:
<?xml-stylesheet type="text/xml" href="the_stylesheet.xsl" ?> disabling implicit templates<xsl:template match="@*|text()"> </xsl:template> Links
XT is a fast, free implementation of XSLT in java.
Xalan
Xalan -o outputfile xmlSource stylesheet
|