原XML  XMLFile.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<catalog>
  <cd>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
  </cd>
</catalog>然后用XSLT解析上面的XML  cdcatalog.xsl
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" />
<xsl:template match="/">
      <h2>My CD Collection</h2>
      <table border="0">
        <tr bgcolor="#9acd32">
          <th align="left">Title</th>
          <th align="left">Artist</th>
        </tr>
        <xsl:for-each select="catalog/cd">
          <tr>
            <td>
              <xsl:value-of select="title"/>
            </td>
            <td>
              <xsl:value-of select="artist"/>
            </td>
          </tr>
        </xsl:for-each>
      </table>
</xsl:template>
</xsl:stylesheet> 
然后在原XML中引用XSL   
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
<catalog>
  <cd>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
  </cd>
</catalog>然后在浏览器中浏览结果
My CD Collection
Title               Artist
Empire Burlesque    Bob Dylan现在我想把XML转换成HTML,然后可以插入到模板页HTML中,可编译不知道如何转换成HTML代码?<xsl:output method="html" />处理器输出HTML代码,不知道在哪输出的?哪位大虾帮帮忙!

解决方案 »

  1.   

    在项目中建立文件夹xml,然后把你的xml和xsl文件放在里面private void TransForm()
        {
            string rootPath = GetRootPath();
            //取得xml路径
            string xmlPath = "/xml/XMLFile.xml";
            xmlPath = xmlPath.Replace("/", "\\");
            xmlPath = rootPath.Trim() + xmlPath;
            //取得xsl路径
            string xslPath = "/xml/cdcatalog.xsl";
            xslPath = xslPath.Replace("/", "\\");
            xslPath = rootPath.Trim() + xslPath;
            //设置html的保存路径
            string htmlPath = "/xml/HTMLFile.html";
            htmlPath = htmlPath.Replace("/", "\\");
            htmlPath = rootPath.Trim() + htmlPath;        //将XML转换成HTML
            XslCompiledTransform transform = new XslCompiledTransform();
            transform.Load(xslPath);
            transform.Transform(xmlPath, htmlPath);
            //Response.Write(htmlPath);
        }
      

  2.   

    private void TransForm()
        {
            string rootPath = GetRootPath();
            //取得xml路径
            string xmlPath = "/xml/XMLFile.xml";
            xmlPath = xmlPath.Replace("/", "\\");
            xmlPath = rootPath.Trim() + xmlPath;
            //取得xsl路径
            string xslPath = "/xml/cdcatalog.xsl";
            xslPath = xslPath.Replace("/", "\\");
            xslPath = rootPath.Trim() + xslPath;
            //设置html的保存路径
            string htmlPath = "/xml/HTMLFile.html";
            htmlPath = htmlPath.Replace("/", "\\");
            htmlPath = rootPath.Trim() + htmlPath;        //将XML转换成HTML
            XslCompiledTransform transform = new XslCompiledTransform();
            transform.Load(xslPath);
            transform.Transform(xmlPath, htmlPath);
            //Response.Write(htmlPath);
        }