XSLT與.NET

 在Net中主要使用XslCompiledTransform類進行編譯樣式表並執行XSLT 轉換.
在下面示例以一個WebService進行示例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
using System.Xml;
using System.Xml.Xsl;
using System.IO;
using System.Text;

namespace xslt
{
    /// <summary>
    /// WebService1 的摘要說明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允許使用 ASP.NET AJAX 從腳本中調用此 Web 服務,請取消對下行的註釋。
    // [System.Web.Script.Services.ScriptService]
    public class Products : System.Web.Services.WebService
    {
        [WebMethod]
        public string GetProduct()
        {
            SqlConnection conn = new SqlConnection(getConnectionString());
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "usp_GetProductsXmlXPath";
            conn.Open();
            XmlReader reader = cmd.ExecuteXmlReader();
            reader.Read();
            string s = reader.ReadOuterXml();
            reader.Close();
            conn.Close();
            //transform
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(s);
            //xslt進行轉換xml
            XslCompiledTransform ct = new XslCompiledTransform();           
            ct.Load(Server.MapPath("~/ProductXmlXPath.xslt"));
            MemoryStream ms = new MemoryStream();
            ct.Transform(doc, null, ms);
            return Encoding.UTF8.GetString(ms.ToArray());
        }

        public static string getConnectionString()
        {
            string ConnectionString;
            ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ToString();
            return ConnectionString;
        }
    }
}


Xslt文件如下:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="/">
        <table style="background-color:lightgreen">
          <xsl:for-each select="./Products/Category">
          <tr>
            <td style="border:solid 1px blank;">
             <xsl:apply-templates select="ProductName"/>
            </td>
            <td style="border:solid 1px blank;">
              <xsl:apply-templates select="UnitPrice"/>
            </td>
          </tr>
          </xsl:for-each>
        </table>
    </xsl:template>
</xsl:stylesheet>
usp_GetProductsXmlXPath過程
CREATE PROCEDURE [dbo].[usp_GetProductsXmlXPath]
AS
  
  SELECT [ProductID]
      ,[ProductName]
      ,[SupplierID]
      ,[CategoryID]
      ,[QuantityPerUnit]
      ,[UnitPrice]
  FROM [Products] 
  FOR XML Path('Category'),ROOT('Products') 


 

發佈了81 篇原創文章 · 獲贊 0 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章