Florent Georges
XML serializer in XSLT 2.0, using FXSL
Introduction
This is an XML serializer for XSLT 2.0, written in XSLT 2.0, using the FXSL library. So giving any sequence, the serializer will produce a lexical representation of the XML within the sequence. The result can be a text node or a sequence of nodes with HTML formating.
Actually, the serializer was written in an extensible way. With the help of FXSL, the several parts of the serializer can be easily customized, and totaly new serialization formats can be added. Or filtering the input events can be easily done (by creating decorators around the existing functions).
serial.xsl defines the main driver for the
serialization. The driver go through the input sequence and
send events to a receiver each time it encounter an element, an
attribute, or whatever.
serial-text.xsl and serial-html.xsl
define such receivers for respectivey text and HTML.
Take care this is a first draft. The serializer was not very-well tested, and is subject to important changes. It is given in the hope it will be usefull to someone.
The serializer was tested with both Saxon 8 for Java and Gexslt/Gestalt, the two only processors I know that support XSLT 2.0.
Any comment would be greatly appreciated at the email address
drkm-lib-xslt AT lists.sf.net (a mailing-list that
doesn't require to subscribe in order to post).
Download
- serial.tar.gz
- all the files below, as a gzipped TAR archive
- serial.zip
- all the files below, as a ZIP archive
The important files they contain are:
- src/serial.xsl
- the main script, require at least one receiver implementation
- src/serial-text.xsl
- the receiver implementation that serialize to a text node
- src/serial-html.xsl
- the receiver implementation that produces HTML
- test/hello-world.xsl
- a simple stylsheet example
- test/hello-world.xml
- a simple input example
- test/hello-world.html
- a simple output example
These stylesheets depend on the FXSL library. Actually, only
four files are used: f/func-apply.xsl,
f/func-compose.xsl,
f/func-compose-flist.xsl and
f/func-id.xsl. Just use the XML Catalog support
of FXSL the usual way, or see the README file at the top of
the distribution.
Usage
These are just a few words about how to use the text and HTML serialization functions, in the simplest way. Nothing is told about how to customize the serializer or how to create receivers (yet). The sources can be interesting for that (while not correctly documented yet).
serial-text.xsl and serial-html.xsl
can be used as examples of creating new receivers.
The function ser:serialize-to-text() takes any
sequence as its unique parameter. The result is a text node
whose the value is the representation of the input sequence.
The function ser:serialize-to-html() takes any
sequence as its unique parameter. The result is a sequence of
text and element nodes representing the input sequence as HTML.
The element nodes are HTML span elements wrapping
a text node, with a class attribute. The class
depends on the role of the text part in the XML lexical
representation.
The possible values for the class attribute are listed in
misc/xml-serial.css. This can be used as a default
CSS file, to be included and/or modified.
To run the sample, transform test/hello-world.xml
(or any other XML file) with test/hello-world.xsl.
That will produce an HTML page with for different serialization
of the input.