这篇“C#怎么使用XSLT实现xsl、xml与html相互转换”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“C#怎么使用XS
这篇“C#怎么使用XSLT实现xsl、xml与html相互转换”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“C#怎么使用XSLT实现xsl、xml与html相互转换”文章吧。
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>
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>
将books.xml按照books.xsl定义的格式转换成out.html
XslCompiledTransform trans = new XslCompiledTransform(); trans.Load(@"..\..\books.xsl"); trans.Transform(@"..\..\books.xml", "out.html");
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>
<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>
在以下示例中,样式表使用 XSLT 扩展对象要转换的书籍价格。
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; } }}
output.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>
<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>
下面的示例使用AddParam方法来创建表示当前日期和时间的参数。
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); } }}
有时候你可能希望把带有其他内容的转换后的 HTML 输出和 WEB 控件组合在一起,XML 控件在页面独立的部分显示 XSL 转换后的结果:
ID="Xml1" runat="server" DocumentSource="DvdList.xml" TransformSource="DvdList.xslt">
注意: 你也可以用编码中XmlDocument 对象赋给 Document 属性,或者把一个包含 XML 内容的字符串赋给 DocumentContent 属性,而不是使用 DocumentSource 属性。类似的,你可以一个 XslTransform 对象值赋给 Transform 属性来提供 XSLT 信息。
以上就是关于“C#怎么使用XSLT实现xsl、xml与html相互转换”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注编程网精选频道。
--结束END--
本文标题: C#怎么使用XSLT实现xsl、xml与html相互转换
本文链接: https://www.lsjlt.com/news/330893.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-13
2024-05-13
2024-05-13
2024-05-13
2024-05-13
2024-05-13
2024-05-13
2024-05-13
2024-05-13
2024-05-13
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0