Detailed sample code for converting XML data into HTML

Source: Internet
Author: User
Tags xsl
You can use a simple XSL style sheet to convert XML data into HTML. With the continuous evolution of XML specifications, it seems necessary to meet the needs of everyone in the new version. suppose there is an XML data that represents the content of a page, now I want to convert the content to layout. Below is the XML to be converted: & lt ;? Xmlver converts XML data to HTML using a simple XSL style table. With the continuous evolution of XML specifications, it seems necessary to meet the needs of everyone in the new version. suppose there is an XML data that represents the content of a page, now I want to convert the content to layout. The XML to be converted is as follows:

  
  
  
   
    
    
     Folder1
     
     
      
      
       File1
       
       
        
         
         
          string
          
         
          50
          
         
          somedata
           
        
       
      
     
    
   
 

This content indicates a group of folders, files, and fields. Each folder contains files, and each file contains the fields used for input data. Each folder in the folder group is represented as a TR element and a TD element in the first row of a TABLE. Each file in the file group represents a TR element and a TD element in the first row of a TABLE element nested in the TR element of the folder. Each domain in the Domain Group is represented as an INPUT in the relevant file.
The following is the XSL used for this conversion:

  
  
 
   functiongetElementCount(nodelist,what){ varrtrn=0; rtrn=nodelist[0].parentNode.selectNodes(what).length; return(rtrn 1);//1isaddedforfillerTD }  
   
     
     
      
      
       width:55px 
            
         
         
          
            
            
            
             
             
              width:55px; 
                   
                
                
                 
                   
                  
                   
                   
                    text 
                     
                       
                      
                        
                      

In the stylesheet tag, several namespaces are set, including the xsl namespaces that define all xsl conversion tags. This allows us to create the msxml namespace for user functions that can be used in the style sheet. Use it to get all the child elements to get a COLSPAN attribute set marked by TD. The fn namespace used to join a group of user-defined functions. This namespace is created by msxml: script element.
Then, we create an external TABLE and the first TR. In TR, create a TD for each folder specified in XML. The xsl: element tag is used because it allows you to add custom attributes or execute a function to set an attribute for the COLSPAN attribute in another TD element.
After creating the required TD for each folder, create TR for each folder. Add only one TD to this TR, but set its COLSPAN attribute to equal to the number of folder tags in the folder group and add one. The other one is used to fill spaces in a fixed layout TABLE.
To get COLSPAN, enter the name of the current context (specified by "." here) and computing node. In the function, obtain the number of nodes specified in the current context, paraentNode, and XPath query. Then, the function returns this quantity and adds it to fill in the TD.
With this TD, another TABLE is embedded in the TABLE, which contains every file in the file group. From this point on, the process is the same as external TABLE conversion. The last step is to add fields in each file.
Once the general layout is complete, you can add the user interface function, such as hiding other folders and file lines until the user clicks the relevant tab. This function can be implemented by writing scripts that support this function, adding an onclick xsl: attribute element to the folder and file TD element, set its value to the name of the script function.
Finally, after the general functions are completed, you can add the class xsl: attributes and add the relevant classNames in the STYLE or CSS to get the desired appearance.
This example creates a foundation for the File-Folder-Field view used in the Web data solution deployment. Visit MSDN to find more information about Microsoft's XML specifications.

The preceding section details the sample code for converting XML data into HTML. 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.