xslt应用系列四 .net之应用篇初级

xslt应用系列四 .net之应用篇初级

------- 转换方法--------

public void Transform()
    {
        SqlConnection nwindConn = new SqlConnection("Data Source=192.168.1.195;database=northwind;uid=sa;pwd=hujie;");
        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);
        //custDS.WriteXml(@"d:/me/net/2005/transform.xml");
        XslTransform xslTran = new XslTransform();
        xslTran.Load(@"d:/me/net/2005/transform.xsl");
        // XmlTextWriter writer = new XmlTextWriter("xslt_output.html",  System.Text.Encoding.UTF8);
        xslTran.Transform(xmlDoc, null, Response.OutputStream);
        // writer.Close();
    }

--------转换用到的xsl文件------

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="CustomerOrders">
    <STYLE>
      BODY {font-family:verdana;font-size:9pt}
      TD {font-size:8pt}
    </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>

-------

建一个aspx页面调用上面的方法即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值