The XsltProcessor Class



The XsltProcessor Class

The first question you might ask about the XsltProcessor class is "Why do we need a second XSLT processor, when there is already the XslTransform class?" This will become more apparent when we look at the Common Query Architecture section later, but for now this class should be considered an introductory prototype whose main aim is to significantly improve XSLT performance without breaking existing applications. You will have noticed that there is a new namespace in the Framework called System.Xml.Query, which provides the XsltProcessor and XQueryProcessor classes—both of which share a common architecture.

In effect XML languages share so much in common that, in reality, only a single XML query engine is needed, with compilers for each different language. This is a similar approach to the .NET languages, where there is a common set of features (the class library) and the choice of language is simply based on user preference. XSLT is just another one of the XML query languages, and the common approach to querying is demonstrated by the XsltProcessor class. The XsltProcessor class provides the ability to compile XSLT stylesheets and execute these against XML documents. However, the map:view extension function cannot be used to perform XSLT over XML views.

Before we look at how we can use the XsltProcessor, this section contains a concise reference to the properties and methods of this class. There are a single constructor, one property, and two methods (each with two overloads). Details are provided in Figure through 8.7.

The Constructor for the XsltProcessor Class

Constructor

Description

XsltProcessor()

Creates a new empty XsltProcessor instance.

The Property of the XsltProcessor Class

Property

Description

XmlCommand

A reference to an XmlCommand instance that represents a compiled stylesheet.

The Methods of the XsltProcessor Class

Method

Description

Compile(stylesheet-uri)

Compiles the specified stylesheet into an XmlCommand. The XsltProcessor then becomes a wrapper for the XmlCommand, which can be continuously executed.

Compile(stylesheet-uri, XmlResolver)

As above, except that the XmlResolver is used to resolve the stylesheet URI and any stylesheets referenced within it by <import> and <include> elements.

Execute (context-document-uri, result-document-uri)

Executes the compiled stylesheet. The context-document-uri is the URI of the XML input document, and the result-document-uri is a file to write the results to.

Execute (context-document-uri, datasources, arglist, results)

As above, except that an XmlResolver provided as the datasources parameter is used to resolve external documents via the document function. The arglist provides a list of arguments to the stylesheet, and the final results parameter is a reference to a Stream or TextWriter that the results will be sent to.

Transforming XML Documents with the XsltProcessor Class

The example in Listing 8.17 uses an inventory store XML document named storedb.xml to generate an HTML page containing inventory information using the XsltProcessor class. First, an XsltProcessor instance is created; then the Compile method is called on the storedb.xsl stylesheet ready to prepare it for execution against an XML document.

Next, the Execute method loads the storedb.xml XML document, executes the stylesheet (which does not need to resolve any external documents via the XmlResolver nor provide any parameters via the XmlQueryArgument List). The resulting HTML is written to the Response.OutputStream as part of this ASP.NET page.

Transforming an XML Document to HTML with an XsltProcessor
Dim xslt As New XsltProcessor()

' compile the XSLT stylesheet
xslt.Compile(strPath & "data\storedb.xsl")

' execute compiled stylesheet against XML document and output result
xslt.Execute("storedb.xml", New XmlUrlResolver(), Nothing, _
                                        Response.OutputStream)

The screenshot in Figure shows the results of the transformation from the example in Listing 8.17.

6. Using the XsltProcessor class to transform an XML document into HTML

graphics/08fig06.gif

Transforming Directly to a File

There is also an Execute overload that allows you to provide an input file and an output file for the transformation, as shown the C# code in Listing 8.18.

Transforming an XML Document Directly to a File
XsltProcessor xslt = new XsltProcessor ();

//compile XSLT stylesheet
xslt.Compile("storedb.xsl");

//execute stylesheet against XML document and output result
xslt.Execute("storedb.xml", "outputdocument.xml" );

As you can see, currently the number of method overloads is limited. This reflects the XsltProcessor class prototype status, along with the fact that there are a number of features that have not been implemented (such as C# and Visual Basic scripting support and the xsl:key function). Despite its prototype status, expect this class to evolve into an extremely high-performance XSLT processor.