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)換的書(shū)籍價(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方法來(lái)創(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 控件在頁(yè)面獨(dú)立的部分顯示 XSL 轉(zhuǎn)換后的結(jié)果:
ID="Xml1" runat="server" DocumentSource="DvdList.xml" TransformSource="DvdList.xslt">
注意: 你也可以用編碼中XmlDocument 對(duì)象賦給 Document 屬性,或者把一個(gè)包含 XML 內(nèi)容的字符串賦給 DocumentContent 屬性,而不是使用 DocumentSource 屬性。類(lèi)似的,你可以一個(gè) XslTransform 對(duì)象值賦給 Transform 屬性來(lái)提供 XSLT 信息。
到此這篇關(guān)于C#使用XSLT實(shí)現(xiàn)xsl、xml與html相互轉(zhuǎn)換的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C#實(shí)現(xiàn)實(shí)體類(lèi)和XML的相互轉(zhuǎn)換
- C# XML字符串包含特殊字符的處理轉(zhuǎn)換方法小結(jié)
- C#實(shí)現(xiàn)實(shí)體類(lèi)和XML相互轉(zhuǎn)換
- C#實(shí)現(xiàn)XML與實(shí)體類(lèi)之間相互轉(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#如何提取經(jīng)緯度文件中的經(jīng)緯度數(shù)據(jù)
近期開(kāi)發(fā)時(shí)需要獲取當(dāng)前的經(jīng)緯度坐標(biāo),下面這篇文章主要給大家介紹了關(guān)于C#如何提取經(jīng)緯度文件中經(jīng)緯度數(shù)據(jù)的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
簡(jiǎn)介Winform中創(chuàng)建用戶控件
用戶控件可以讓開(kāi)發(fā)人員對(duì)VS控件進(jìn)行組裝。下面我們來(lái)創(chuàng)建一個(gè)按鈕的用戶控件我們可以給它添加屬性,并且添加相應(yīng)鼠標(biāo)移入、移出事件。2013-03-03
淺析.NET中AsyncLocal的實(shí)現(xiàn)原理
這篇文章主要為大家詳細(xì)介紹了.NET中AsyncLocal的具體實(shí)現(xiàn)原理,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,如果有講得不清晰或不準(zhǔn)確的地方,還望指出2023-08-08
C#實(shí)現(xiàn)六大設(shè)計(jì)原則之迪米特法則
這篇文章介紹了C#實(shí)現(xiàn)六大設(shè)計(jì)原則之迪米特法則的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-02-02
C#自定義鼠標(biāo)拖拽Drag&Drop效果之基本原理及基本實(shí)現(xiàn)代碼
拖拽效果無(wú)論是在系統(tǒng)上、應(yīng)用上、還是在網(wǎng)頁(yè)上,拖拽隨處可見(jiàn),下面通過(guò)本文介紹下C#自定義鼠標(biāo)拖拽Drag&Drop效果之基本原理及基本實(shí)現(xiàn)代碼,需要的朋友可以參考下2022-04-04
C#將國(guó)產(chǎn)Linux視頻錄制生成mp4的具體實(shí)現(xiàn)
這篇文章主要介紹了C#將國(guó)產(chǎn)Linux視頻錄制生成mp4的具體實(shí)現(xiàn),文中通過(guò)代碼示例介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-08-08

