Due to the needs of the project, I recently learned the transformation from XLST and XML to HTML.
The ultimate goal of an XML document is to store information as a basic type of information. loads are converted to different formats for various applications. W3C proposed extended style language specifications to achieve the above purpose. XSLT (the Extensible Stylesheet Language Transformation) is a standard language for XML text conversion and formatting. This document uses a simple example to describe how the XML file is formatted and converted to HTML.
The following is a part of the XML file. Describes the entities in the database:
<? XML version = "1.0" encoding = "UTF-8 "?
<Entities>
<Entity Title = "Company customer" name = "company" module = "users">
<Item Title = "name" name = "name" type = "text"/>
<Item Title = "no." name = "Number" type = "text" property = "get"/>
<Item Title = "manager" name = "manage" type = "text"/>
<Item Title = "phone" name = "phone" type = "text"/>
<Item Title = "Contact" name = "linkman" type = "text"/>
<Item Title = "Address" name = "Address" type = "text"/>
<Item Title = "fax" name = "fax" type = "text" require = "false"/>
</Entity>
<Entity Title = "user" name = "user" module = "users">
<Item Title = "email" name = "email" type = "text"/>
<Item Title = "password" name = "password" type = "text"/>
<Item Title = "total consumption" name = "totalconsumption" type = "int"/>
<Item Title = "Real Name" name = "name" type = "text" require = "false"/>
<Item Title = "company" name = "company" type = "entity" entityname = "company" require = "false"/>
</Entity>
<Entity Title = "User address" name = "useraddress" module = "users">
<Item Title = "user" name = "user" type = "entity" entityname = "user"/>
<Item Title = "Address" name = "Address" type = "text"/>
<Item Title = "phone" name = "phone" type = "text"/>
<Item Title = "whether it is the default" name = "isdefault" type = "bool"/>
</Entity>
</Entities>
What I want to do now is to convert an XML file into a table, which means that objects and attributes can be displayed intuitively. Below is the XLST corresponding to the XML file:
<? XML version = "1.0" encoding = "UTF-8"?>
<XSL: stylesheet version = "1.0" xmlns: XSL = "http://www.w3.org/1999/XSL/Transform"
Xmlns: msxsl = "urn: Schemas-Microsoft-com: XSLT" exclude-result-prefixes = "msxsl"
>
<XSL: template match = "/">
<HTML>
<Body>
<H2 align = "center">
Real Data Analysis of Short-distance catering logistics platform <br/>
</H2>
<XSL: Apply-templates select = "entities/entity"/> // apply the template to the entity Node
</Body>
</Html>
</XSL: Template>
// Template corresponding to the entity Node
<XSL: template match = "entity">
<B>
<Font color = "bisgue">
<XSL: value-of select = "@ title"/> (<XSL: value-of select = "@ name"/>) data analysis:
</Font>
<Br/>
</B>
<Table width = "70%" bordercolor = "black" border = "1">
<Tr align = "center" style = "background-color: White;">
& Lt; th width = "20%" & gt; Data name & lt;/th & gt;
& Lt; th width = "20%" & gt; Code </Th>
<TH width = "20%"> type </Th>
<TH width = "20%"> whether it can be null </Th>
<TH> description </Th>
</Tr>
<XSL: For-each select = "item"> // batch processing of nodes, which corresponds to the element XSL: apply-templates is two different methods, but the output result is figured out.
<Tr align = "center">
<TD>
<XSL: value-of select = "@ title"/>
</TD>
<TD>
<XSL: value-of select = "@ name"/>
</TD>
<TD>
<XSL: value-of select = "@ Type"/>
</TD>
<TD>
<XSL: Apply-templates select = "@ require"/>
<XSL: If test = "not (@ require)"> // judge whether a node exists and use the function not (the node to be judged)
No
</XSL: If>
</TD>
<TD>
<XSL: value-of select = "(@ description)"/>
<XSL: If test = "not (@ description)">
Null
</XSL: If>
</TD>
</Tr>
</XSL: For-each>
</Table>
<Br/>
</XSL: Template>
<XSL: template match = "@ require">
Yes
</XSL: Template>
</XSL: stylesheet>
The following is the output format of the XML file after it is applied to XLST:
Company Data Analysis:
& Lt; th width = "20%" & gt; Data name & lt;/th & gt;
Code |
type |
whether it can be null |
description |
name |
name |
text |
NO |
null |
NO. |
Number |
text |
NO |
null |
Manager |
Manage |
text |
NO |
null |
phone number |
phone |
text |
NO |
null |
contact |
linkman |
text |
NO |
null |
address |
address |
text |
NO |
null |
Fax |
Fax |
text |
Yes |
null |
User Data Analysis:
Data name |
Code |
Type |
Can it be blank? |
Description |
Email |
Email |
Text |
No |
Null |
Password |
Password |
Text |
No |
Null |
Total consumption |
Totalconsumption |
Int |
No |
Null |
Real name |
Name |
Text |
Yes |
Null |
Company |
Company |
Entity |
Yes |
Null |
User address (useraddress) data analysis:
Data name |
Code |
Type |
Can it be blank? |
Description |
User |
User |
Entity |
No |
Null |
Address |
Address |
Text |
No |
Null |
Phone number |
Phone |
Text |
No |
Null |
Is it default? |
Isdefault |
Bool |
No |
Null |
The following code converts an XML file to an HTML file:
/// <Summary>
/// Convert XML into HTML
/// </Summary>
/// <Param name = "xmlpath"> XML file path </param>
/// <Param name = "xslfilepath"> XSLT file path </param>
/// <Param name = "htmlfilepath"> claimed HTML file path </param>
Public static void xmltranstohtml (string xmlpath, string delimiter filepath, string htmlfilepath)
{
// Generate the HTML file path
String htmlfilepath = htmlfilepath;
Xpathdocument myxpathdoc = new xpathdocument (xmlpath );
Extends compiledtransform my‑trans = new extends compiledtransform ();
// Load the XSL File
Mydomaintrans. Load (your filepath );
Xmltextwriter mywriter = new xmltextwriter (htmlfilepath, system. Text. encoding. Default );
Mydomaintrans. Transform (myxpathdoc, null, mywriter );
Mywriter. Close ();
}
Call this method in the test class and run the test to obtain the HTML file we need. (Compiling environment vs.net 2008 ).