XML文件

books.xml:

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <bookstore>
  3. <book genre="autobiography" publicationdate="1991" ISBN="1-861003-11-0">
  4. <title>The Autobiography of Benjamin Franklin</title>
  5. <author>
  6. <first-name>Benjamin</first-name>
  7. <last-name>Franklin</last-name>
  8. </author>
  9. <price>8.99</price>
  10. </book>
  11. <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
  12. <title>The Confidence Man</title>
  13. <author>
  14. <first-name>Herman</first-name>
  15. <last-name>Melville</last-name>
  16. </author>
  17. <price>11.99</price>
  18. </book>
  19. <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
  20. <title>The Gorgias</title>
  21. <author>
  22. <name>Plato</name>
  23. </author>
  24. <price>9.99</price>
  25. </book>
  26. </bookstore>

转为html文档

1、xsl文件

books.xsl:

  1. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  2. <xsl:template match="/">
  3. <HTML>
  4. <head>
  5. <title>Price List</title>
  6. </head>
  7. <body>
  8. <table>
  9. <xsl:apply-templates/>
  10. </table>
  11. </body>
  12. </HTML>
  13. </xsl:template>
  14.  
  15. <xsl:template match="bookstore">
  16. <xsl:apply-templates select="book"/>
  17. </xsl:template>
  18.  
  19. <xsl:template match="book">
  20. <tr>
  21. <td>
  22. <xsl:value-of select="title"/>
  23. </td>
  24. <td>
  25. <xsl:value-of select="price"/>
  26. </td>
  27. </tr>
  28. </xsl:template>
  29. </xsl:stylesheet>

2、转换

将books.xml按照books.xsl定义的格式转换成out.html

  1. XslCompiledTransform trans = new XslCompiledTransform();
  2. trans.Load(@"..\..\books.xsl");
  3. trans.Transform(@"..\..\books.xml", "out.html");

3、结果

out.html:

  1. <HTML>
  2. <head>
  3. <META http-equiv="Content-Type" content="text/html; charset=utf-8">
  4. <title>Price List</title>
  5. </head>
  6. <body>
  7. <table>
  8. <tr>
  9. <td>The Autobiography of Benjamin Franklin</td>
  10. <td>8.99</td>
  11. </tr>
  12. <tr>
  13. <td>The Confidence Man</td>
  14. <td>11.99</td>
  15. </tr>
  16. <tr>
  17. <td>The Gorgias</td>
  18. <td>9.99</td>
  19. </tr>
  20. </table>
  21. </body>
  22. </HTML>

转为xml文档

1、prices.xsl

  1. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:myObj="urn:price-conv">
  2.  
  3. <!--Price conversion factor-->
  4. <xsl:param name="conv" select="1.15"/>
  5.  
  6. <xsl:template match="bookstore">
  7. <bookstore>
  8. <xsl:for-each select="book">
  9. <book>
  10. <xsl:copy-of select="node()"/>
  11. <new-price>
  12. <xsl:value-of select="myObj:NewPriceFunc(./price, $conv)"/>
  13. </new-price>
  14. </book>
  15. </xsl:for-each>
  16. </bookstore>
  17. </xsl:template>
  18. </xsl:stylesheet>

2、转换XsltArgumentList.AddExtensionObject

在以下示例中,样式表使用 XSLT 扩展对象要转换的书籍价格。

  1. using System;
  2. using System.IO;
  3. using System.Xml;
  4. using System.Xml.XPath;
  5. using System.Xml.Xsl;
  6.  
  7. public class Sample {
  8.  
  9. public static void Main() {
  10.  
  11. // Create the XslCompiledTransform and load the stylesheet.
  12. XslCompiledTransform xslt = new XslCompiledTransform();
  13. xslt.Load("prices.xsl");
  14.  
  15. // Create an XsltArgumentList.
  16. XsltArgumentList xslArg = new XsltArgumentList();
  17.  
  18. // Add an object to calculate the new book price.
  19. BookPrice obj = new BookPrice();
  20. xslArg.AddExtensionObject("urn:price-conv", obj);
  21.  
  22. using (XmlWriter w = XmlWriter.Create("output.xml"))
  23. {
  24. // Transform the file.
  25. xslt.Transform("books.xml", xslArg, w);
  26. }
  27. }
  28.  
  29. // Convert the book price to a new price using the conversion factor.
  30. public class BookPrice{
  31.  
  32. ;
  33.  
  34. public decimal NewPriceFunc(decimal price, decimal conv){
  35. decimal tmp = price*conv;
  36. newprice = );
  37. return newprice;
  38. }
  39. }
  40. }

3、结果

prices.xsl


  1. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:myObj="urn:price-conv">
  2.  
  3. <!--Price conversion factor-->
  4. <xsl:param name="conv" select="1.15"/>
  5.  
  6. <xsl:template match="bookstore">
  7. <bookstore>
  8. <xsl:for-each select="book">
  9. <book>
  10. <xsl:copy-of select="node()"/>
  11. <new-price>
  12. <xsl:value-of select="myObj:NewPriceFunc(./price, $conv)"/>
  13. </new-price>
  14. </book>
  15. </xsl:for-each>
  16. </bookstore>
  17. </xsl:template>
  18. </xsl:stylesheet>

调用XSL参数

1、xml文件

order.xml

  1. <!--Represents a customer order-->
  2. <order>
  3. <book ISBN='10-861003-324'>
  4. <title>The Handmaid's Tale</title>
  5. <price>19.95</price>
  6. </book>
  7. <cd ISBN='2-3631-4'>
  8. <title>Americana</title>
  9. <price>16.95</price>
  10. </cd>
  11. </order>

2、order.xsl

  1. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  2. <xsl:param name="date"/>
  3. <xsl:template match="/">
  4. <order>
  5. <date><xsl:value-of select="$date"/></date>
  6. <total><xsl:value-of select="sum(//price)"/></total>
  7. </order>
  8. </xsl:template>
  9. </xsl:stylesheet>

3、转换

下面的示例使用AddParam方法来创建表示当前日期和时间的参数。

  1. using System;
  2. using System.IO;
  3. using System.Xml;
  4. using System.Xml.Xsl;
  5.  
  6. public class Sample
  7. {
  8.  
  9. public static void Main()
  10. {
  11.  
  12. // Create the XslCompiledTransform and load the stylesheet.
  13. XslCompiledTransform xslt = new XslCompiledTransform();
  14. xslt.Load("order.xsl");
  15.  
  16. // Create the XsltArgumentList.
  17. XsltArgumentList xslArg = new XsltArgumentList();
  18.  
  19. // Create a parameter which represents the current date and time.
  20. DateTime d = DateTime.Now;
  21. xslArg.AddParam("date", "", d.ToString());
  22.  
  23. // Transform the file.
  24. using (XmlWriter w = XmlWriter.Create("output.xml"))
  25. {
  26. xslt.Transform("order.xml", xslArg, w);
  27. }
  28. }
  29. }

使用 XML 控件

有时候你可能希望把带有其他内容的转换后的 HTML 输出和 Web 控件组合在一起,XML 控件在页面独立的部分显示 XSL 转换后的结果:

  1. <asp:Xml ID="Xml1" runat="server" DocumentSource="DvdList.xml" TransformSource="DvdList.xslt"></asp:Xml>

这个示例最好的一点是只需设置 2 个属性而不需要手工书写任何代码。

不必为了使用 XML 控件而使用独立的文件。

你也可以在编码中把 XmlDocument 对象赋给 Document 属性,或者把一个包含 XML 内容的字符串赋给 DocumentContent 属性,而不是使用 DocumentSource 属性。类似的,你可以一个 XslTransform 对象值赋给 Transform 属性来提供 XSLT 信息。

如果需要编程来支持 XML 和 XSLT 数据(从数据库记录中抽取数据),这些技术将非常有用。

使用 LINQ to XML 转换 XML

XSL 并不是改变 XML 格式的唯一方式(过去它是唯一实际可行的办法),但今天,LINQ to XML 提供了一个富有竞争力的选择。要使用 LINQ to XML 进行转换,你需要一个运用投影的 LINQ 表达式。技巧在于投影必须返回一个 XElement 而不是匿名类型。

  1. string xmlFile = Server.MapPath("DvdList.xml");
  2. XDocument doc = XDocument.Load(xmlFile);
  3.  
  4. XDocument newDoc = new XDocument(
  5. new XDeclaration("1.0", "utf-8", "yes"),
  6. new XElement("Movies",
  7. from DVD in doc.Descendants("DVD")
  8.  
  9. select new XElement[]
  10. {
  11. new XElement ("Moive",
  12. new XAttribute("name", (string)DVD.Element("Title")),
  13. DVD.Descendants("Star")
  14. )
  15. }
  16. )
  17. );
  18.  
  19. string newFile = Server.MapPath("MovieList.xml");
  20. newDoc.Save(newFile);

结果:

  1. <?xml version="1.0" encoding="utf-8" standalone="yes"?>
  2. <Movies>
  3. <Moive name="The Matrix">
  4. <Star>Keanu Reeves</Star>
  5. <Star>Laurence Fishburne</Star>
  6. </Moive>
  7. <Moive name="Forrest Gump">
  8. <Star>Tom Hanks</Star>
  9. <Star>Robin Wright</Star>
  10. </Moive>
  11. </Movies>

基于 LINQ 转换的语法通常要比使用 XSL 样式表的转换更容易理解,并且更加精确你也可以很方便的替换为另一个 IEnumable<T>的集合,包括 LINQ to Entities 获得的结果集,然后随意打包成另一种格式的 XML 文档。

XSLT可扩展样式表语言转换 System.Xml.Xsl、XslCompiledTransform类的更多相关文章

  1. 【HTML/XML 8】XSL,可扩展样式表语言

    导读:上篇博客说了在XML文档中实现表现形式的一种形式:CSS层叠样式表,本篇博客将接着说明其另一种实现方式XSL,并将分析XSL和CSS之间的 关系. 一.XSL简介 XSL(eXtensible ...

  2. CSS篇-样式表、选择器、权重、伪类

    CSS定义 CSS:Cascading Style Sheet(层叠样式表) // 写法 选择器 { 属性名: 属性值; } CSS样式表 (1)三种样式表使用 // 内联样式 <div sty ...

  3. CSS样式表及选择器相关内容(二)-伪类与伪元素选择器

    伪类与伪元素选择器归纳: 一.伪类选择器(伪类以":"开头,用在选择器后,用于指明元素在某种特殊的状态下才能被选中)    1.a标签伪类选择器,其他标签类似        eg: ...

  4. 使用 XSLT 作为 HTML 的样式表

    简介 当听到样式表这个词时,您可能会想到 CSS 样式表.XSLT 样式表通常用于 XML 转换,比如在 Web 服务之间映射数据.因为 XSLT 非常适合此用途,所以创建了顶层元素 <styl ...

  5. 微软BI 之SSIS 系列 - XML Task 中XSLT 样式表转换错误记录

    开篇介绍 此文章专门记录 XSLT 样式表转换过程中的语法问题 错误一 值与属性的倒置 修改了几次样式表,但还是一如既往的报错,报错信息如下: [XML Task] Error: An error o ...

  6. System.Web.Optimization对脚本和样式表的压缩操作

    1 是否允许样式表压缩 BundleTable.EnableOptimizations = true; 在MVC项目中的 BundleConfig操作中是微软已经给我们准备好的CSS和JS压缩,我们可 ...

  7. System.Web.Optimization对脚本和样式表的操作

    这个也是本章重点向描述的部分,首先我们可以使用VS2012RC来新建一个MVC4.0项目,版本可以为4.0或4.5.在Global.asax文件代码中,我们发现已经把过滤器,路由器,以及对样式表和脚本 ...

  8. 程序员接触新语言————hello world ^-^,web3种样式表

    我的第一个网页 <!DOCTYPE html> <html> <head lang="en"> <meta charset="U ...

  9. CSS样式----图文详解:css样式表和选择器

    主要内容 CSS概述 CSS和HTML结合的三种方式:行内样式表.内嵌样式表.外部样式表 CSS四种基本选择器:标签选择器.类选择器.ID选择器.通用选择器 CSS三种扩展选择器:组合选择器.后代选择 ...

随机推荐

  1. eNSP上VLAN的基础的配置及access接口

    本实验模拟公司内部,为不同的部门划分不同的VLAN ,形成的不同广播域,来保护信息的安全,拓扑图如下所示

  2. Appium移动自动化测试-----(十)appium API 之上下文操作

    其实上下文的操作主要针对于混合应用.啥是混合应用,简单来说就是APP用里面嵌入网页.Android上的浏览器就属于混合应用. 1.获取当前上下文 方法: getContext() 获取当前所有的可用的 ...

  3. 在 FR 网络配置 OSPF

    一.环境准备 1. 软件:GNS3 2. 路由:c7200 二.实验操作 实验要求: 1.掌握配置帧中继的基本方法. 2.掌握在路由器中模拟帧中继交换机的方法. 3.掌握 NBMA 网络中 OSPF  ...

  4. LeetCode 671. 二叉树中第二小的节点(Second Minimum Node In a Binary Tree) 9

    671. 二叉树中第二小的节点 671. Second Minimum Node In a Binary Tree 题目描述 给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 ...

  5. Jenkins+maven+gitlab自动化部署之docker发布sprint boot项目(七)

    Jenkins发布docker应用与发布java应用配置基本一致,需要配置Dockerfile及构建的步骤,步骤如下: 1.jenkins主机构建应用为jar包 2.jenkins主机把生产的jar包 ...

  6. hashCode和identifyHashCode的区别

    API: System类提供一个identifyHashCode(Object o)的方法,该方法返回指定对象的精确hashCode值,也是根据该对象的地址计算得到的HashCode值.当某个类的ha ...

  7. 织梦/dedecms采集怎么去除a标签

    dedecms采集去除a标签代码 DedeCMS采集规则-过滤-替换-技巧2009-01-14 15:491.采集去除链接[Copy to clipboard]CODE:{dede:trim}]*)& ...

  8. 关于NumPy的常用函数random.randint

    np.random.randint(low, high=None, size=None, dtype='l') 该函数作用:用于产生离散均匀分布的整数 low:生成元素的最小值 high:生成元素的值 ...

  9. 基于UDP的编程

    前提:基于Linux系统的学习 服务器端编程模型1 socket(2) 创建通讯端点,返回一个文件描述符fd2 bind(2) 将fd绑定到本地的地址和端口while(1){ 阻塞等待客户端请求数据的 ...

  10. Android--Fragment嵌套的问题

    项目中遇到Fragment嵌套应用的问题 子Fragment中要用getChildFragmentManager()方法获取FragmentManager,否则会出问题!