从sql2005读取数据库通过xslt送显

xslt样式:
 1<?xml version="1.0" encoding="utf-8"?>
 2
 3<xsl:stylesheet version="1.0"
 4    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 5    <xsl:output method="html" />
 6    <xsl:param name="BackGroundColor" select="Blue" />
 7<xsl:template match="/">
 8    <HTML>
 9        <HEAD>
10            <TITLE>xslt测试</TITLE>
11        </HEAD>
12        <BODY>
13            <table border="1" cellSpacing="1" cellPadding="1">
14                <center>
15                    <xsl:for-each select="//music">
16                        <xsl:element name="tr">
17                            <xsl:attribute name="bgcolor">
18                                <xsl:value-of select="$Color" />
19                            </xsl:attribute>
20                            <xsl:element name="td">
21                                <xsl:value-of select="gs" />
22                            </xsl:element>
23                            <xsl:element name="td">
24                                <xsl:value-of select="gname" />
25                            </xsl:element>
26                            <xsl:element name="td">
27                                <xsl:attribute name="align">center</xsl:attribute>
28                                <xsl:value-of select="url" />
29                            </xsl:element>
30                        </xsl:element>
31                    </xsl:for-each>
32                </center>
33            </table>
34        </BODY>
35    </HTML>
36</xsl:template>
37</xsl:stylesheet>

页面文件中:
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.Xsl" %>
<%@ Import Namespace="System.Xml.XPath" %>
<%@ Import Namespace="System.Web.Configuration" %>

<script runat="server">                
  
void Page_Load(object sender, System.EventArgs e)
  
{      
      
string connString = WebConfigurationManager.ConnectionStrings["adventureWorks"].ConnectionString; //从WEB.CONFIG读数据库字符连接串
      using (SqlConnection conn = new SqlConnection(connString))
      
{
          conn.Open();
          SqlCommand command 
= new SqlCommand("Select top 5 * from music for xml auto,elements", conn);          
          XmlReader reader 
= command.ExecuteXmlReader();
          XPathDocument xpathDoc 
= new XPathDocument(reader); //读取数据并以XML形式返回
          conn.Close();
          
string xslPath = Server.MapPath("xsl/music.xsl"); //给XML加样式       
          XslCompiledTransform transform = new XslCompiledTransform();          
          transform.Load(xslPath);
          XsltArgumentList argsList 
= new XsltArgumentList();
          
string backGroundColor = "Tan";
          argsList.AddParam(
"Color""", backGroundColor); //给样式附值
          transform.Transform(xpathDoc, argsList, Response.Output);  
          
      }

  }
        
</script>

posted on 2006-07-05 14:48  苍冥  阅读(511)  评论(0编辑  收藏  举报

导航