对DataSet应用XSLT转换

简介:

.NET Framework Developer's Guide  

Applying an XSLT Transform to a DataSet

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

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

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

< xsl:template  match ="CustomerOrders" >
   < HTML >
   < STYLE >
  BODY {font-family:verdana;font-size:9pt}
  TD   {font-size:8pt}
   </ STYLE >
     < BODY >
     < TABLE  BORDER ="1" >
       < xsl:apply-templates  select ="Customers" />
     </ TABLE >
     </ BODY >
   </ HTML >
</ 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 >


The following code is the code to fill the DataSet and apply the XSLT style sheet.

Note   If the  DataSet that you are applying an XSLT style sheet to contains relations, you will achieve best performance if you set the  Nested property of the  DataRelation to  true for each nested relation. This allows you to use XSLT style sheets that implement natural top-down processing to navigate the hierarchy and transform the data, as opposed to using performance-intensive XPath location axes (for example, preceding-sibling and following-sibling in style sheet node test expressions) to navigate the data hierarchy. For more information on nested relations, see  Nested DataRelations.


    
    
[Visual Basic]
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Xml
Imports System.Xml.Xsl

Public  Class Sample
  Public Shared Sub Main()
    Dim nwindConn As SqlConnection = New SqlConnection("Data Source=localhost;Initial Catalog=northwind;Integrated Security=SSPI")
    nwindConn.Open()

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

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

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

    nwindConn.Close()

    myDataSet.Relations.Add("CustOrders", _
                            myDataSet.Tables("Customers").Columns("CustomerID"), _
                            myDataSet.Tables("Orders").Columns("CustomerID")).Nested = true

    Dim xmlDoc As XmlDataDocument = New XmlDataDocument(myDataSet) 
      
    Dim xslTran As XslTransform = New XslTransform
    xslTran.Load("transform.xsl")
             
    Dim writer As XmlTextWriter = New XmlTextWriter("xslt_output.html", System.Text.Encoding.UTF8)

    xslTran.Transform(xmlDoc, Nothing, writer)
    writer.Close()
  End Sub

End Class



[C#]
using System;
using System.Data;
using System.Data.SqlClient;
using System.Xml;
using System.Xml.Xsl;

public  class Sample
{
  public static void Main()
  {
    SqlConnection nwindConn = new SqlConnection("Data Source=localhost;Initial Catalog=northwind;Integrated Security=SSPI;");
    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, writer);
    writer.Close();
  }

} 本文转自斯克迪亚博客园博客,原文链接:http://www.cnblogs.com/sgsoft/archive/2004/08/25/36309.html,如需转载请自行联系原作者
相关文章
|
8月前
|
人工智能 前端开发 关系型数据库
过年了,用魔搭+魔笔打造您的 AI 春节贺卡生成器!
本文介绍了如何获取和利用现有的大模型资源,结合魔笔低代码,低成本、高效率地打造一个 AI 春节贺卡生成器。
|
9月前
|
域名解析 UED SEO
如何选择网站模板建设网站?
本文主要介绍了网站模板的相关知识,包括什么是网站模板、选择模板的标准、使用网站模板建设网站的方法和部署步骤等。同时,文章强调了选择高品质模板的重要性,并提供了丰富的模板界面和搜索引擎优化元素,帮助用户快速搭建网站并提升用户体验。
378 8
|
10月前
|
安全 搜索推荐 Android开发
揭秘安卓与iOS系统的差异:技术深度对比
【10月更文挑战第27天】 本文深入探讨了安卓(Android)与iOS两大移动操作系统的技术特点和用户体验差异。通过对比两者的系统架构、应用生态、用户界面、安全性等方面,揭示了为何这两种系统能够在市场中各占一席之地,并为用户提供不同的选择。文章旨在为读者提供一个全面的视角,理解两种系统的优势与局限,从而更好地根据自己的需求做出选择。
1003 2
|
10月前
|
机器学习/深度学习 传感器 人工智能
深度学习中的图像识别技术及其应用
在人工智能的浪潮中,深度学习已经成为推动技术创新的核心力量。本文将深入探讨深度学习在图像识别领域的应用,从基本原理到实践案例,展示如何通过神经网络模型实现高效准确的图像处理。我们将一起探索卷积神经网络(CNN)的奥秘,并通过实际代码示例,了解如何训练和部署这些模型来解决现实世界的问题。无论你是深度学习的初学者还是希望深化理解的开发者,这篇文章都将为你提供价值丰富的知识和技能。
|
JavaScript 前端开发 Java
《手把手教你》系列技巧篇(二十四)-java+ selenium自动化测试-三大延时等待(详细教程)
【4月更文挑战第16天】本文介绍了Selenium的三种等待方式:硬性等待、隐式等待和显式等待。硬性等待是指无论页面是否加载完成,都会等待指定时间后再执行下一步;隐式等待是在整个会话中设置一个全局等待时间,如果元素在规定时间内出现则执行,否则继续等待;显式等待是更加灵活的等待方式,可以指定特定条件,如元素可见、可点击等,只有当条件满足时才会执行下一步。
271 7
|
数据采集 机器学习/深度学习 存储
DataWorks
DataWorks是阿里云推出的一款数据集成、数据开发和数据运维平台,旨在帮助企业轻松实现数据开发、数据集成、数据管理和数据安全等方面的需求。
429 0
|
监控 前端开发 JavaScript
关于 LowCode&ProCode 混合研发的思考
关于 LowCode&ProCode 混合研发的思考
1673 0
关于 LowCode&ProCode 混合研发的思考
|
存储 SQL 缓存
分布式 PostgreSQL - Citus 架构及概念
分布式 PostgreSQL - Citus 架构及概念
1056 0
分布式 PostgreSQL - Citus 架构及概念
|
存储 人工智能 数据可视化
猫狗识别经典案例:Serverless助力模型“升级”
众所周知,在人工智能领域,一些训练好的模型可能会随着时间的发展,数据集的完善也会需要进行升级。例如某公司的人脸识别系统,可能会因为新员工的入职,老员工的离职要进行模型的升级迭代;再或者某分类模型可能在上线之初由于训练集的不完善,导致识别率并不高,但是在实际使用过程中不断的有用户进行问题反馈,进而不断的对模型进行完善,提升模型最终的准确率。
391 0
猫狗识别经典案例:Serverless助力模型“升级”