The .NET Framework XSLT Processor in VB.NET

Creation Code 39 Extended in VB.NET The .NET Framework XSLT Processor

The .NET Framework XSLT Processor
Printing Code 3 Of 9 In VB.NET
Using Barcode generator for VS .NET Control to generate, create Code39 image in VS .NET applications.
www.OnBarcode.com
Scan USS Code 39 In VB.NET
Using Barcode recognizer for .NET framework Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
In the .NET Framework, the core class for XSLT is XslTransform. Located in the System.Xml.Xsl namespace, the XslTransform class implements the XSLT processor. You make use of this class in two steps: first you load the style sheet in the processor, and then you apply transformations to as many source documents as you need. The XslTransform class supports only the XSLT 1.0 specification. A style sheet declares itself compliant with this version of the specification by including the following namespace: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
Encoding Bar Code In Visual Basic .NET
Using Barcode generator for Visual Studio .NET Control to generate, create bar code image in .NET applications.
www.OnBarcode.com
Decode Barcode In Visual Basic .NET
Using Barcode decoder for VS .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
By the way, note that the version attribute is mandatory to ensure the correctness of the style sheet document. The key methods in the XslTransform class are Load and Transform. They perform the two steps just mentioned. In particular, you use the Load method to read the style sheet from a variety of sources. The Transform method, on the other hand, applies the transformation rules set in the style sheet to a given XML source document. A Quick XSLT Transformer Earlier in the chapter, we used XSLT scripts to transform an XML source document into something else say, an HTML page or another XML schema. The scripts were tested simply by adding a processing instruction to the XML source document. Such an instruction tells specialized browsers, like Internet Explorer 5 and later, to use the referenced XSLT script to transform the XML document before displaying it. A .NET Framework application can programmatically control the entire transformation process using the XslTransform class. The following console application represents a quick command-line XSLT transformer. It takes three arguments (the XML source, the XSLT style sheet, and the output file), sets up the processor, and saves the results of the transformation to the output file. using System; using System.Xml; using System.Xml.Xsl; class QuickXslTransformer { public QuickXslTransformer(string source, string stylesheet, string output) { XslTransform xslt = new XslTransform(); xslt.Load(stylesheet); xslt.Transform(source, output); } public static void Main(string[] args) { try { QuickXslTransformer o; args[2]); } catch (Exception e) { Console.WriteLine( "Unable to apply the XSLT transformation."); Console.WriteLine("Error:\t{0}", e.Message); 250 o = new QuickXslTransformer(args[0], args[1],
Encode Code-39 In C#.NET
Using Barcode creation for .NET Control to generate, create Code 39 Full ASCII image in VS .NET applications.
www.OnBarcode.com
Drawing ANSI/AIM Code 39 In VS .NET
Using Barcode generation for ASP.NET Control to generate, create Code 3/9 image in ASP.NET applications.
www.OnBarcode.com
Console.WriteLine("Exception: e.GetType().ToString()); } return; } }
Print Code 3/9 In .NET Framework
Using Barcode encoder for Visual Studio .NET Control to generate, create USS Code 39 image in VS .NET applications.
www.OnBarcode.com
EAN13 Printer In VB.NET
Using Barcode generation for Visual Studio .NET Control to generate, create GTIN - 13 image in VS .NET applications.
www.OnBarcode.com
{0}",
Bar Code Printer In VB.NET
Using Barcode encoder for .NET framework Control to generate, create barcode image in .NET applications.
www.OnBarcode.com
1D Barcode Creation In VB.NET
Using Barcode generator for VS .NET Control to generate, create Linear 1D Barcode image in .NET applications.
www.OnBarcode.com
The heart of the application is found in the following three lines of rather selfexplanatory code: XslTransform xslt = new XslTransform(); xslt.Load(stylesheet); xslt.Transform(source, output); The style sheet can be loaded from a variety of sources, including XPath documents, XML readers, local disk files, and URLs. The Load method compiles the style sheet and uses the stored information to initialize the XSLT processor. When Load returns, the processor is ready to perform any requested transformation. The Transform method loads an XML document, runs the XSLT script, and writes the results to the specified stream. Transform is particularly handy, because it saves you from explicitly loading the source document and creating the output file. As we'll see more in detail in the section "Performing Transformations," on page 314, Transform uses an intermediate XPath document to transform the XML. Note Several other programming environments allow you to exercise total control over the XSLT process. In particular, in Microsoft Win32, the combined use of two distinct instances of the Microsoft.XMLDOM COM object lets you programmatically perform an XSLT transformation. The following JScript code illustrates how to proceed: // Collects arguments from the WSH command line source = WScript.Arguments(0); stylesheet = WScript.Arguments(1); output = WScript.Arguments(2); // Instantiates the XMLDOM for the source xml = new ActiveXObject("Microsoft.XMLDOM"); xml.load(source); // Instantiates the XMLDOM for the style sheet xsl = new ActiveXObject("Microsoft.XMLDOM"); xsl.load(stylesheet); // Creates the output fso = new ActiveXObject("Scripting.FileSystemObject"); f = fso.CreateTextFile(output); f.Write(xml.transformNode(xsl.documentElement)); f.Close();
Encode Data Matrix 2d Barcode In Visual Basic .NET
Using Barcode drawer for .NET framework Control to generate, create ECC200 image in .NET applications.
www.OnBarcode.com
European Article Number 8 Generator In VB.NET
Using Barcode encoder for VS .NET Control to generate, create EAN8 image in .NET framework applications.
www.OnBarcode.com
The XslTransform Class Now that we've seen how the XslTransform class implements the .NET Framework processor to transform XML data into arbitrary text using XSL style sheets, let's look more closely at its programming interface. As shown in the following code, XslTransform has only the default constructor. In addition, it is a sealed class, meaning that you can use it only as is and other classes can't inherit from it. public sealed class XslTransform { } The programming interface of the class is fairly simple and consists of just one public property and a couple of methods. Properties of the XslTransform Class The only property that the XslTransform class exposes is XmlResolver, which handles an instance of the XmlResolver class. Interestingly, the XmlResolver property is writeonly that is, you can set it, but you can't check the currently set resolver object. As we've seen in previous chapters, the XmlResolver object is used to resolve external references found in the documents being processed. In this context, the XmlResolver property is used only during the transformation process. It is not used, for example, to resolve external resources during load operations. If you don't create a custom resolver object, an instance of the XmlUrlResolver class is used. Methods of the XslTransform Class TheXslTransform class supplies two methods specific to its activity the Load and Transform methods mentioned earlier. The Load and Transform methods are described in more detail in Table 7-5. Table 7-5: Methods of the XSLT Processor Method Description Loads the specified XSLT style sheet document from a number Load of possible sources, including remote URLs and XML readers. The method has several overloads, including overloads that let you specify a custom XmlResolver object to load any style sheets referenced through xsl:import and xsl:include statements. Transform Transforms the specified XML data using the loaded XSLT style sheet and writes the results to a given stream. Some of the method's overloads let you specify an argument list as input to the transformation. The following code snippet shows how to use an XmlResolver object with credentials to access a remote XSLT style sheet: XmlUrlResolver resolver = new XmlUrlResolver(); NetworkCredential domain); cred = new NetworkCredential(uid, pswd,
Decoding GTIN - 128 In Visual C#
Using Barcode recognizer for .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
UPC Symbol Creation In Java
Using Barcode printer for Java Control to generate, create GS1 - 12 image in Java applications.
www.OnBarcode.com
resolver.Credentials = cred; XslTransform xslt = new XslTransform(); 252
DataMatrix Drawer In None
Using Barcode maker for Office Excel Control to generate, create Data Matrix 2d barcode image in Microsoft Excel applications.
www.OnBarcode.com
Create Denso QR Bar Code In None
Using Barcode maker for Excel Control to generate, create QR Code image in Microsoft Excel applications.
www.OnBarcode.com
xslt.Load(stylesheet, resolver); The XslTransform class is also unique from the threading and security standpoints. Let's see why. Threading Considerations XslTransform is guaranteed to operate in a thread-safe way only during transform operations. In other words, although an instance of the class can be shared by multiple threads, only the Transformmethod can be called safely from multiple threads. For the sake of your code, you must ensure that both of the following conditions are met: The Load method is not concurrently called from within different threads. No other method (for example, Transform) is called on the object during load operations. In a nutshell, the XslTransform class is multithreaded only with respect to transformations. The reasons for this behavior stem from the internal architecture of the class, which is summarized in Figure 7-4.
Generating PDF-417 2d Barcode In Java
Using Barcode drawer for Android Control to generate, create PDF 417 image in Android applications.
www.OnBarcode.com
Paint Denso QR Bar Code In None
Using Barcode drawer for Software Control to generate, create Quick Response Code image in Software applications.
www.OnBarcode.com
Figure 7-4: The Load method is not thread-safe, and its state can be overwritten and spoiled by concurrent calls. The Transform method, on the other hand, reads the shared state and can run concurrently from multiple threads. When the Load method is called, the style sheet is compiled and its contents are used to set the internal state of the object. For performance reasons, this code is not grouped into a critical section, which would serialize the threads' access to the internal state. After loading the style sheet, the XSLT processor needs to modify its state to reflect the loaded document. The operation does not occur atomically within the virtual boundaries created by a lock statement. As a result, concurrently running threads could in theory access the same instance of the processor and break the data consistency. The load operation is thread-sensitive because it alters the global state of the object. The transform operation, on the other hand, is inherently thread-safe because it performs read-only access to the processor's state. Nothing bad can happen if concurrent threads apply transformations using the same processor.
Make Code 128A In Java
Using Barcode maker for Android Control to generate, create Code 128 image in Android applications.
www.OnBarcode.com
Code-128 Recognizer In None
Using Barcode scanner for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
To avoid threading risks, be aware that loading a style sheet is an unprotected operation. Either lock the operation yourself, or avoid spawning concurrent threads that perform style sheet loading on the same processor. Security Considerations The XslTransform class has a link demand permission set attached. A link demand specifies which permissions direct callers must have to run the code, as shown in the following example. Callers' rights are checked during just-in-time compilation. [PermissionSet(SecurityAction.LinkDemand, Name="FullTrust")] public sealed class XslTransform { } The permission set attribute for the XslTransform class is expressed by name and points to one of the built-in permission sets FullTrust. What does this mean to you Only callers (direct callers are involved with the check, not caller's callers) with fully trusted access to all the local resources can safely call into the XSLT processor. Try running the XSLT Quick Security Tester sample application over a network. Because of the class security settings, a security exception is thrown. Figure 7-5 shows the security exception dialog box.
Figure 7-5: The XSLT processor class works only if called by locally trusted callers. An XSLT application can work well as long as you invoke it locally, but it will raise a security exception if you run it over a network share. Under the Hood of the XSLT Processor In the overall behavior of the .NET Framework XSLT processor, three phases can be clearly identified: loading the style sheet document, setting up the internal state, and performing the transformations. Although you see, and interact with, only a single class (XslTransform), a lot of internal classes are involved in the process. The first two phases occur within the context of the Load method. Of course, you can't call the Transform method before a previous call to Load has successfully terminated. If you do, you will experience an XsltException exception on the Transform method. 254
Load always works synchronously, so when it returns, you can be sure that the loading step has been completed. You will not get from Load any return value that denotes the failure or the success of the operation. When something goes wrong with the Load method, however, some exceptions are thrown. In particular, you will get a FileNotFoundException exception if you are pointing to a missing style sheet, and you will get a more generic XsltCompileException exception if the XSLT script contains errors. An XsltCompileException exception provides you with a line position and number indicating where the error occurred in the style sheet. Loading the Style Sheet The input style sheet can be loaded from four sources: a URL, an XML reader, an XPath document, or an XPath navigator. Whatever the source, the Load method first expresses it as an XPath navigator. As discussed in 6, an XPath navigator represents a generic interface able to navigate over any XMLbased, or XML-looking, data store. The XPathNavigator class enables you to move from one node to the next and to retrieve node-sets using XPath queries. The source style sheet is normalized to an XPath navigator mostly for performance reasons. The style sheet must be compiled and, given the compiler's architecture, a navigator is an extremely efficient object for performing the task. Compiling is a process that simply excerpts information from the original style sheet and stores it in handy data structures for further use. The entire set of these data structures is said to be the state of the XSLT processor. Figure 7-6 illustrates the flow of the Load method.
Figure 7-6: The style sheet is first normalized to an XPath navigator and then compiled. Managing the Processor's State The style sheet compiler populates three internal data structures with the data read from the source. The compiled style sheet object shown in Figure 7-6 represents an 255
index of the style sheet contents. The other two data structures are tables containing compiled versions of the XPath queries to execute and the actions that the various templates require. As mentioned, the state of the XSLT processor is not set atomically, which might pose problems if you are using the XSLT processor from within a multi-threaded application. Once set by the Load method, the processor's state is not modified until the same Load method is called again. Performing Transformations The transformation method, depicted in Figure 7-7, takes at least two explicit arguments the source XML document and the output stream plus a couple of implicit parameters. The compiled style sheet object is of course one of the implicit input arguments. The second implicit parameter is the XmlResolver property. As mentioned, the XmlResolver property is designed to help the processor resolve external resources.
Figure 7-7: The XSLT processor generates the output text based on the source XML document and the internally stored information about the style sheet. The Transform method can also take a third explicit argument an object of class XsltArgumentList. The argument contains the namespace-qualified arguments used as input to the transformation process. (More on this in the section "Creating a .NET Framework Argument List," on page 324.) The XML source document is normalized as an XPath navigator and passed down in this form to the XSLT processor. Interestingly, the internal processor class has two types of overloads. Some of the overloads work as void methods and simply write to the specified stream. Others work as functions and specifically return an XML reader object. As you'll see in a moment, this feature provides an interesting opportunity: implementing asynchronous XSLT transformations. Note How easy is it to normalize XML readers, URLs, and documents to 256
XPath navigators Remember that you can always create an XPathDocument object from any XML file or reader. Once you have a reference to an XPathDocument object, or an instance of any other object that implements the IXPathNavigableinterface, you simply call the CreateNavigator method and you're done. The CreateNavigator method, of course, is part of the IXPathNavigable interface.
Copyright © OnBarcode.com . All rights reserved.