XSLT syntax-sample code for converting xml documents using XSLT in. net

Source: Internet
Author: User
Tags xsl file
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. 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

Example code:

You only need to add 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 = "#" specifies the xsl namespace, as shown in the following example:

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:

 

2) xsl: for-each loop display select = "xpath expression" select node conversion (similar to the foreach statement in programming languages ),

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

 
  
 

3) xsl: if element condition display nodes (similar to the if statement in programming languages) should be replaced by <and>

 
  its weight is less than 10 km
 

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

  
  
   this is a pig 
  
    
   this is not a pig
  
 

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

Select sub-node price

 

Select weight

 

6) xsl: attribute: construct the attributes of an xml node.

Used to add attributes to a node, for example:

 
  
 

Output

7) xsl: apply-templates application Template

If the xml file structure is complex, you can define multiple templates and then use The label application template. for 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


 
   
                    
           
   lovely pets        
                       
   
            lovely pets        
    
 
  • #;/xsl:attribute> #;/xsl:attribute> #@N00.jpg?1143660418 Courier said: " " weight:

    its weight is less than 10 km

The complete example file pets-templates.xsl:

 
   
                    
           
   lovely pets        
                       
   
              lovely pets          
    
 
  • #;/xsl:attribute> Courier dog said: " " weight:

    its weight is less than 10 km

  • #;/xsl:attribute> Courier pig said: " " weight:

    its weight is less than 10 km

  • #@N00.jpg?1143660418 Courier cat said: " " weight:

    its weight is less than 10 km

  • In c #. net, XslCompiledTransform can be used to convert xml documents, but this class has been marked as obsolete by Microsoft. it is better not to use it any more. 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 using compiledtransform is in html format. this class automatically adds an unclosed meta tag to the html head tag. Microsoft has helped us think too much.

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

    The above is the detailed sample code for converting xml documents using XSLT in. net. For more information, see other related articles in the first PHP community!

    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.