XSL Transformations

Java and .NET both provide comparable support for XSLT. An understanding of XSL is necessary to use these features; the W3C Recommendation "Extensible Stylesheet Language (XSL) Version 1.0" contains a full definition of XSL.

The .NET System.Xml.Xsl.XslTransform class encapsulates an XSLT processor providing functionality comparable to that of the javax.xml.Transformer class. The use of both classes follows roughly the same pattern. For the .NET XslTransform class, this can be summarized as follows:

  • Create an XslTransform instance.

  • Load a style sheet into the XslTransform using the Load method.

  • Execute the transformation against an XML source using the Transform method.

Creating a Transformer

Java provides the static javax.xml.transform.TransformerFactory.newTransformer method to create instances of javax.xml.transform.Transformer. The TransformerFactory provides a vendor-neutral mechanism, allowing different Tranformer implementations to be easily plugged into the generic framework.

.NET provides a single concrete XslTranform implementation that is instantiated using a default constructor. The following statement instantiates a new XslTransform:

XslTransform myTransform = new XslTransform();

Loading a Style Sheet

The Java Transformer class is the primary mechanism for writing XML SAX and DOM sources to files. However, if it’s used to perform XSL Transformations, an XSL style sheet must be provided as a parameter to the newTransformer factory method. Once the Tranformer is created with a given style sheet, it can be reused but cannot load a new style sheet.

The .NET XslTransform class provides the Load method to load a style sheet into an existing XslTranform instance. Separating the style sheet loading from the transformer creation allows new style sheets to be loaded into existing XslTransform instances as required.

Overloaded versions of the Load method support the loading of the style sheet from a variety of sources, including instances of IXPathNavigable, URL (contained in a String), XmlReader, and XPathNavigator. For example, the following statements load a style sheet from a file named test.xsl in the assembly directory:

XslTransform myTransformer = new XslTransform();
myTransformer.Load("test.xsl");

Additional overloaded Load methods take a System.Xml.XmlResolver instance as a second argument. The XmlResolver is used to load any style sheets referenced by xsl:import or xsl:include statements contained within the loaded style sheet. If an XmlResolver is not specified, a System.Xml.XmlUrlResolver instance with default credentials is used.

Transforming XML Data

The Java Transformer.transform method takes a javax.xml.transform.Source instance, which provides the XML to be transformed, and a javax.xml.transform.Result instance, which provides a destination for the output of the transformation process. Both Source and Result are interfaces; implementations are provided to support input from and output to DOM, SAX, and stream instances.

.NET provides a series of overloaded Transform methods that take a wide variety of source and destination references. Many of the Transform methods also take an instance of XsltArgumentList that contains arguments that can be used by the style sheet performing the transformation.

The following code fragment demonstrates the simplest Transform overload, which takes the names of an input and an output file:

XslTransform myTransformer = new XslTransform();
myTransformer.Load("test.xsl");
myTransformer.Transform("input.xml", "output.xml");

More commonly, the source would be an XmlReader or XmlDocument passed to Transform as an IXPathNavigable or XPathNavigator reference, and the output would be a stream, a TextWriter, an XmlWriter, or an XmlReader.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.5.86