Example of XML document conversion using XSLT syntax in. net

Source: Internet
Author: User
Tags xsl xsl file xslt

The style sheet file that can be extended by XSL. You can format the display of XML, or convert XML into another format.

To learn XSL, you must be familiar with XPath. XSL is as simple and powerful as XPath and is easy to learn.

1. Since XSL can format the XML display style, Let's first look at how to reference the XSL file in XML

As follows:CodeExample:

<? XML version = "1.0" encoding = "UTF-8"?>

<? XML-stylesheet type = "text/XSL" href = "url. XSL"?>

You only need to add <? XML-stylesheet type = "text/XSL" href = "url. XSL"?> You can.

2. XSL format

XSL is also a standard XML file. It starts with an XML file declaration. The root element must be XSL: styleshee, and the root element must have the version attribute to specify the XSL version, and xmlns: XSL = "http://www.w3.org/5o/#/transform, as shown in the following example

<? XML version = "1.0" encoding = "encoding"?>

<XSL: stylesheet version = "1.0" xmlns: XSL = "http://www.w3.org/1999/XSL/Transform">

3. The main points of XSL are as follows: XML

 
   
   
   
    
    
      100 
     
    
      This is a blue pig 
     
    
    
    
      80 
     
    
      This is a Red Cat 
     
    
    
    
      80 
     
    
      This is a green dog 
     
    
    
    
      80 
     
    
      This is a green cat 
     
    
    
    
      100 
     
    
      This is a blue dog 
     
    
    
    
      80 
     
    
      This is a Red Dog 
     
    
   

The preceding XML format is as follows:

1) XSL: Template defines the conversion template for matching nodes. The attribute match = "XPath expression" is used to define elements matching the template.

The template that matches the root node is defined as follows:

<XSL: template match = "/">

</XSL: Template>

2) XSL: For-each loop display select = "XPath expression" select node conversion (similarProgramming Language),

In the following example, child elements under pets are selected and the names of child elements are displayed cyclically:

<XSL: For-each select = "/pets/*">

<XSL: value-of select = "Name ()"/>

</XSL: For-each>

3) XSL: If element condition display nodes (similar to the IF statement in programming languages) should be replaced by & lt; and

<XSL: If test = "@ weight & lt; 10">

<I> its weight is less than 10 km </I>

</XSL: If>

4) XSL: multi-branch condition display of choose (similar to the switch statement in programming languages)

<XSL: Choose>

<XSL: When test = "name () = 'pig'">

<I> This is a pig </I>

</XSL: When>

<XSL: otherwise>

<I> This is not a pig </I>

</XSL: otherwise>

</XSL: Choose>

5) XSL: value-of displays the value of the selected node or attribute.

Select sub-node price

<XSL: value-of select = "pets/*/price"/>

Select weight

<XSL: value-of select = "pets/*/@ weight"/>

6) XSL: attribute: Construct the attributes of an XML node.

Used to add attributes to a node, for example:

<Font>

<XSL: attribute name = "color"> <XSL: value-of select = "pets/*/@ color"/> </XSL: attribute>

</Font>

<Font color = "red"> </font>

7) XSL: Apply-templates Application Template

If the XML file structure is complex, you can define multiple templates, and then use the <XSL: Apply-templates> label Application Template, XSL: apply-templates you can specify the select = "XPath" attribute to select the template of the application, or do not specify select to select the template of the current node.

See the following sample XSLT file pets-templates.xsl

Complete sample XSL file: pets. XSL
<? XML version = "1.0" encoding = "UTF-8"?> <XSL: stylesheet version = "1.0" xmlns: XSL = "http://www.w3.org/1999/XSL/Transform"> <XSL: template match = "/"> <HTML> 

complete example file pets-templates.xsl:

<? XML version = "1.0" encoding = "UTF-8"?> <XSL: stylesheet version = "1.0" xmlns: XSL = "http://www.w3.org/1999/XSL/Transform"> <XSL: template match = "/"> <HTML> 

in C #. net, which can also be used to convert XML documents using compiledtransform. However, this class has been marked obsolete by Microsoft and should not be reused. The following code example:

 using system; using system. collections. generic; using system. LINQ; using system. text; using system. io; using system. XML; namespace usexslt {class program {static void main (string [] ARGs) {// declare the xsltransform class instance system. XML. XSL. export compiledtransform trans = new system. XML. XSL. extends compiledtransform (); string effectfile = @ "X: \ about.net \ system. XML \ example \ pets. XSL "; using (streamreader RDR = new streamreader (xsltfile) {using (xmlreader xmlrdr = xmlreader. create (RDR) {// load the XSL file trans. load (xmlrdr) ;}} string inputfile = @ "X: \ about.net \ system. XML \ example \ pets. XML "; string outputfile = @" X: \ about.net \ system. XML \ example \ pets-out.htm "; // convert the source file output to the output file outputfile trans. transform (inputfile, outputfile) ;}}

Note that the file converted from xslcompiledtransform is in HTML format, this class automatically adds an unclosed meta tag <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8 ">; Microsoft has helped us think too much.

XSLT can also specify parameters and define variables. For more information, see related documents.

C # processing XML:

1. read and Write XML documents through xmldocument 2. use xmlreader to read XML, use xmlwriter to write xml3. use LINQ to XML to access xml4. use xmlscheme to define fixed-format XML documents. 5. XML serialization or deserialization Class 6. search for XML nodes using XPath 7. convert XML format using XSLT

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.