I. Preface:
XSLT conversion technology is an important technology in XML. This article will introduce some different XSLT conversion technologies under the. NET Framework to XML developers. This article also describes how to run
Use various input data sources to complete an XSLT conversion. In the. NET Framework, the system. xml. XSL. Transform transform class can be based on An XSLT style table.
File conversion an XML document, which is the most important class in XSLT conversion. It also supports W3C XSLT
1.0. The namespace used is http://www.w3.org/5o/#/transform.
2. Input data sources related to XSLT conversion:
There are many classes in the. NET Framework that can read XML documents to implement XSLT conversion. The most useful class is the system. xml. xmlreader class. It
Is a virtual base class, so it cannot be used directly. It must be inherited by a class .. NET framework, there are three classes inherited from the class: xmltextreader class,
Xmlnodereader class and xmlvalidatingreader class are included in the namespace system. xml. Where
The xmltextreader class can read the pipeline stream from an XML document and check whether the document is well-structured. However, it does not use DTD or
Verify the XML file in XML mode. The xmlnodereader class allows data to be read from APIs of any XML Document Object Model (DOM). For example
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 complies with rules defined by a DTD or XML mode. The following is an xmlreader class
The application instance of the input data source for XSLT conversion.
// Import the string of An XSLT file to a textreader object
System. Io. textreader TR = new system. Io. streamreader ("numbers. XSL ");
// Use the preceding textreader object as the data source of the xmltextreader object
System. xml. xmlreader xr = new system. xml. xmltextreader (TR );
// Create a new terratransform object
System. xml. XSL. Pipeline transform trans = new system. xml. XSL. Pipeline transform ();
// Import the style sheet in the xmlreader object to the transform object above.
Trans. Load (xr );
Another class that can read XML documents to implement XSLT conversion is the system. xml. XPath. xpathnavigator class or any
System. xml. XPath. ixpathnavigable interface class. These classes include
System. xml. XPath. 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 an XPATH query method for any XML data.
The system. xml. XPath. xpathdocument class is the fastest among these classes, because it is read-only and requires a high speed in XSLT conversion.
This class should be used when it is high. The efficiency of the system. xml. xmldocument class is second only to that of the system. xml. XPath. xpathdocument class.
The system. xml. xmldatadocumnet class is not suitable for XSLT conversion because it has the lowest efficiency.
The system. xml. XPath. ixpathnavigable interface can be implemented on any data source, and it allows any type of data as the data source for XSLT conversion. Lower
ExampleCodeCan be connected to the back of the above example.
// Create a new xpathdocument object and import the 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 that the XML or XSLT data source is an xmlreader object or an xpathnavigator object. It has two
The important methods are the load () method and the transform () method. These two methods do not provide a method to directly process stream objects by overloading them, but they can indirectly
The stream object is used as the data source for XSLT conversion, and the stream object is obtained from an XML file or An XSLT style table. The following code creates a stream object and
Shows how to use it as a data source for XSLT conversion. However, this code segment is just an example and does not have actual data.
// Create a new stream object
System. Io. Stream ST = new system. Io. memorystream ();
// Fill the stream object above with an XML file and set the position of the stream object to 0.
St. Position = 0;
// Import the stream object to an xpathdocument object
System. xml. XPath. xpathdocument XP = new system. xml. XPath. xpathdocument (ST );
// Perform the same operations on the XSLT document
System. Io. Stream Using tstream = new system. Io. memorystream ();
// Use An XSLT file to fill in the above Stream object and set the position of the stream object to 0.
St. Position = 0;
// Create an xmlreader object and use the above Stream object as the data source
System. xml. xmlreader extends txr = new system. xml. xmltextreader (effectstream );
// Later, the xmlreader object can be passed to the load () method of the transform class.
No actual data exists in the above Code. We need to fill the XML file or XSLT style table into the stream object for actual operations. Another option is to set the input data source.
The method is to obtain an XML or XSLT document from a URL in the form of a string. There may also be an optional system. xml. xmlresolver object. This method
Is the most direct and effective method.
3. Output Data sources related to XSLT conversion:
After introducing the input data source for XSLT conversion, this article will introduce the relevant output data source. The XSL: Output element has
Can be ignored. For example, when xmlwriter or xmlreader is used as the output class, this element is ignored. Detailed information about the XSL: Output Element
Refer toArticle"Outputs from an external transform", which is not described here.
To sum up, the necessary conditions for XSLT conversion include an XML document, An XSLT document, and a valid object that can process output. The following code uses an xpathdocument object and a style sheet file for XSLT conversion. The output result is directly displayed on the console.
// Create a new terratransform object
System. xml. XSL. Pipeline transform XSLT = new system. xml. XSL. Pipeline transform ();
// Import the 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 for data output to the console
System. xml. xmltextwriter writer = new
System. xml. xmltextwriter (system. Console. Out );
// Perform the actual XSLT conversion operation
XSLT. Transform (Doc, null, writer );
// Close the xmltextwriter object after the operation is completed
// At the same time, please note that once this object is disabled, no additional information can be written to the console.
Writer. Close ();
4. stream objects related to XSLT conversion:
The. NET framework allowsProgramAttackers can directly output the result of XSLT conversion to a system. io. stream object. The following code uses a system for output. io. the memorystream object then shows how to use it to perform operations related to the output of XSLT conversion.
// Create a new terratransform object
System. xml. XSL. Pipeline transform XSLT = new system. xml. XSL. Pipeline transform ();
// Import the 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 ();
// Perform the actual conversion operation
XSLT. Transform (Doc, null, STR );
// Flush the stream object and set its position to 0
Str. Flush ();
Str. Position = 0;
If a system. xml. xmldocument object or a system. xml. XPath. xpathdocument object has been loaded
If they are in the primary storage, they can also be used as the input data source for XSLT conversion. Because both objects implement
The system. xml. XPath. ixpathnavigable interface can be used as a parameter of the transform () method of the transform object.
Number is directly used.
// Create a new terratransform object
System. xml. XSL. Pipeline transform XSLT = new system. xml. XSL. Pipeline transform ();
// Import the style sheet from the XSLT File
XSLT. Load ("numbersxml. XSL ");
// Import an XML document to 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 );
// Perform the actual conversion operation
XSLT. Transform (Doc, null, XW );
// Flush the xmlwriter object and set the position of the stream object to 0
XW. Flush ();
Str. Position = 0;
5. embedded scripts and Code related to XSLT conversion:
The XSLT conversion object in the. NET Framework provides support for embedding script language with Script extension elements in the XSLT document. A set of functions provided by scripting languages are more powerful than those provided by pure XSLT.
The function is rich and can often be used to perform more complex operations on the document content, such as the application of scientific computing functions and access to external information sources. In the. NET Framework, programmers use msxsl:
The script element can mark embedded scripts or code in the XSLT style sheet. The script extension element must be in urn: Schemas-Microsoft-com: XSLT
Namespace .. The analyzer in the. NET Framework supports scripts written in C #, VB. NET, VBScript, JScript, and other code. In the script Element
By providing an implements-Prefix attribute, we define the namespace called by the function, and define the programming language used by the function by providing a language attribute.
Statement. At the same time, we must note that a style sheet can be embedded with script code in multiple languages, but the same namespace can only use the same language. The following example shows a style that contains embedded C # functions.
Table.
<XSL: stylesheet version = '1. 0'
Xmlns: XSL = 'HTTP: // www.w3.org/5o/#/transform'
Xmlns: msxsl = 'urn: Schemas-Microsoft-com: XSLT'
Xmlns: thescript = 'urn: mmscript'>
<XSL: Output omit-XML-declaration = 'yes' method = 'text'
Media-type = 'text/plain 'indent = 'no'/>
<XSL: variable name = 'displayme'/>
<Msxsl: script implements-Prefix = 'thescript' Language = 'C # '>
<! [CDATA [
Public String helloname (string name)
{
Return "hello" + name;
}
]>
</Msxsl: SCRIPT>
<XSL: template match = '/'>
<XSL: Text disable-output-escaping = 'yes'> Print integers> 3 </XSL: Text>
<XSL: Apply-templates select = 'root/numbers '/>
Script result: <XSL: value-of select = 'thescript: helloname ("Joe") '/>
Done: <XSL: value-of select = '$ displayme'/>
</XSL: Template>
<XSL: template match = 'numbers '>
Numbers: <XSL: Apply-templates select = 'integer [@ value> 3] '/>
</XSL: Template>
<XSL: template match = 'integer'>
Integer: <XSL: value-of select = '@ value'/>
</XSL: Template>
</XSL: stylesheet>
6. style sheet parameters and extension objects:
XSLT provides a mechanism for using parameters and extending objects in style sheets. In the. NET Framework, programmers can use
The system. xml. XSL. Extension targumentlist class implements passing parameters or extended objects to a style sheet. The parameter mechanism used in the style sheet allows programmers to declare
Global variable, which is defined as an element of the XSL: variable type. It exists as a child element of the XSL: stylesheet, but it is not included in the XSL:
Template. By calling the addparam () method of the xsltargumentlist class, the programmer can add parameters. The three parameters in this method are
Valid name, namespace URI, and parameter value. If the parameter value is not a string, Boolean value, number, node fragment, or node set
Set), then it will be forcibly converted to double precision floating point value or string. An extension object is any. Net class that can return XSLT data type values. By calling
The programmer of the addextensionobject () method of the xsltargumentlist class can add extended objects. The two parameters in this method are valid
Name and the URI Of The namespace. The following code shows how to use the parameter targumentlist class in the style sheet and how to extend the object.
// 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 terratransform object
System. xml. XSL. Pipeline transform trans = new system. xml. XSL. Pipeline transform ();
// Import An XSLT file to the preceding transform object
Trans. Load ("numbersextension. XSL ");
// Create a new xsltargumentlist object
System. xml. XSL. Repeated targumentlist parameter Arg = new
System. xml. XSL. Pipeline targumentlist ();
// Call the addparam () method of the xsltargumentlist object to add Parameters
Parameter Arg. addparam ("displayme", "", "is this fun? ");
// Create a new extension object
Sayhello HI = new sayhello ();
// Call the addextensionobject () method of the extends targumentlist object to add the extension object
Using Arg. addextensionobject ("urn: sayhello", hi );
// Create a new system. Io. Stream object to process output results
System. Io. Stream STR = new system. Io. memorystream ();
// Perform the actual conversion operation
Trans. Transform (XP, xslarg, STR );
// Flush the stream object and set its position to 0
Str. Flush ();
Str. Position = 0;
// Create a new streamreader object to read the stream and return a string
System. Io. streamreader sr = new system. Io. streamreader (STR );
String xmlout = Sr. readtoend ();
// Close the streamreader object
Sr. Close ();
// Write the result to the console
Console. Write (xmlout );
// The Extended object returns the hello name
Public class sayhello
{
Public String helloname (string name ){
Return "hello" + name;
}
}
Through the above introduction, we found that there are many similarities between using extended objects and using embedded scripts. Next we will analyze some advantages and disadvantages of using extended objects over using embedded scripts.
Portability: embedded scripts are easier to transplant than extended objects, because any. NET platform can correctly convert a style sheet with an embedded script. On the other hand, style sheets using extended objects
Only one piece of code can be used for conversion.. NET platform or some other platforms that support extended objects, and require that this code correctly implement
Extension object.
Size: A style sheet dependent on an extended object is smaller than a style sheet containing embedded scripts and easier to maintain.
Flexibility: As we can see, style sheets dependent on extended objects can modify their behavior according to the function of modifying their extended objects. The style sheets using embedded scripts can always perform operations only according to the functions specified by the embedded script language.
Performance: Because the extension object is pre-compiled rather than real-time compiled, the performance of the script using the extension object is somewhat higher than that of the embedded script. However, the frequency of loading style sheets and the size of embedded scripts should also be considered.
Maintainability: it may be difficult to modify and correct styles using extended objects because the real code is separated from the style sheet and the code that provides extended object implementation. Although the embedded script is a little slower, it has the advantage of putting all the code locally.
By considering the above factors, we can decide whether to use extended objects or embedded scripts under specific circumstances.
VII. Summary:
This article introduces some knowledge about XSLT conversion in the. NET Framework. XSLT conversion technology is an important technology in XML .. . NET Framework provides
Robust support. It not only fully supports W3C-defined XSLT specifications, but also has many useful extensions, such as embedding a multi-language script in a style sheet, and using extended objects to expand XSLT functions,
This enhances the ease of use of style sheets in. NET applications. Finally, I hope you can use this article to master some basic XSLT conversion technologies related to the. NET Framework, and understand the advantages and disadvantages of each technology
It is well applied to actual projects.