天天看點

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

.NET Framework Developer's Guide

The <b>WriteXml</b> method of the <b>DataSet</b> enables you to write the contents of a <b>DataSet</b> as XML data. A common task is to then transform that XML to another format using XSL Transformations (XSLT). However, synchronizing a <b>DataSet</b> with an <b>XmlDataDocument</b> enables you to apply an XSLT stylesheet to the contents of a <b>DataSet</b> without having to first write the contents of the <b>DataSet</b> as XML data using <b>WriteXml</b>.

The following example populates a <b>DataSet</b> with tables and relationships, synchronizes the <b>DataSet</b> with an <b>XmlDataDocument</b>, and writes a portion of the <b>DataSet</b> as an HTML file using an XSLT stylesheet. Following are the contents of the XSLT stylesheet.

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

&lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"&gt;

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet
對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

&lt;xsl:template match="CustomerOrders"&gt;

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

  &lt;HTML&gt;

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

  &lt;STYLE&gt;

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

  BODY {font-family:verdana;font-size:9pt}

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

  TD   {font-size:8pt}

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

  &lt;/STYLE&gt;

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

    &lt;BODY&gt;

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

    &lt;TABLE BORDER="1"&gt;

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

      &lt;xsl:apply-templates select="Customers"/&gt;

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

    &lt;/TABLE&gt;

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

    &lt;/BODY&gt;

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

  &lt;/HTML&gt;

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

&lt;/xsl:template&gt;

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet
對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

&lt;xsl:template match="Customers"&gt;

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

    &lt;TR&gt;&lt;TD&gt;

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

      &lt;xsl:value-of select="ContactName"/&gt;, &lt;xsl:value-of select="Phone"/&gt;&lt;BR/&gt;

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

    &lt;/TD&gt;&lt;/TR&gt;

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

      &lt;xsl:apply-templates select="Orders"/&gt;

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet
對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet
對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

&lt;xsl:template match="Orders"&gt;

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

  &lt;TABLE BORDER="1"&gt;

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

    &lt;TR&gt;&lt;TD valign="top"&gt;&lt;B&gt;Order:&lt;/B&gt;&lt;/TD&gt;&lt;TD valign="top"&gt;&lt;xsl:value-of select="OrderID"/&gt;&lt;/TD&gt;&lt;/TR&gt;

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

    &lt;TR&gt;&lt;TD valign="top"&gt;&lt;B&gt;Date:&lt;/B&gt;&lt;/TD&gt;&lt;TD valign="top"&gt;&lt;xsl:value-of select="OrderDate"/&gt;&lt;/TD&gt;&lt;/TR&gt;

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

    &lt;TR&gt;&lt;TD valign="top"&gt;&lt;B&gt;Ship To:&lt;/B&gt;&lt;/TD&gt;

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

        &lt;TD valign="top"&gt;&lt;xsl:value-of select="ShipName"/&gt;&lt;BR/&gt;

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

        &lt;xsl:value-of select="ShipAddress"/&gt;&lt;BR/&gt;

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

        &lt;xsl:value-of select="ShipCity"/&gt;, &lt;xsl:value-of select="ShipRegion"/&gt;  &lt;xsl:value-of select="ShipPostalCode"/&gt;&lt;BR/&gt;

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

        &lt;xsl:value-of select="ShipCountry"/&gt;&lt;/TD&gt;&lt;/TR&gt;

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

  &lt;/TABLE&gt;

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet
對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet
對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

&lt;/xsl:stylesheet&gt;

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

The following code is the code to fill the <b>DataSet</b> and apply the XSLT style sheet.

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

[Visual Basic]

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

Imports System

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

Imports System.Data

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

Imports System.Data.SqlClient

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

Imports System.Xml

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

Imports System.Xml.Xsl

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet
對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

Public Class Sample

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

  Public Shared Sub Main()

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

    Dim nwindConn As SqlConnection = New SqlConnection("Data Source=localhost;Initial Catalog=northwind;Integrated Security=SSPI")

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

    nwindConn.Open()

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet
對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

    Dim myDataSet As DataSet = New DataSet("CustomerOrders")

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet
對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

    Dim custDA As SqlDataAdapter = New SqlDataAdapter("SELECT * FROM Customers", nwindConn)

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

    custDA.Fill(myDataSet, "Customers")

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet
對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

    Dim ordersDA As SqlDataAdapter = New SqlDataAdapter("SELECT * FROM Orders", nwindConn)

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

    ordersDA.Fill(myDataSet, "Orders")

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet
對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

    nwindConn.Close()

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet
對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

    myDataSet.Relations.Add("CustOrders", _

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

                            myDataSet.Tables("Customers").Columns("CustomerID"), _

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

                            myDataSet.Tables("Orders").Columns("CustomerID")).Nested = true

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet
對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

    Dim xmlDoc As XmlDataDocument = New XmlDataDocument(myDataSet) 

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet
對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

    Dim xslTran As XslTransform = New XslTransform

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

    xslTran.Load("transform.xsl")

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet
對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

    Dim writer As XmlTextWriter = New XmlTextWriter("xslt_output.html", System.Text.Encoding.UTF8)

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet
對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

    xslTran.Transform(xmlDoc, Nothing, writer)

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

    writer.Close()

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

  End Sub

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

End Class

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet
對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

[C#]

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

using System;

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

using System.Data;

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

using System.Data.SqlClient;

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

using System.Xml;

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

using System.Xml.Xsl;

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet
對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

public class Sample

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

{

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

  public static void Main()

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

  {

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

    SqlConnection nwindConn = new SqlConnection("Data Source=localhost;Initial Catalog=northwind;Integrated Security=SSPI;");

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

    nwindConn.Open();

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet
對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

    DataSet custDS = new DataSet("CustomerDataSet");

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet
對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

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

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

    custDA.Fill(custDS, "Customers");

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet
對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

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

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

    ordersDA.Fill(custDS, "Orders");

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet
對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

    nwindConn.Close();

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet
對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

    custDS.Relations.Add("CustOrders",

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

                         custDS.Tables["Customers"].Columns["CustomerID"],

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

                         custDS.Tables["Orders"].Columns["CustomerID"]).Nested = true;

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet
對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

    XmlDataDocument xmlDoc = new XmlDataDocument(custDS); 

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet
對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

    XslTransform xslTran = new XslTransform();

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

    xslTran.Load("transform.xsl");

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet
對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

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

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet
對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

    xslTran.Transform(xmlDoc, null, writer);

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

    writer.Close();

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

  }

對DataSet應用XSLT轉換 Applying an XSLT Transform to a DataSet

}

本文轉自斯克迪亞部落格園部落格,原文連結:http://www.cnblogs.com/sgsoft/archive/2004/08/25/36309.html,如需轉載請自行聯系原作者