C#(5):XSLT可扩展样式表语言转换 System.Xml.Xsl、XslCompiledTransform类



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>

一、转为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、转换

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

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

3、结果

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>

二、转为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、转换XsltArgumentList.AddExtensionObject

在以下示例中,样式表使用 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;
    }
  }
}

3、结果

output.xml

三 、调用XSL参数

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、转换

下面的示例使用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);
        }
    }
}

四、使用 XML 控件

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

 ID="Xml1" runat="server" DocumentSource="DvdList.xml"    TransformSource="DvdList.xslt">

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

posted on 2020-05-14 16:40  springsnow  阅读(354)  评论(0编辑  收藏  举报

导航