C#使用XSLT實(shí)現(xiàn)xsl、xml與html相互轉(zhuǎn)換
XML文件
books.xml:
<xml version="1.0" encoding="utf-8" ?> <bookstore> <book genre="autobiography" publicationdate="1991" ISBN="1-861003-11-0"> <title>The Autobiography of Benjamin Franklin</title> <author> <first-name>Benjamin</first-name> <last-name>Franklin</last-name> </author> <price>8.99</price> </book> <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2"> <title>The Confidence Man</title> <author> <first-name>Herman</first-name> <last-name>Melville</last-name> </author> <price>11.99</price> </book> <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6"> <title>The Gorgias</title> <author> <name>Plato</name> </author> <price>9.99</price> </book> </bookstore>
一、轉(zhuǎn)為html文檔
1、xsl文件
books.xsl:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <HTML> <head> <title>Price List</title> </head> <body> <table> <xsl:apply-templates/> </table> </body> </HTML> </xsl:template> <xsl:template match="bookstore"> <xsl:apply-templates select="book"/> </xsl:template> <xsl:template match="book"> <tr> <td> <xsl:value-of select="title"/> </td> <td> <xsl:value-of select="price"/> </td> </tr> </xsl:template> </xsl:stylesheet>
2、轉(zhuǎn)換
將books.xml按照books.xsl定義的格式轉(zhuǎn)換成out.html
XslCompiledTransform trans = new XslCompiledTransform(); trans.Load(@"..\..\books.xsl"); trans.Transform(@"..\..\books.xml", "out.html");
3、結(jié)果
out.html:
<HTML> <head> <META http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Price List</title> </head> <body> <table> <tr> <td>The Autobiography of Benjamin Franklin</td> <td>8.99</td> </tr> <tr> <td>The Confidence Man</td> <td>11.99</td> </tr> <tr> <td>The Gorgias</td> <td>9.99</td> </tr> </table> </body> </HTML>
二、轉(zhuǎn)為xml文檔
1、prices.xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:myObj="urn:price-conv"> Price conversion factor <xsl:param name="conv" select="1.15"/> <xsl:template match="bookstore"> <bookstore> <xsl:for-each select="book"> <book> <xsl:copy-of select="node()"/> <new-price> <xsl:value-of select="myObj:NewPriceFunc(./price, $conv)"/> </new-price> </book> </xsl:for-each> </bookstore> </xsl:template> </xsl:stylesheet>
2、轉(zhuǎn)換XsltArgumentList.AddExtensionObject
在以下示例中,樣式表使用 XSLT 擴(kuò)展對(duì)象要轉(zhuǎn)換的書籍價(jià)格。
using System; using System.IO; using System.Xml; using System.Xml.XPath; using System.Xml.Xsl; public class Sample { public static void Main() { // Create the XslCompiledTransform and load the stylesheet. XslCompiledTransform xslt = new XslCompiledTransform(); xslt.Load("prices.xsl"); // Create an XsltArgumentList. XsltArgumentList xslArg = new XsltArgumentList(); // Add an object to calculate the new book price. BookPrice obj = new BookPrice(); xslArg.AddExtensionObject("urn:price-conv", obj); using (XmlWriter w = XmlWriter.Create("output.xml")) { // Transform the file. xslt.Transform("books.xml", xslArg, w); } } // Convert the book price to a new price using the conversion factor. public class BookPrice{ private decimal newprice = 0; public decimal NewPriceFunc(decimal price, decimal conv){ decimal tmp = price*conv; newprice = decimal.Round(tmp, 2); return newprice; } } }
三 、調(diào)用XSL參數(shù)
1、xml文件
order.xml
Represents a customer order <order> <book ISBN='10-861003-324'> <title>The Handmaid's Tale</title> <price>19.95</price> </book> <cd ISBN='2-3631-4'> <title>Americana</title> <price>16.95</price> </cd> </order>
2、order.xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:param name="date"/> <xsl:template match="/"> <order> <date><xsl:value-of select="$date"/></date> <total><xsl:value-of select="sum(//price)"/>total> </order> </xsl:template> </xsl:stylesheet>
3、轉(zhuǎn)換
下面的示例使用AddParam方法來創(chuàng)建表示當(dāng)前日期和時(shí)間的參數(shù)。
using System; using System.IO; using System.Xml; using System.Xml.Xsl; public class Sample { public static void Main() { // Create the XslCompiledTransform and load the stylesheet. XslCompiledTransform xslt = new XslCompiledTransform(); xslt.Load("order.xsl"); // Create the XsltArgumentList. XsltArgumentList xslArg = new XsltArgumentList(); // Create a parameter which represents the current date and time. DateTime d = DateTime.Now; xslArg.AddParam("date", "", d.ToString()); // Transform the file. using (XmlWriter w = XmlWriter.Create("output.xml")) { xslt.Transform("order.xml", xslArg, w); } } }
四、使用 XML 控件
有時(shí)候你可能希望把帶有其他內(nèi)容的轉(zhuǎn)換后的 HTML 輸出和 Web 控件組合在一起,XML 控件在頁面獨(dú)立的部分顯示 XSL 轉(zhuǎn)換后的結(jié)果:
ID="Xml1" runat="server" DocumentSource="DvdList.xml" TransformSource="DvdList.xslt">
注意: 你也可以用編碼中XmlDocument 對(duì)象賦給 Document 屬性,或者把一個(gè)包含 XML 內(nèi)容的字符串賦給 DocumentContent 屬性,而不是使用 DocumentSource 屬性。類似的,你可以一個(gè) XslTransform 對(duì)象值賦給 Transform 屬性來提供 XSLT 信息。
到此這篇關(guān)于C#使用XSLT實(shí)現(xiàn)xsl、xml與html相互轉(zhuǎn)換的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C#實(shí)現(xiàn)實(shí)體類和XML的相互轉(zhuǎn)換
- C# XML字符串包含特殊字符的處理轉(zhuǎn)換方法小結(jié)
- C#實(shí)現(xiàn)實(shí)體類和XML相互轉(zhuǎn)換
- C#實(shí)現(xiàn)XML與實(shí)體類之間相互轉(zhuǎn)換的方法(序列化與反序列化)
- C#實(shí)現(xiàn)將文件轉(zhuǎn)換為XML的方法
- C#中使用JSON.NET實(shí)現(xiàn)JSON、XML相互轉(zhuǎn)換
- C# XML與Json之間相互轉(zhuǎn)換實(shí)例詳解
- C#對(duì)象與XMl文件之間的相互轉(zhuǎn)換
相關(guān)文章
C#實(shí)現(xiàn).net頁面之間傳值傳參方法匯總
這篇文章主要介紹了C#實(shí)現(xiàn).net頁面之間傳值傳參方法,實(shí)例匯總了幾類常見的傳值傳參的方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-10-10