开发者社区


C++/C | Java | .Net/Windows | Linux/Unix | 开源 | 其他
首页 - 编程
编程
全文搜索:   

XSLT / XML / C#
作者:
2001-08-21 17:36:33


这个例程展示了如何将xslt 应用于从数据库中读出的xml格式数据上. 例程完全使用C#语言编写:

using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Xml;
using System.Xml.Xsl;

public class XsltTransform
    {
        public static void Transform()
        {
            SqlConnection nwindConn = new SqlConnection("Data
            Source=INMUMIS123;database=northwind;uid=sa;pwd=;");
            nwindConn.Open();

            DataSet custDS = new DataSet("CustomerDataSet");

            SqlDataAdapter custDA = new SqlDataAdapter("SELECT * FROM Customers",
            nwindConn);
            custDA.Fill(custDS, "Customers");

            SqlDataAdapter ordersDA = new SqlDataAdapter("SELECT * FROM Orders",
            nwindConn);
            ordersDA.Fill(custDS, "Orders");

            nwindConn.Close();

            custDS.Relations.Add("CustOrders",
            custDS.Tables["Customers"].Columns["CustomerID"],
            custDS.Tables["Orders"].Columns["CustomerID"]).Nested = true;

            XmlDataDocument xmlDoc = new XmlDataDocument(custDS);

            XslTransform xslTran = new XslTransform();
            xslTran.Load("transform.xsl");

            // This is for generating the output in new html
            XmlTextWriter writer = new XmlTextWriter("xslt_output.html",
            System.Text.Encoding.UTF8);
            writer.Close();

            // This is for writing in the current page
            xslTran.Transform(xmlDoc, null, Response.OutputStream);

        }
}

 

上面这个称为XsltTransform的类连接到一个数据库,将数据填充到XmlDocument 中,然后将下面提供的Xslt应用于这个XML.


<%@ Page language="c#"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    <HEAD>
        <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
        <meta name="CODE_LANGUAGE" Content="C#">
        <meta name="vs_defaultClientScript" content="JavaScript (ECMAScript)">
        <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>

    <script>
    public void Page_Load(object sender, System.EventArgs e)
    {
        Transform();
    }
    public void Transform()
    {
        SqlConnection nwindConn = new SqlConnection("Data        
        Source=INMUMIS123;database=northwind;uid=sa;pwd=;");
        nwindConn.Open();

        DataSet custDS = new DataSet("CustomerDataSet");

        SqlDataAdapter custDA = new SqlDataAdapter("SELECT * FROM Customers",
        nwindConn);
        custDA.Fill(custDS, "Customers");

        SqlDataAdapter ordersDA = new SqlDataAdapter("SELECT * FROM Orders",
        nwindConn);
        ordersDA.Fill(custDS, "Orders");

        nwindConn.Close();

        custDS.Relations.Add("CustOrders",
        custDS.Tables["Customers"].Columns["CustomerID"],
        custDS.Tables["Orders"].Columns["CustomerID"]).Nested = true;

        XmlDataDocument xmlDoc = new XmlDataDocument(custDS);

        XslTransform xslTran = new XslTransform();
        xslTran.Load("transform.xsl");

        // XmlTextWriter writer = new XmlTextWriter("xslt_output.html",        
        System.Text.Encoding.UTF8);

        xslTran.Transform(xmlDoc, null, Response.OutputStream);
        // writer.Close();
    }
</script>

<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
</form>
</body>
</HTML>
 

上面的aspx page 用这个类(XsltTranform)建立了一对象, 然后调用了Transform 函数。


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

<xsl:template match="CustomerOrders">

    <STYLE>
    BODY
    TD
    </STYLE>

        <TABLE BORDER="1">
            <xsl:apply-templates select="Customers"/>
        </TABLE>


</xsl:template>

<xsl:template match="Customers">
    <TR><TD>
        <xsl:value-of select="ContactName"/>, <xsl:value-of select="Phone"/><BR/>
    </TD></TR>
        <xsl:apply-templates select="Orders"/>
</xsl:template>


<xsl:template match="Orders">
    <TABLE BORDER="1">
        <TR><TD valign="top"><B>Order:</B></TD><TD valign="top"><xsl:value-of select="OrderID"/></TD></TR>
        <TR><TD valign="top"><B>Date:</B></TD><TD valign="top"><xsl:value-of select="OrderDate"/></TD></TR>
        <TR><TD valign="top"><B>Ship To:</B></TD>
        <TD valign="top"><xsl:value-of select="ShipName"/><BR/>
            <xsl:value-of select="ShipAddress"/><BR/>
            <xsl:value-of select="ShipCity"/>, <xsl:value-of select="ShipRegion"/> <xsl:value-of select="ShipPostalCode"/><BR/>
<xsl:value-of select="ShipCountry"/></TD></TR>
        </TABLE>
</xsl:template>

</xsl:stylesheet>

 

最后是这个xslt 文件


声明:
Builder.com.cn(原ZDNet China应用开发频道)原创文章版权所有,未经许可严禁转载,且不构成投资建议。
近期相关报道:
实用技术文档
J2me XML
C/C++ C#
Java Oracle
Mysql .Net
VB.NET CSS
SQL Server 数据库
SQL UNIX
Linux Jsp
PHP Perl
Javascript IIS
XHTML ColdFusion
ASP/ASP.NET Apache
AJAX
订阅技术邮件
订阅"技术圈"杂志!请在下面选择您感兴趣的专题,填写e-mail地址,然后按订阅按钮:
应用开发管理
VS.NET 周刊
Database 周刊
WEB Service周刊
JAVA 周刊
IT 认证
Windows服务器周刊
互联网开发
当Windows Server 2008专家得5000元现金大奖
CNET NETWORKS 中国: 爱卡汽车网 | CNET科技资讯网 | e询网 | CWEEK | 蜂鸟网 | GameSpot China | 个人电脑 | PChome | SPN | 网友世界 | ZDNet China | 中关村在线
CNET NETWORKS 美国: BNET | CNET.com | CNET Download.com | CNET News.com | CNET Reviews | CNET Shopper.com | GameSpot | MP3.com | mySimon | Release 1.0 | Search.com | TechRepublic | TV.com | Webshots | ZDNet
Copyright (c) 2006 CNET Networks 版权所有。 ZDNet 是CNET Networks公司注册服务商标。
ZDNet 公司标识是 CNET Networks公司注册服务商标。
中华人民共和国电信与信息服务业务经营许可证编号:京ICP证010391号