Obviously, an XSLT stylesheet is only useful if you can apply it to some XML. Apart from executing XML in an XML editor or IDE, you have at least three ways to apply XSLT to XML:
q Using Command-line tools
q Via code
q In a browser
If you need to generate a new document based on a source document and standalone XSLT file, a command-line tool may be useful. The two most notable are Open Source Saxon tool and Microsoft's msxsl.exe.
Saxon is an Open Source tool, originally written by Michael Kay. It supports command-line usage and code. It is available for Java and .NET development in two flavors. Saxon-B provides basic conformance support for XSLT 2.0 and XQuery, whereas Saxon-SA provides Schema-aware support and is a commercial package. You execute XSLT using Saxon using the following command-line (Java version shown):
java -jar saxon8.jar [options] [Source document] [Stylesheet document] [parameters]
The resulting document is displayed in the console. You can send the output to a file either using the –o filename option or the output pipe (>). The following line shows using the Saxon processor to execute the sample shown in Listing 8-1 on the customers.xml file.
java -jar \Tools\saxon\saxon8.jar customers.xml language\sample.xslt
The msxsl.exe tool from Microsoft is similar to Saxon. It requires that the MSXML DLLs are also available (at least version 3.0). The command-line parameters and use are also similar:
MSXSL source stylesheet [options] [param=value...] [xmlns:prefix=uri...]
For example, calling the sample in Listing 8-1 is done with the following:
msxsl customers.xml language\sample.xslt –o result.xml
Note | Although the MSXSL tool can be used with most XSLT scripts, it cannot process scripts with embedded .NET code, as with Listing 8-13. |
Both Java and .NET provide built-in functionality for performing XSLT transformations. Other languages also provide this functionality as part of their class libraries. These functions load both source documents and stylesheets and generate the result document. Listing 8-14 shows transforming XML using Java, whereas Listing 8-15 shows using Visual Basic and the .NET Framework to transform XML. Both samples put the resulting XML into a text box for display (see Figure 8-1), but it could also be output to a document or loaded into an XML-handling class.
Figure 8-1
Listing 8-14: Using XSLT from Jav a
![]() |
import java.io.*; import javax.swing.*; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerException; import javax.xml.transform.stream.StreamSource; import javax.xml.transform.stream.StreamResult; private void transformButtonActionPerformed(java.awt.event.ActionEvent evt) { if(this.sourceField.getText().length() > 0 && this.stylesheetField.getText().length() >0) { try { File source = new File(this.sourceField.getText()); File stylesheet = new File(this.stylesheetField.getText()); StreamSource sourceSource = new StreamSource(source); StreamSource styleSource = new StreamSource(stylesheet); Transformer transformer = TransformerFactory.newInstance().newTransformer(styleSource); StringWriter w = new StringWriter(); StreamResult result = new StreamResult(w); transformer.transform(sourceSource, result); this.resultField.setText(w.getBuffer().toString()); } catch (TransformerException te) { System.out.println ("\n** Transformer error"); System.out.println(" " + te.getMessage()); } catch(Exception e) { System.out.println ("\n** General error"); System.out.println(" " + e.getMessage()); } } } private void stylesheetButtonActionPerformed(java.awt.event.ActionEvent evt) { int ret = fc.showOpenDialog(this); if(JFileChooser.APPROVE_OPTION == ret) { this.stylesheetField.setText(fc.getSelectedFile().getPath()); } } private void sourceButtonActionPerformed(java.awt.event.ActionEvent evt) { int ret = fc.showOpenDialog(this); if(JFileChooser.APPROVE_OPTION == ret) { this.sourceField.setText(fc.getSelectedFile().getPath()); } }
![]() |
The XSLT functionality is available in the javax.xml.transform package. The main class is the Transformer, created by the TransformerFactory. This class takes a number of sources, via a DOM, SAX, or a stream. Similarly, it can produce results as a DOM, SAX, or stream.
In this sample, the source and stylesheet files are loaded into StreamSource objects. This object can load from a file, stream, or reader to prepare the text for transformation. The source document is transformed, and the result displayed in the resultField JTextArea.
Listing 8-15: Using XSLT from Visual Basic and the .NET Framework
![]() |
Imports System.IO Imports System.Text Imports System.Xml Imports System.Xml.Xsl Public Class MainForm Private Sub SourceXmlButton_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles SourceXmlButton.Click With FileOpenDialog .Filter = "XML Files|*.xml|All Files|*.*" If .ShowDialog = Windows.Forms.DialogResult.OK Then Me.SourceXmlField.Text = .FileName End If End With End Sub Private Sub StylesheetButton_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles StylesheetButton.Click With FileOpenDialog .Filter = "XSLT Files|*.xsl;*.xslt|All Files|*.*" If .ShowDialog = Windows.Forms.DialogResult.OK Then Me.StylesheetField.Text = .FileName End If End With End Sub Private Sub TransformButton_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles TransformButton.Click If File.Exists(Me.SourceXmlField.Text) AndAlso _ File.Exists(Me.StylesheetField.Text) Then Dim trans As New XslCompiledTransform Dim out As New StringBuilder Using w As XmlWriter = XmlWriter.Create(out) With trans .Load(Me.StylesheetField.Text) .Transform(Me.SourceXmlField.Text, w) End With End Using Me.ResultField.Text = out.ToString End If End Sub End Class
![]() |
The .NET Framework 2.0 comes with the XslCompiledTransform class for performing XSLT transformations. There is also the earlier XslTransform class, although this is deprecated. The XslCompiledTransform class can load the XSLT stylesheet and source document from a stream, XmlReader, a variable that implements IXPathNavigable, or a file. Similarly, the result of the transformation can be passed to an XmlWriter, file, or Stream. You can also provide arguments to the transformation if necessary.
Most modern browsers also support using XSLT when displaying XML documents. This means that you can send XML to the browser and have the client render it into HTML based on your stylesheet. The stylesheet may make the XML more presentable, add more information, or both. For example, Figure 8-2 shows XML being displayed in Internet Explorer. The XML is colorized to identify elements, attributes, and text. Although this is useful when testing XML produced in code, it is less than useful for most viewers.
Figure 8-2
You add an XSLT stylesheet to an XML document with the xml-stylesheet processing instruction.
<?xml-stylesheet type='text/xsl' href='URL to xslt' version='1.0'?>
With the addition of the stylesheet, the preceding XML becomes more useful (see Figure 8-3).
Figure 8-3
The advantage to adding the stylesheet information to the XML is two-fold. First, it means that global changes to multiple XML files are made in only one place-the XSLT file. Second, it pushes the processing required to transform and display the XML to the client. This means that less server-side processing is required.