XSLT / XML / C#

ZDNet软件频道 时间:2001-08-21 作者: |  我要评论()
本文关键词:C# XML XSLT
这个例程展示了如何将xslt 应用于从数据库中读出的xml格式数据上.
这个例程展示了如何将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 文件



百度大联盟认证黄金会员Copyright© 1997- CNET Networks 版权所有。 ZDNet 是CNET Networks公司注册服务商标。
中华人民共和国电信与信息服务业务经营许可证编号:京ICP证010391号 京ICP备09041801号-159
京公网安备:1101082134