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;
    }
  }
}

三 、调用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 信息。

到此这篇关于c#使用xslt实现xsl、xml与html相互转换的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持。