根据xslt定义xml文件生成html文件
有xls文件如下:
 <!--
<!--
 - XSLT is a template based language to transform Xml documents
    - XSLT is a template based language to transform Xml documents
 It uses XPath to select specific nodes
    It uses XPath to select specific nodes 
 for processing.
    for processing.
 
    
 - A XSLT file is a well formed Xml document
    - A XSLT file is a well formed Xml document
 -->
-->

 <!-- every StyleSheet starts with this tag -->
<!-- every StyleSheet starts with this tag -->
 <xsl:stylesheet
<xsl:stylesheet 
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 version="1.0">
      version="1.0">
 <!-- indicates what our output type is going to be -->
<!-- indicates what our output type is going to be -->
 <xsl:output method="html" />
<xsl:output method="html" />
 <!--
    <!-- 
 Main template to kick off processing our Sample.xml
        Main template to kick off processing our Sample.xml
 From here on we use a simple XPath selection query to
        From here on we use a simple XPath selection query to 
 get to our data.
        get to our data.
 -->
    -->
 <xsl:template match="/">
    <xsl:template match="/">    
 <html>
        <html>    
 <head>
            <head>                
 <title>Welcome to <xsl:value-of select="/company/name"/></title>
                <title>Welcome to <xsl:value-of select="/company/name"/></title>                
 <style>
                <style>
 body,td {font-family:Tahoma,Arial; font-size:9pt;}
                    body,td {font-family:Tahoma,Arial; font-size:9pt;}
 </style>
                </style>                
 </head>
            </head>            
 <body>
            <body>
 <h2>Welcome to <xsl:value-of select="/company/name"/></h2>
                <h2>Welcome to <xsl:value-of select="/company/name"/></h2>
 <p/>
                <p/>
 <b>Our contact details:</b>
                <b>Our contact details:</b>
 <br/>
                <br/>
 <br/>
                <br/>        
 <xsl:value-of select="/company/name"/>
                <xsl:value-of select="/company/name"/>
 <br/>
                <br/>
 <xsl:value-of select="/company/address1"/>
                <xsl:value-of select="/company/address1"/>
 <br/>
                <br/>
 <xsl:value-of select="/company/address2"/>
                <xsl:value-of select="/company/address2"/>
 <br/>
                <br/>
 <xsl:value-of select="/company/city"/>
                <xsl:value-of select="/company/city"/>
 <br/>
                <br/>
 <xsl:value-of select="/company/country"/>
                <xsl:value-of select="/company/country"/>
 </body>
            </body>    
 </html>
        </html>    
 </xsl:template>
    </xsl:template>
 </xsl:stylesheet>
</xsl:stylesheet>
 
xml文件如下:
 <company>
<company>
 <name>XYZ Inc.</name>
    <name>XYZ Inc.</name>
 <address1>One Abc Way</address1>
    <address1>One Abc Way</address1>
 <address2>Some avenue</address2>
    <address2>Some avenue</address2>
 <city>Tech city</city>
    <city>Tech city</city>
 <country>Neverland</country>
    <country>Neverland</country>
 </company>
</company>

 2个文件命名分别为:sample.xsl和sample.xml
2个文件命名分别为:sample.xsl和sample.xml
C#代码如下:
 public static void Transform(string sXmlPath, string sXslPath)
   public static void Transform(string sXmlPath, string sXslPath)
 {
        {

 try
            try
 {
            {

 //load the Xml doc
                //load the Xml doc
 XPathDocument myXPathDoc = new XPathDocument(sXmlPath);
                XPathDocument myXPathDoc = new XPathDocument(sXmlPath);

 XslTransform myXslTrans = new XslTransform();
                XslTransform myXslTrans = new XslTransform();

 //load the Xsl
                //load the Xsl 
 myXslTrans.Load(sXslPath);
                myXslTrans.Load(sXslPath);

 //create the output stream
                //create the output stream
 XmlTextWriter myWriter = new XmlTextWriter
                XmlTextWriter myWriter = new XmlTextWriter
 ("result.html", null);
                    ("result.html", null);

 //do the actual transform of Xml
                //do the actual transform of Xml
 myXslTrans.Transform(myXPathDoc, null, myWriter);
                myXslTrans.Transform(myXPathDoc, null, myWriter);

 myWriter.Close();
                myWriter.Close();


 }
            }
 catch (Exception e)
            catch (Exception e)
 {
            {

 Console.WriteLine("Exception: {0}", e.ToString());
                Console.WriteLine("Exception: {0}", e.ToString());
 }
            }

 }
        }
生成result.html如下:
 <html>
<html>
 <head>
<head>
 <title>Welcome to XYZ Inc.</title>
<title>Welcome to XYZ Inc.</title>
 <style>
<style>
 body,td {font-family:Tahoma,Arial; font-size:9pt;}
body,td {font-family:Tahoma,Arial; font-size:9pt;}
 </style>
</style>
 </head>
</head>
 <body>
<body>
 <h2>Welcome to XYZ Inc.</h2>
<h2>Welcome to XYZ Inc.</h2>
 <p/>
<p/>
 <b>Our contact details:</b>
<b>Our contact details:</b>
 <br />
<br />
 <br />
<br />
 XYZ Inc.
XYZ Inc.
 <br />
<br />
 One Abc Way
One Abc Way
 <br />
<br />
 Some avenue
Some avenue
 <br />
<br />
 Tech city
Tech city
 <br />
<br />
 Neverland
Neverland
 </body>
</body>
 </html>
</html>
这样可以生成页面
主要是xls 需要好好学习
  
 <!--
<!-- - XSLT is a template based language to transform Xml documents
    - XSLT is a template based language to transform Xml documents It uses XPath to select specific nodes
    It uses XPath to select specific nodes  for processing.
    for processing. 
     - A XSLT file is a well formed Xml document
    - A XSLT file is a well formed Xml document -->
-->
 <!-- every StyleSheet starts with this tag -->
<!-- every StyleSheet starts with this tag --> <xsl:stylesheet
<xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
      version="1.0"> <!-- indicates what our output type is going to be -->
<!-- indicates what our output type is going to be --> <xsl:output method="html" />
<xsl:output method="html" /> <!--
    <!--  Main template to kick off processing our Sample.xml
        Main template to kick off processing our Sample.xml From here on we use a simple XPath selection query to
        From here on we use a simple XPath selection query to  get to our data.
        get to our data. -->
    --> <xsl:template match="/">
    <xsl:template match="/">     <html>
        <html>     <head>
            <head>                 <title>Welcome to <xsl:value-of select="/company/name"/></title>
                <title>Welcome to <xsl:value-of select="/company/name"/></title>                 <style>
                <style> body,td {font-family:Tahoma,Arial; font-size:9pt;}
                    body,td {font-family:Tahoma,Arial; font-size:9pt;} </style>
                </style>                 </head>
            </head>             <body>
            <body> <h2>Welcome to <xsl:value-of select="/company/name"/></h2>
                <h2>Welcome to <xsl:value-of select="/company/name"/></h2> <p/>
                <p/> <b>Our contact details:</b>
                <b>Our contact details:</b> <br/>
                <br/> <br/>
                <br/>         <xsl:value-of select="/company/name"/>
                <xsl:value-of select="/company/name"/> <br/>
                <br/> <xsl:value-of select="/company/address1"/>
                <xsl:value-of select="/company/address1"/> <br/>
                <br/> <xsl:value-of select="/company/address2"/>
                <xsl:value-of select="/company/address2"/> <br/>
                <br/> <xsl:value-of select="/company/city"/>
                <xsl:value-of select="/company/city"/> <br/>
                <br/> <xsl:value-of select="/company/country"/>
                <xsl:value-of select="/company/country"/> </body>
            </body>     </html>
        </html>     </xsl:template>
    </xsl:template> </xsl:stylesheet>
</xsl:stylesheet>
xml文件如下:
 <company>
<company> <name>XYZ Inc.</name>
    <name>XYZ Inc.</name> <address1>One Abc Way</address1>
    <address1>One Abc Way</address1> <address2>Some avenue</address2>
    <address2>Some avenue</address2> <city>Tech city</city>
    <city>Tech city</city> <country>Neverland</country>
    <country>Neverland</country> </company>
</company>

C#代码如下:
 public static void Transform(string sXmlPath, string sXslPath)
   public static void Transform(string sXmlPath, string sXslPath) {
        {
 try
            try {
            {
 //load the Xml doc
                //load the Xml doc XPathDocument myXPathDoc = new XPathDocument(sXmlPath);
                XPathDocument myXPathDoc = new XPathDocument(sXmlPath);
 XslTransform myXslTrans = new XslTransform();
                XslTransform myXslTrans = new XslTransform();
 //load the Xsl
                //load the Xsl  myXslTrans.Load(sXslPath);
                myXslTrans.Load(sXslPath);
 //create the output stream
                //create the output stream XmlTextWriter myWriter = new XmlTextWriter
                XmlTextWriter myWriter = new XmlTextWriter ("result.html", null);
                    ("result.html", null);
 //do the actual transform of Xml
                //do the actual transform of Xml myXslTrans.Transform(myXPathDoc, null, myWriter);
                myXslTrans.Transform(myXPathDoc, null, myWriter);
 myWriter.Close();
                myWriter.Close();

 }
            } catch (Exception e)
            catch (Exception e) {
            {
 Console.WriteLine("Exception: {0}", e.ToString());
                Console.WriteLine("Exception: {0}", e.ToString()); }
            }
 }
        }生成result.html如下:
 <html>
<html> <head>
<head> <title>Welcome to XYZ Inc.</title>
<title>Welcome to XYZ Inc.</title> <style>
<style> body,td {font-family:Tahoma,Arial; font-size:9pt;}
body,td {font-family:Tahoma,Arial; font-size:9pt;} </style>
</style> </head>
</head> <body>
<body> <h2>Welcome to XYZ Inc.</h2>
<h2>Welcome to XYZ Inc.</h2> <p/>
<p/> <b>Our contact details:</b>
<b>Our contact details:</b> <br />
<br /> <br />
<br /> XYZ Inc.
XYZ Inc. <br />
<br /> One Abc Way
One Abc Way <br />
<br /> Some avenue
Some avenue <br />
<br /> Tech city
Tech city <br />
<br /> Neverland
Neverland </body>
</body> </html>
</html>这样可以生成页面
主要是xls 需要好好学习
 
                    
                 
        
 
             
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号