Introduction to XSLT Conversion Technology under .NET Framework

xiaoxiao2021-03-06  69

XSLT conversion technology in .Net framework introduction Author: Wang Kaiming This article taken from: SEOUL May 8, 2003 a. Preface: XSLT conversion technology is an important technology in XML. This article will introduce some of the different XSLT conversion technologies under the .NET framework to XML developers. At the same time, this paper will also show how to use a variety of different input data sources to complete an XSLT conversion. Under the .NET framework, System.xml.xsl.xsltransform class can convert an XML document according to a XSLT style table file, which is the most important class in XSLT conversion, and it supports the syntax of W3C's XSLT 1.0, which is used The namespace is http://www.w3.org/1999/xsl/transform. Two. Input Data Sources related to XSLT conversion: There are many classes to complete the read XML document to implement XSLT conversion, with the most useful type of System.xml.xmlReader class. It is a virtual base class, so it cannot be applied directly, and there must be a class inherit it. There are three classes in the .NET framework that inherited from this class: XMLTextReader class, XMLNodeReader class, and XMLValidatingReader class, which are included in the namespace system.xml. The XMLTextReader class can read the character stream from an XML document and check if the document has a good structure, but it does not use the DTD or the XML mode to perform the verification of the XML document. The XMLNodeReader class allows data to be read from any XML document object model (DOM) API, such as a System.xml.xmlNode object, and XMLNode objects don't have to be a root node of a complete XML document, which can be one child node. The XMLValidatingReader class ensures that an XML document is complied with a rule determined by a DTD or an XML schema. Below is an application example of an XMLReader class as an input data source for XSLT conversion.

// Import a string of a XSLT file into a TextReader object

System.io.textReader Tr = new system.io.streamReader ("NumBers.xsl");

// Top the TEXTReader object as the data source of 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 table in the XMLReader object into the XSLTransform object above

Trans.Load (XR);

Another class that can complete the read XML document to implement the XSLT conversion function is the system.xml.xpath.xpathnavigator class or any class that implements the system.xml.xpath.ixpathnavigable interface, including System.xml.XPath. The .XPathDocument class, System.xml.xmldocument class, and system.xml.xmldataDocument class. System.xml.xpath.xpathnavigator class is based on XPath data model and provides a method of making XPath queries for any XML data. The System.xml.xPath.XPathDocument class is the fastest class in these classes because it is read-only, and this class should be used when XSLT conversion is high. The efficiency of the System.xml.xmldocument class is second only to the System.xml.xpath.xpathDocument class, and the System.xml.xmldAdocumNet class is not suitable for XSLT conversions because it is the lowest efficiency. The System.xml.xpath.ixpathnavigable interface can be implemented on any data source, while it allows any type of data to be used as a data source for XSLT conversion. The following example code can be attached to the above example. // Create a new XPathDocument object and import data source from an XML file

System.xml.xpath.xpathdocument xp = new

System.xml.xpath.XpathDocument ("Numbers.xml");

// Create a new XPathnavigator object

System.xml.xpath.xpathnavigator xpn = xp.createnavigator ();

The xsltransform class requires XML or XSLT data sources of XMLReader or an XPathnavigator object. Its two most important methods are the LOAD () method and transform () method, which does not provide a method of reloading the STREAM object directly, but can indirectly convert the stream object as XSLT conversion The data source, and the Stream object is obtained from an XML file or a XSLT style sheet. The following code creates a Stream object and shows how to use it as a data source for XSLT conversion, but the code segment is only an example and does not actually exist.

// Create a new Stream object

System.io.stream st = new system.io.MemoryStream ();

/ / This uses an XML file to populate the STREAM object above and set the location of the stream object to 0.

St.Position = 0;

// Import the Stream object into an XPathDocument object

System.xml.xpath.xpathdocument xp = new system.xml.xpath.xpathdocument (ST);

// Do the same operation to the XSLT document

System.io.stream xsltstream = new system.io.MemoryStream ();

/ / Condown to populate the above Stream object with a XSLT file and set the position of the Stream object to 0

St.Position = 0;

// Create an XMLReader object and use the above Stream object as a data source

System.xml.xmlreader xsltxr = new system.xml.xmlTextReader (xsltstream);

// After the XMLReader object can be passed to the LOAD () method of the xsltransform class, there is no actual data in the LOAD () method, we need to populate the XML file or XSLT style table to the Stream object to make actual Operation. Another way to set input data sources is to get an XML document or XSLT document from a URL in the form of a string, and there may be a selectable system.xml.xmlResolver object, which is the most direct and most effective way. .

three. Output data source related to XSLT conversion:

After introducing the input data source for XSLT conversion, this paper then introduces you to the relevant output data source. According to 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 an output class, the element is ignored. For details on the XSL: Output element, please refer to MSDN's article "Outputs from an xsltransform", which is limited to the space is not more introduced.

In summary, the necessary conditions for XSLT conversion include an XML document, an XSLT document, and a valid object that can process the output. The following code uses an XPathDocument object and a style sheet file for XSLT conversion, and the result of its output is displayed directly in the console.

// Create a new XslTransform object

System.xml.xsl.xsltransform xslt = new system.xml.xsl.xsltransform ();

/ / Import style sheet from the 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 is used to complete the data output to the console

System.xml.xmlTextWriter Writer = New

System.xml.xmlTextWriter (System.Console.out);

/ / Performance XSLT conversion operation

XSLT.Transform (DOC, NULL, WRITER);

/ / Close the XMLTextWriter object after completing the operation

/ / Please note that once you turn off the object, you cannot write any additional information in the console.

Writer.close ();

four. Conversion to XSLT conversion-related stream:

The .NET framework allows the programmer to output the results of the XSLT conversion directly to a system.io.Stream object, the following code will use the System.IO.MemoryStream object for output, then display how to use it to convert the output with XSLT conversion Related operations.

// Create a new XslTransform object

System.xml.xsl.xsltransform xslt = new system.xml.xsl.xsltransform ();

/ / Import style sheet from the 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 ();

// actually converted

XSLT.Transform (DOC, NULL, STR); / / For the Stream object, Flush operation, and set it to 0

Str.flush ();

Str.Position = 0;

If a system.xml.xmldocument object or a system.xml.xpath.xpathDocument object has been loaded into the main memory, then they can also be used as input data sources for XSLT conversion. Because these two objects implements the System.xml.xPath.ixPathNavigable interface, they can be used directly as the 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 style sheet from the XSLT file

xslt.load ("numbersxml.xsl);

// Import an XML document into an 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 ();

System.xml.xmlwriter xw = new

System.xml.xmlTextWriter (STR, System.Text.Encoding.utf8);

// actually converted

XSLT.Transform (DOC, NULL, XW);

// For the XMLWRITER object, FLUSH operation and set the position of the Stream object to 0

Xw.flush ();

Str.Position = 0;

Fives. Embedded scripts and code related to XSLT conversion: The XSLT conversion object in the .NET framework provides support for embed scripting language with scripting extensions in the XSLT document. A set of functions provided by the scripting language is rich than the features provided by pure XSLT, which can often be used to make more complex operations for document content, such as the application scientific computing function, access external information source, and the like. Under the .NET framework, programmers use MSXSL: Script elements to mark embedded scripts or code in the XSLT style sheet. The script extension element must be declared in the URN: Schemas-Microsoft-COM: Xslt Namespace namespace. The analyzer in the .NET framework supports scripts written by C #, VB.NET, VBScript, or JScript. In the script element, we define the programming language used by the function to define the function called by providing a logage property by providing a logage attribute. At the same time, we have to pay attention to a script code that can be embedded in a style sheet, but only the same language can be used under the same namespace. The following example shows a style sheet containing embedded C # functions.

XMLns: XSL = 'http://www.w3.org/1999/xsl/transform'

XMLns: msxsl = 'URN: Schemas-Microsoft-COM: xslt'

XMLns: thesecript = 'URN: CUSTOMScript'>

Media-type = 'text / plain' indent = 'no' />

Public String Helloname (String Name)

{

Return "Hello" Name;

}

]]]]

Print Integers> 3

Script Result:

DONE:

NumBers:

Integer:

six. Style table parameters and extended objects:

XSLT provides a mechanism for using parameters and expansion objects in style sheets. Under the .NET framework, the programmer can pass the parameters or extended objects to a style sheet by using System.xml.xsl.xsltargumentList class. The mechanism for using the parameters in the style sheet allows the programmer to declare a global variable, which is defined as an elements of the XSL: Variable type, which is existing as a sub-elements of an XSL: Stylesheet, but it is not included in XSL: Template. By calling the addParam () method programmer of the XSLTargumentListList class, the function of adding parameters can be implemented. The three parameters in this method are valid names, NRIs, and parameter values. If the parameter value is not a string, a Boolean value, a node fragment, a node set, it will be forced to convert to a double precision floating point value or a string. And an extended object is any .NET class that can return the XSLT data type. By calling the addextensionObject () method of the XSLTargumentList () method programmer to implement the function of adding the extended object, the two parameters in this method are valid names and the NRI of the namespace. The following code shows how to use the XSLTargumentList class to use parameters in the style sheet and how to extend objects.

// 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 a XSLT file to 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 addparam () method of the XSLTargumentList object to add parameters

Xslarg.AddParam ("Displayme", "IS this fun?");

// Create a new extension object

Sayhello hi = new sayhello ();

// Call the AddExtensionObject () method of the XSLTargumentList object to add an extension object

Xslarg.addextensionObject ("URN: Sayhello", Hi);

// Create a new System.io.Stream object to process the output result

System.io.stream str = new system.io.MemoryStream ();

// actually converted

Trans.Transform (XP, Xslarg, STR);

/ / Flush operation on the Stream object and set it to 0

Str.flush ();

Str.Position = 0;

// Create a new StreamReader object to read the current and return string

System.io.streamReader Sr = New System.io.StreamReader (STR);

String Xmlout = Sr.ReadToend ();

// Turn off the StreamReader object

sr.close ();

// Write the result into the console

Console.write (XMlout);

/ / Extended object Returns Hello Name

Public Class Sayhello

{

Public String Helloname (String Name) {

Return "Hello" Name;

}

}

By introducing the above, we have found many of the same extensions and using embedded scripts. Below we analyze some advantages and disadvantages that use extended objects with embedded scripts. Portability: The embedded script is easier to transplant than the extended object, as any .NET platform can correctly use the embedded script to convert a style sheet. On the other hand, the style of the extension object can only be converted with a code, and it is necessary to perform under the .NET platform or other platform supporting the extension object, and requires this code to correctly implement a valid The name and namespace URI extension objects. Size: A style sheet depends on the extended object is small than the style sheet containing embedded scripts, and it is easier to maintain. Flexibility: As we see, style sheets that depend on extended objects can modify their behavior according to the function of modifying their extension objects. The style sheet using embedded scripts will always perform operations with the function specified by the embedded scripting language. Performance: Because the extension object is precompiled, instead of instant compilation, the performance of scripts that use extended objects is somewhat than embedded scripts. However, it is necessary to consider the frequency of the load style sheet and the size of the size of the embedded script. Maintainability: Style sheets using extended objects may be more difficult during modification and error correction, because the true code is separated from the style sheet and the code that provides the extension object. Embedded script is slightly slower, but it has the advantages of all code in local. By considering some of the above factors, we can decide whether to use extended objects or embedded scripts in specific cases. Seven. Summary: This article introduces some knowledge related to the .NET framework and XSLT conversion, XSLT conversion technology is an important technology in XML. The .NET framework provides robust support for XSLT conversion, which not only fully supports the W3C defined XSLT specification, but also useful extensions, such as embedding a multilingual script in style table, using extended objects to extend the function of XSLT, etc. Others, thereby enhancing the ease of use of style sheets in the .NET application. Finally, I hope that you can master the basic technology of the XSLT conversion in this article and understand the advantages and disadvantages of each technology to better apply to the actual project. (Editor:

West Gate Blowing)

转载请注明原文地址:https://www.9cbs.com/read-119925.html

New Post(0)