XSLT Transformation
November 8, 2010 Leave a Comment
http://xml.apache.org/xalan-j/overview.html
Xalan-Java fully implements XSL Transformations (XSLT) Version 1.0 and the XML Path Language (XPath) Version 1.0. XSLT is the first part of the XSL stylesheet language for XML. It includes the XSL Transformation vocabulary and XPath, a language for addressing parts of XML documents. For links to background materials, discussion groups, frequently asked questions, and tutorials on XSLT, see Getting up to speed with XSLT.
Note XSL also includes a vocabulary for formatting documents, which is not part of Xalan-Java. For more information, see Extensible Stylesheet Language (XSL) Version 1.0 and the Apache XML FOP (Formatting Objects Project).
XSL stylesheets are written in the XSLT language. An XSL stylesheet contains instructions for transforming XML documents into XML, HTML, XHTML or plain text. In structural terms, an XSL stylesheet specifies the transformation of one tree of nodes (the XML input) into another tree of nodes (the output or transformation result).
Note The XSL stylesheet may generate and refer to cascading style sheets (CSS) as part of its output.
In the following example, the foo.xsl stylesheet is used to transform foo.xml into foo.out:
foo.xml:
<?xml version=”1.0″?>
<doc>Hello</doc>
foo.xsl:
<?xml version=”1.0″?>
<xsl:stylesheet xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” version=”1.0″>
<xsl:template match=”doc”>
<out><xsl:value-of select=”.”/></out>
</xsl:template>
</xsl:stylesheet>
foo.out:
<out>Hello</out>
By default, Xalan-Java uses Xerces-Java, but it may be configured with system properties to work with other XML parsers (see Plugging in a Transformer and XML parser). The input may be submitted in the form of a stream of XML markup (from a URI, a character or byte stream, or another transformation), a SAX InputStream, or a DOM Node.
Xalan-Java performs the transformations specified in the XSL stylesheet and packages a sequence of SAX events that may be serialized to an output stream or writer, used to build a DOM tree, or forwarded as input to another transformation.
Xalan-Java Features
* Includes an Interpretive processor for use in a tooling and debugging environment and a Compiling processor (XSLTC) for use in a high performance runtime environment.
* Implements the relevant W3C specifications: XSL Transformations (XSLT) Version 1.0 and XML Path Language (XPath) Version 1.0.
* Implements Java API for XML Processing (JAXP) 1.3, and builds on SAX 2 and DOM level 3.
* Implements the XPath API in JAXP 1.3.
* May be configured to work with any XML parser, such as Xerces-Java, that implements JAXP 1.3 (see Plugging in an XML parser).
* Can process Stream, SAX or DOM input, and output to a Stream, SAX or DOM.
* Transformations may be chained (the output of one transformation may be the input for another).
* May be run from the command line for convenient file-to-file transformations.
* Includes an applet wrapper.
* May be used in a servlet to transform XML documents into HTML and serve the results to clients.
* Supports the creation of Java and scripting language extensions. and provides a growing library of extension elements and functions.
org.apache.xalan.xslt.Process provides a basic utility for performing transformations from the command line. You can use this utility, for example, to run several of the extensions samples. The command line for most standard transformations is as follows:
java org.apache.xalan.xslt.Process -in xmlSource
-xsl stylesheet -out outputfile
where xmlSource is the XML source file name, stylesheet is the XSL stylesheet file name, and outputfile is the output file name.
If you want the output to be displayed on the screen, simply omit the -out flag and argument.
You can use this utility to try out XSL stylesheets you have written, to make sure they do what you expect with the XML source files they are designed to transform. The utility provides useful messages if the source file or stylesheet is not well formed.
Debug
http://www.ibm.com/developerworks/xml/library/x-tipxslmsg.html
Echo printing is one of the oldest ways to debug a thorny problem. Nonetheless, it’s still one of the simplest and quickest ways. When you aren’t quite sure why a function doesn’t behave as expected, print a few variables to the console with printf() or its equivalent to see just what’s going on.
Of course, this assumes you have a console to print debugging output on. XSLT doesn’t necessarily have any such thing. However XSLT does have an element corresponding to printf(): xsl:message. The xsl:message element does not change the result tree produced by the XSLT stylesheet at all. It merely outputs a message somewhere the programmer can see it. This is usually the console, but it can be a dialog box or a log file. Wherever the output goes, this is an excellent debugging aid.
The xsl:message element is optional. Processors are not required to support it. However most do, and usually they do so by printing messages on the console.
<xsl:template match="/"> <xsl:message>Matched root node</xsl:message> <xsl:apply-templates select="*"/> </xsl:template>