以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 WORD to XML, HTML to XML 』  (http://bbs.xml.org.cn/list.asp?boardid=13)
----  How to convert XML files to the HTML.[推荐]  (http://bbs.xml.org.cn/dispbbs.asp?boardid=13&rootid=&id=39434)


--  作者:阳光不老
--  发布时间:10/27/2006 1:31:00 PM

--  How to convert XML files to the HTML.[推荐]
Introduction
You have an XML document ,you need to convert into a more readable file format. For example, you have personnel data that is stored as an XML document and you need to display it on a web page or in a text file.

Solution
The solution for this is to use an XSLT stylesheet to transform the XML into another format using the XslTransform class. In the example code, we are transforming some personnel data from a fictitious business stored in Personnel.xml. First, we load the stylesheet for generating HTML output, then we perform the transformation to HTML via XSLT using the PersonnelHTML.xsl stylesheet. After that, we transform the data to comma-delimited format using the PersonnelCSV.xsl stylesheet:

Expand code snippet
public static void TransformXML( )

{

      // Create a resolver with default credentials.

      XmlUrlResolver resolver = new XmlUrlResolver( );

      resolver.Credentials = System.Net.CredentialCache.DefaultCredentials;

      // transform the personnel.xml file to html

      XslTransform transform = new XslTransform( );

      // load up the stylesheet

      transform.Load(@"..\PersonnelHTML.xsl",resolver);

      // perform the transformation

      transform.Transform(@"..\Personnel.xml",@"..\Personnel.html",resolver);

      // transform the personnel.xml file to comma delimited format

      // load up the stylesheet

      transform.Load(@"..\PersonnelCSV.xsl",resolver);

      // perform the transformation

      transform.Transform(@"..\Personnel.xml", @"..\Personnel.csv",resolver);

}

The Personnel.xml file contains the following items:

<?xml version="1.0" encoding="utf-8"?>

<Personnel>

     <Employee name="Shahab" title="Customer Service" companyYears="1"/>

     <Employee name="Noosha" title="Manager" companyYears="12"/>

     <Employee name="NavidChas" title="Salesman" companyYears="3"/>

     <Employee name="Mehrdad" title="CEO" companyYears="27"/>

<Personnel>

The PersonnelHTML.xsl stylesheet looks like this:

Collapse
<?xml version="1.0" encoding="UTF-8"?>

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

           xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xsl:template match="/">
<html>
<head />

  <body title="Personnel">

     <xsl:for-each select="Personnel">

     <p>

     <xsl:for-each select="Employee">

     <xsl:if test="position( )=1">

          <table border="1">

          <thead>

          <tr>

                    <td>Employee Name</td>

                    <td>Employee Title</td>

                    <td>Years with Company</td>

          </tr>

       </thead>

       <tbody>

       <xsl:for-each select="../Employee">

           <tr>

           <td>

              <xsl:for-each select="@name">

              <xsl:value-of select="." />

              </xsl:for-each>

         </td>

         <td>

              <xsl:for-each select="@title">

              <xsl:value-of select="." />

              </xsl:for-each>

        </td>

        <td>

              <xsl:for-each select="@companyYears">

              <xsl:value-of select="." />

              </xsl:for-each>

        </td>

        </tr>

        </xsl:for-each>

      </tbody>

      </table>

      </xsl:if>

</xsl:for-each>

</p>

</xsl:for-each>

</body>

</html>

</xsl:template>

</xsl:stylesheet>

Here is the HTML source:

Collapse
<html xmlns:xs="http://www.w3.org/2002/XMLSchema">

<head>

<META http-equiv="Content-Type" content="text/html; charset=utf-8">

</head>

<body title="Personnel">

     <p>

     <table border="1">
     <thead>

     <tr>

          <td>Employee Name</td>

          <td>Employee Title</td>
          <td>Years with Company</td>

     </tr>
     </thead>
  
     <tbody>

     <tr>
          <td>Shahab</td>

          <td>Customer Service</td>
          <td>1</td>

     </tr>

     <tr>
          <td>Noosha</td>

          <td>Manager</td>
          <td>12</td>

     </tr>

     <tr>
          <td>Navid</td>

          <td>Salesman</td>
          <td>3</td>

     </tr>

    <tr>
         <td>Mehrdad</td>

         <td>CEO</td>
         <td>27</td>

    </tr>

    </tbody>
    </table>

    </p>

</body>

</html>

The comma-delimited output is generated using PersonnelCSV.xsl and Personnel.xml; the stylesheet is shown here:

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

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

xs="http://www.w3.org/2002/XMLSchema">

<xsl:output method="text" encoding="UTF-8"/>

<xsl:template match="/">

<xsl:for-each select="Personnel">

<xsl:for-each select="Employee">

<xsl:for-each select="@name">

<xsl:value-of select="." />

</xsl:for-each>,<xsl:for-each select="@title">

<xsl:value-of select="." />

</xsl:for-each>,<xsl:for-each select="@companyYears">

<xsl:value-of select="." />

</xsl:for-each>

<xsl:text> </xsl:text>

</xsl:for-each>

</xsl:for-each>

</xsl:template>

</xsl:stylesheet>

The output from the PersonnelCSV.xsl stylesheet is shown here:

Shahab,Customer Service,1
Noosha,Manager,12
Navid,Salesman,3
Mehrdad,CEO,27


--  作者:tomcat7911
--  发布时间:11/22/2006 3:18:00 PM

--  
还是英文的
--  作者:东方伯
--  发布时间:8/2/2007 9:17:00 PM

--  
还需要学习啊,好多看不懂!
--  作者:fangel2000
--  发布时间:10/27/2007 5:07:00 PM

--  
有没有好点的 “convert HTML to XML” 的啊,大家给推荐一下啊
--  作者:bigon2000
--  发布时间:6/11/2008 4:40:00 PM

--  
路还很长啊...而且还很苦...努力学习....
--  作者:andy天使鱼
--  发布时间:12/5/2008 12:28:00 PM

--  
没看懂啊
--  作者:hjx_221
--  发布时间:1/23/2009 1:13:00 PM

--  
祝福牛年万事平安顺利,新春愉快
W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
62.012ms