新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> XML与各种文件格式的相互转换及相关工具。 word to xml, xml to word, html to xml, xml to pdf,
    csv to xml, rtf to xml, text to xml, xml to text, xls to xml, xml to xls
    FOP
    [返回] 中文XML论坛 - 专业的XML技术讨论区XML.ORG.CN讨论区 - XML技术『 WORD to XML, HTML to XML 』 → How to convert XML files to the HTML.[推荐] 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 26381 个阅读者浏览上一篇主题  刷新本主题   平板显示贴子 浏览下一篇主题
     * 贴子主题: How to convert XML files to the HTML.[推荐] 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     阳光不老 帅哥哟,离线,有人找我吗?
      
      
      等级:大二期末(C++考了100分!)
      文章:37
      积分:384
      门派:XML.ORG.CN
      注册:2004/10/18

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给阳光不老发送一个短消息 把阳光不老加入好友 查看阳光不老的个人资料 搜索阳光不老在『 WORD to XML, HTML to XML 』的所有贴子 访问阳光不老的主页 引用回复这个贴子 回复这个贴子 查看阳光不老的博客楼主
    发贴心情 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


       收藏   分享  
    顶(0)
      




    ----------------------------------------------
    规矩网络 Refree.Cn 打造中国顶级的资源重组网站!

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2006/10/27 13:31:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 WORD to XML, HTML to XML 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2022/12/9 22:08:38

    本主题贴数7,分页: [1]

     *树形目录 (最近20个回帖) 顶端 
    主题:  How to convert XML files ..(5414字) - 阳光不老,2006年10月27日
        回复:  祝福牛年万事平安顺利,新春愉快(29字) - hjx_221,2009年1月23日
        回复:  没看懂啊(8字) - andy天使鱼,2008年12月5日
        回复:  路还很长啊...而且还很苦...努力学习....(38字) - bigon2000,2008年6月11日
        回复:  有没有好点的 “convert HTML to XML” 的啊,大家给推荐一下啊..(59字) - fangel2000,2007年10月27日
        回复:  还需要学习啊,好多看不懂!(26字) - 东方伯,2007年8月2日
        回复:  还是英文的(10字) - tomcat7911,2006年11月22日

    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    70.313ms