July 24, 2007, 1:17 a.m.
posted by pitbull
The XsltProcessor ClassThe 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.
Transforming XML Documents with the XsltProcessor ClassThe 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
Transforming Directly to a FileThere 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. |
- Comment
