XSLT transformation technology is an important technology in XML, this article will introduce XML developers to some different XSLT transformation techniques under the. NET Framework. At the same time, this article will also describe how to use various input data sources to complete an XSLT transformation. Under the. NET Framework, the System.Xml.Xsl.XslTransform class can transform an XML document based on an XSLT stylesheet file, which is the most important class in the XSLT transformation, and it can support the syntax of the XSLT 1.0 of the Consortium. Its use of the namespace is http://www.w3.org/1999/XSL/Transform.
Two Input data sources related to XSLT transformations:
. NET Framework, there are many classes that can complete the ability to read XML documents for XSLT transformations, the most useful of which is the System.Xml.XmlReader class. It is a virtual base class, so it cannot be used directly, and a class must inherit it. NET Framework, there are three classes that inherit from this class: The XmlTextReader class, the XmlNodeReader class, and the XmlValidatingReader class, which are all contained within the namespace System.Xml. Where the XmlTextReader class can read a character stream from an XML document and check whether the document is well-formed (well-formed), it does not use a DTD or XML schema to validate XML documents. The XmlNodeReader class allows data to be read from any XML Document Object Model (DOM) API, such as a System.Xml.XmlNode object, and the XmlNode object is not necessarily the root node of a complete XML document, it can be a child node. The XmlValidatingReader class ensures that an XML document conforms to the rules defined by a DTD or XML schema. The following is an application instance of an XmlReader class as an input data source for an XSLT transformation.
Import a string of an XSLT file into a TextReader object
System.IO.TextReader tr = new System.IO.StreamReader ("numbers.xsl");
Use the TextReader object above as the data source for the XmlTextReader object
System.Xml.XmlReader XR = new System.Xml.XmlTextReader (TR);
Create a new XslTransform object
System.Xml.Xsl.XslTransform trans = new System.Xml.Xsl.XslTransform ();
Import the style sheet from the XmlReader object into the XslTransform object above
Trans. Load (XR);
Another class that can finish reading an XML document to implement the XSLT transformation functionality is the System.Xml.XPath.XPathNavigator class or any class that implements the System.Xml.XPath.IXPathNavigable interface, which includes the system.xml.x Path.xpathdocument class, System.Xml.XmlDocument class, and System.Xml.XmlDataDocument class. The System.Xml.XPath.XPathNavigator class is based on the XPath data model and provides a way to make XPath queries on any Xml data.
The System.Xml.XPath.XPathDocument class is the fastest class in these classes because it is read-only and should be used when the XSLT transformation requires a high speed. The efficiency of the System.Xml.XmlDocument class is second only to the System.Xml.XPath.XPathDocument class, and the System.Xml.XmlDataDocumnet class is not suitable for XSLT transformations because it is the least efficient. The System.Xml.XPath.IXPathNavigable interface can be implemented on any data source, and it allows any type of data to be used as the data source for XSLT transformations. The following example code can be followed by the example above.
Create a new XPathDocument object and import the data source from an XML file
The XslTransform class requires that the XML or XSLT data source be a XmlReader object or a XPathNavigator object. Its two most important methods are the load () method and the transform () method, which does not provide an overloaded method of handling the stream object directly, but can indirectly use the stream object as the data source for the XSLT transformation. The stream object is obtained from an XML file or an XSLT style sheet. The following code creates a Stream object and shows how to use it as the data source for the XSLT transformation, but the code snippet is just an example and there is no actual data.
Create a new Stream object
System.IO.Stream st = new System.IO.MemoryStream ();
This uses an XML file to populate the above stream object and place the stream object in 0
St. Position = 0;
Import a Stream object into a XPathDocument object
System.Xml.XPath.XPathDocument XP = new System.Xml.XPath.XPathDocument (ST);
Doing the same thing with an XSLT document
System.IO.Stream Xsltstream = new System.IO.MemoryStream ();
This uses an XSLT file to populate the above stream object and place the stream object in 0
St. Position = 0;
Creates a XmlReader object and takes the stream object above as the data source
System.Xml.XmlReader XSLTXR = new System.Xml.XmlTextReader (xsltstream);
Later, the XmlReader object can be passed to the load () method of the XslTransform class.
There is no actual data in the code above, and we need to populate the Stream object with an XML file or XSLT style sheet to do the actual operation. Another way to set up an input data source is to get an XML document or XSLT document from a URL in the form of a string, as well as a selectable System.Xml.XmlResolver object, which is the most direct and efficient method.
Three Output data sources associated with the XSLT transformation:
After introducing the input data source of the XSLT transformation, this article then introduces the output data source associated with it. Depending on the different types of output classes used, xsl:output elements can sometimes be ignored. For example, when using the XmlWriter class or the XmlReader class as the output class, the element is ignored. For more information about the xsl:output element, refer to the MSDN article "outputs from a XslTransform", which is not covered in space.
To sum up, the necessary requirements for an XSLT transformation include an XML document, an XSLT document, and a valid object to process the output. The following code uses a XPathDocument object and a stylesheet file for XSLT transformations whose output is directly displayed in the console.
Create a new XslTransform object
System.Xml.Xsl.XslTransform XSLT = new System.Xml.Xsl.XslTransform ();
Import a style sheet from an XSL file
Xslt. Load ("numbers.xsl");
Create a new XPathDocument object and import an XML file
System.Xml.XPath.XPathDocument doc = new
System.Xml.XPath.XPathDocument ("Numbers.xml");
Create a new XmlTextWriter object to complete the data output to the console
System.Xml.XmlTextWriter writer = new
System.Xml.XmlTextWriter (System.Console.Out);
Perform the actual XSLT transformation operation
Xslt. Transform (doc, NULL, writer);
Close the XmlTextWriter object after the operation completes
Also note that once you close the object, you cannot write any additional information to the console.
Writer. Close ();
Four Stream objects associated with the XSLT transformation:
. NET Framework allows programmers to directly output the results of an XSLT transformation to a System.IO.Stream object, the following code uses a System.IO.MemoryStream object for output, and then shows how to use it to perform operations related to the output of the XSLT transformation.
Create a new XslTransform object
System.Xml.Xsl.XslTransform XSLT = new System.Xml.Xsl.XslTransform ();
Import a style sheet from an XSLT file
Xslt. Load ("numbers.xsl");
Import an XML file
System.Xml.XPath.XPathDocument doc = new
System.Xml.XPath.XPathDocument ("Numbers.xml");
Create a Stream object for output
System.IO.Stream str = new System.IO.MemoryStream ();
Perform the actual conversion operation
Xslt. Transform (doc, NULL, str);
Flush the Stream object and place it in 0
Str. Flush ();
Str. Position = 0;
If a System.Xml.XmlDocument object or a System.Xml.XPath.XPathDocument object has been loaded into main memory, they can also be used as input data sources for XSLT transformations. Since both objects implement the System.Xml.XPath.IXPathNavigable interface, they can be used directly as parameters of the transform () method of the XslTransform object.
Create a new XslTransform object
System.Xml.Xsl.XslTransform XSLT = new System.Xml.Xsl.XslTransform ();
Import a style sheet from an XSLT file
Xslt. Load ("numbersxml.xsl");
Import an XML document into a XPathDocument object
System.Xml.XPath.XPathDocument doc = new
System.Xml.XPath.XPathDocument ("Numbers.xml");
Create a Stream object for output
System.IO.Stream str = new System.IO.MemoryStream ();
Flush the XmlWriter object and place the stream object in 0
Xw. Flush ();
Str. Position = 0;
Five Embedded scripts and code related to XSLT transformations:
. NET Framework provides the support for embedding scripting language in the XSLT document with script extension elements. Scripting languages provide a set of features that are richer than the functionality provided by pure XSLT and can often be used to perform more complex operations on document content, such as the use of its applied computing functions, access to external sources of information, and so on. Under the. NET Framework, programmers use the Msxsl:script element to mark embedded scripts or code in an XSLT style sheet. Script extension elements must be declared in the Urn:schemas-microsoft-com:xslt namespace namespace ... Net Framework enables you to support scripts written in code such as C #, vb.net, VBScript, or JScript. In the script element, we define the namespace called by the function by providing a Implements-prefix property, which defines the programming language used by the function by providing a language property. At the same time, we should be aware that a stylesheet can embed scripting code in multiple languages, but only the same language can be used under the same namespace. The following example shows a style sheet that contains embedded C # functions.
XSLT provides a mechanism for using parameters and extending objects in a style sheet. Under the. NET Framework, programmers can pass parameters or extended objects to a style sheet by using the System.Xml.Xsl.XsltArgumentList class implementation. The mechanism for using parameters in a stylesheet allows programmers to declare a global variable that is defined as an element of the xsl:variable type that exists as a xsl:stylesheet child element, but it is not included in the xsl:template. By invoking the AddParam () method of the XsltArgumentList class, the programmer can implement the ability to add parameters, in which three parameters are in turn a valid name, a namespace URI, and a parameter value. If the parameter value is not a string, Boolean, number, node Fragment, or node set, it is coerced into a double-precision floating-point value or a string. An extended object is any. NET class that returns the value of an XSLT data type. By invoking the AddExtensionObject () method of the XsltArgumentList class, programmers can implement the ability to add extended objects, with two parameters being valid names and the URI of the namespace. The following code shows how to use the XsltArgumentList class to work with parameters in a style sheet and to extend the object's methods.
Create a new XPathDocument object and import an XML file
System.Xml.XPath.XPathDocument XP = new
System.Xml.XPath.XPathDocument ("Numbers.xml");
Create a new XslTransform object
System.Xml.Xsl.XslTransform trans = new System.Xml.Xsl.XslTransform ();
Import an XSLT file into the XslTransform object above
Trans. Load ("numbersextension.xsl");
Create a new XsltArgumentList object
System.Xml.Xsl.XsltArgumentList Xslarg = new
System.Xml.Xsl.XsltArgumentList ();
Call the XsltArgumentList object's AddParam () method to add parameters
Xslarg.addparam ("Displayme", "", "Is this fun?");
To create a new extension object
SayHello hi = new SayHello ();
Call the XsltArgumentList object's AddExtensionObject () method to add an extension object
Xslarg.addextensionobject ("Urn:sayhello", HI);
Create a new System.IO.Stream object to process the output
System.IO.Stream str = new System.IO.MemoryStream ();
Perform the actual conversion operation
Trans. Transform (XP, Xslarg, str);
Flush the Stream object and place it in 0
Str. Flush ();
Str. Position = 0;
Creates a new StreamReader object to read the stream and return the string
System.IO.StreamReader sr = new System.IO.StreamReader (str);
String xmlout = Sr. ReadToEnd ();
Close StreamReader Object
Sr. Close ();
Write the results to the console
Console.Write (xmlout);
Extended object returns Hello Name
public class SayHello
{
public string Helloname (string name) {
Return "Hello" + name;
}
}
From the above, we find that there are many similarities between using extended objects and using embedded scripts. Here's a look at some of the pros and cons of using extended objects relative to using embedded scripts.
Portability: Embedded scripts are easier to migrate than extended objects because of any. NET platform can correctly transform a style sheet with embedded scripts. On the other hand, the style sheet that uses the extended object can only be converted with a piece of code, and it needs to be in the. NET platform or some other platform that supports extended objects, and requires that this code correctly implements an extension object with a valid name and namespace URI.
Size: A style sheet that relies on an extended object is smaller than the style sheet that contains the embedded script and is easier to maintain.
Flexibility: As we've seen, stylesheets that rely on extended objects can modify their behavior by modifying the functionality of the extended objects that constrain them. Style sheets that use embedded scripts can always perform actions only according to the features specified in the Embedded scripting language.
Performance: Because the extended object is precompiled rather than just-in-time, the script that uses the extended object is somewhat higher in performance than the embedded script. However, it is also important to consider factors such as how often the style sheet is mounted and the size of the embedded script.
Maintainability: Using an extended object's stylesheet can be difficult to modify and correct, because the real code is separated from the style sheet and the code that provides the extension object implementation. Embedded scripts, albeit a bit slower, have the advantage of putting all the code in place locally.
By considering some of these factors, we can decide whether to use extended objects or embedded scripts in specific cases.
Seven Summarize:
This article introduced to you. NET Framework and XSLT transformations, XSLT transformation technology is an important technology in XML. NET Framework provides robust support for XSLT transformations, which not only fully supports the XSLT specification defined by the consortium, but also has many useful extensions, such as embedding scripts for multiple languages in style sheets, extending the functionality of XSLT using extended objects, and so on. NET application, the ease of use of style sheets. Finally, I hope you can learn through this article. NET Framework for some of the basic techniques of XSLT transformations, and understand the pros and cons of each technology to better apply to actual projects.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.