Run an XSL transformation of an XML document.
Task package: | org.schmant.task.base |
Java package: | org.schmant.task.xml.xslt |
Category: | XML tasks |
Since: | 0.5 |
EntityFS-aware? | Yes* |
Implements: | ActionTaskFactory GeneratorTaskFactory ProcessTaskFactory |
Produces: | The interpreted target property. The exact type depends on the type used for the target. |
See also: | TemplateCompilerTF |
Description:
Run an XSL transformation of an XML document.
The XSL template is either represented as a compiled Templates document (the templates property), or as an XML Source (the stylesheet property). By using a precompiled Templates object, the XSL template can be shared between several transformation tasks.
The XSL transformation is run using Java's standard Transformer API. If another implementation than the current standard implementation should be used, that can be set in the transformerFactoryClassName property. The default implementation is often Java's own implementation, but that may have been changed by service descriptions in JAR:s on the classpath.
Concurrency note: Be careful with running XSL transformation tasks in parallel threads. Not all XSL transformer and XSL stylesheet combinations are thread safe (is any?). For instance is not the Xalan + Docbook XSL combination thread safe.
Required properties
Properties
attributes | top |
A list of TransformerFactory attributes. The attributes are set on the transformer factory in the order that they were added.
- Setter method:
clearAttributes()
Clear the list of attributes.- Setter method:
setAttribute(String name, Object value)
Set one attribute.parameters:name
– The attribute's name.value
– The attribute's value.- Setter method:
setAttributes(Object o)
Set one or several attributes.parameters:
features | top |
A collection of features for the
TransformerFactory
.
- Setter method:
clearFeatures()
Clear the map of features.- Setter method:
setFeature(String name, boolean value)
Set one feature.parameters:name
– The feature's name.value
– Is the feature enabled?- Setter method:
setFeatures(Map<String, Boolean> m)
Set a map of features.parameters:m
– The map of features.
logFooter | top |
The message that is logged to info
level
after the task has been successfully run.
- Setter method:
setLogFooter(String s)
parameters:s
– The footer message.- Default value:
- Empty (no footer message is logged.)
- See also:
- logHeader
logHeader | top |
The message that is logged to info
level
before the task is run.
- Setter method:
setLogHeader(String s)
parameters:s
– The header message.- Default value:
- A task class specific message.
- See also:
- logFooter
outputProperties | top |
Properties that are passed on to the XSL stylesheet.
- Setter method:
addOutputProperties(Properties p)
Add several output properties (perhaps read from a property file).parameters:p
– The properties.- Setter method:
addOutputProperty(String key, String value)
Add one output property.parameters:key
– The property name.value
– The property value.- Setter method:
clearOutputProperties()
Clear the XSL stylesheet properties.
overwriteStrategy | top |
The overwrite strategy decides how the task will react if there already is an entity (file or directory) in a location where it wants to create a new entity.
If the strategy is to not overwrite existing entities, the task will fail when it cannot create the entities that it wants to create.
Non-empty directories are never overwritten, regardless of the chosen strategy.
- Setter method:
setOverwrite(boolean b)
Setting this to a value ofparameters:true
means that the DoOverwriteAndLogWarning strategy is used. A value of false gives the DontOverwriteAndThrowException strategy.b
– Should an existing entity be overwritten?- Setter method:
setOverwriteStrategy(OverwriteStrategy strat)
Set the overwrite strategy.parameters:strat
– The overwrite strategy.- Default value:
- DontOverwriteAndThrowException
- See also:
- target
parameters | top |
A map with XSL transformer parameter names and values. The parameters are passed on to the Transformer instance used for transformation.
- Setter method:
setParameter(String name, Object value)
Set one parameter.parameters:name
– The parameter name.value
– The parameter value.- Setter method:
setParameters(Map<String, ?> m)
Set several parameters at once.parameters:m
– A parameter map.
reportLevel | top |
This property is used to change the Report level for all task created by this task factory. The report level is changed for the thread running the task when the it is run, and is restored to its previous level when the it is done.
- Setter method:
setReportLevel(Level l)
Set the report levelparameters:l
– The new report level.
source (required) | top |
The XML document to transform.
- Setter method:
setSource(Object o)
parameters:
stylesheet (semi-required) | top |
The XSL stylesheet.
- Setter method:
setStylesheet(Object o)
parameters:- See also:
- templates
target (required) | top |
The location of the generated target file.
- Setter method:
setTarget(Object o)
parameters:
templates (semi-required) | top |
This can be set to reuse a Templates instance between tasks. Templates can be compiled with the TemplateCompilerTF.
- Setter method:
setTemplates(Object o)
parameters:- See also:
- stylesheet
traceLogging | top |
If trace logging is enabled for a task, it reports its configuration before it is run.
Trace logging may also be enabled globally for all tasks by calling TraceMode.setTraceMode(boolean).
- Setter method:
setTraceLogging(boolean b)
Enable or disable trace logging.parameters:b
– Enable trace logging?
transformerFactoryClassName | top |
The fully qualified name of the
TransformerFactory
to use for creating the
Transformer
for the XSL
transformation.
- Setter method:
setTransformerFactoryClassName(String s)
parameters:- Default value:
- The current default transformer factory. This is often Java's own implementation, but it may also be an implementation that have been selected by a service definition in a Jar on the classpath. (Schmant's classpath and Jar files from all enabled task packages.)
xmlUriResolver | top |
A URIResolver that is used to resolve external entities in the XML document to transform. This can for instance be a XmlCatalogResolver.
- Setter method:
setUriResolver(URIResolver r)
Set both the XML and the XSL URI resolver.parameters:r
– The URI resolver- Setter method:
setXmlUriResolver(URIResolver r)
Set only the XML URI resolver.parameters:r
– The URI resolver- See also:
- xmlUriResolverDecorator
- xslUriResolver
xmlUriResolverDecorator | top |
An optional UriResolverDecorator that the XSL transformer uses to resolve external entities for the XML document to transform.
- Setter method:
setUriResolverDecorator(UriResolverDecorator d)
This sets both the xmlUriResolverDecorator and the xslUriResolverDecorator properties.parameters:d
– The URI resolver decorator.- Setter method:
setXmlUriResolverDecorator(UriResolverDecorator d)
This sets only the xmlUriResolverDecorator.parameters:d
– The URI resolver decorator.- See also:
- xslUriResolverDecorator
- xmlUriResolver
xslUriResolver | top |
A URIResolver
that is
used to resolve external entities in the stylesheet. This can for instance be a
XmlCatalogResolver.
- Setter method:
setUriResolver(URIResolver r)
Set both the XML and the XSL URI resolver.parameters:r
– The URI resolver- Setter method:
setXslUriResolver(URIResolver r)
Set only the stylesheet URI resolver.parameters:r
– The URI resolver- See also:
- xslUriResolverDecorator
- xmlUriResolver
xslUriResolverDecorator | top |
An optional UriResolverDecorator that the XSL compiler uses to resolve external entities for the stylesheet.
- Setter method:
setUriResolverDecorator(UriResolverDecorator d)
This sets both the xslUriResolverDecorator and the xmlUriResolverDecorator properties.parameters:d
– The URI resolver decorator.- Setter method:
setXslUriResolverDecorator(UriResolverDecorator d)
This sets only the xslUriResolverDecorator.parameters:d
– The URI resolver decorator.- See also:
- xmlUriResolverDecorator
- xslUriResolver
Examples
Example 1
Run an XSL transformation of the XML document in the file
doc
using the stylesheet in the file style
. Put the
result in the file result.xml
in directory d
.
JavaScript
Example 2
Transform all XML files in the directory hierarchy under
doc_src
using the stylesheet in the file style
. Put
the results in the directory hierarchy under doc
.
JavaScript
Example 3
Transform all XML files in the directory hierarchy under
doc_src
using the stylesheet in the file style
. Put
the results in the directory hierarchy under doc
. Use a
TaskExecutor for running the tasks.
JavaScript
Example 4
This example does pretty much the same as the two previous
examples, but it uses the XmlCatalogResolver
cr
to resolve external entities in an XML catalog. In this example,
the source document parsing and XSL transformation is split into two separate
steps. That is sometimes necessary in order to persuade the various XML processors to
resolve entities the way we want. An Iterator is used to iterate
over all XML source documents manually. This is perhaps the easiest way to
handle dependencies between different tasks in a loop.
JavaScript
Example 5
Build a Docbook multi-page HTML manual from the XML file
manual
. Put the manual in doc
. The docbook stylesheets
are in /usr/share/xml/docbook
. tmpDir
is a directory
for temporary files. te
is a TaskExecutor
Note: The next example after this example is the recommended way of accomplishing the same thing. At the time of writing (2007.04), both Saxon and Xalan occasionally stumble on what looks as a threading issue in Xerces DOM classes when running several parallel XSLT tasks that use a DOMSource for the XSL template.
Note 2: The combination Xalan + Docbook XSL stylesheets is not thread safe. Be sure to never run several transformation tasks concurrently in parallel threads.
JavaScript
Example 6
This example does the same Docbook transformation as the previous example. The difference is that this example compiles the stylesheet using the TemplateCompilerTF task before doing the transformation, whereas the previous example only parsed it. This is the recommended way of running an XSL transformation.
Note: The combination Xalan + Docbook XSL stylesheets is not thread safe. Be sure to never run several transformation tasks concurrently in parallel threads.
JavaScript
JRuby
Jython
* An EntityFS-aware task is implemented using EntityFS. This means that it uses the filter settings of DirectoryView:s and also that it often can work with other file system implementations than File-based, such as the RAM file system.