最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#使用XSLT實現(xiàn)xsl、xml與html相互轉換

 更新時間:2022年06月06日 11:22:34   作者:springsnow  
這篇文章介紹了C#使用XSLT實現(xiàn)xsl、xml與html相互轉換的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

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參數(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、轉換

下面的示例使用AddParam方法來創(chuàng)建表示當前日期和時間的參數(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 控件

有時候你可能希望把帶有其他內容的轉換后的 HTML 輸出和 Web 控件組合在一起,XML 控件在頁面獨立的部分顯示 XSL 轉換后的結果:

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

注意: 你也可以用編碼中XmlDocument 對象賦給 Document 屬性,或者把一個包含 XML 內容的字符串賦給 DocumentContent 屬性,而不是使用 DocumentSource 屬性。類似的,你可以一個 XslTransform 對象值賦給 Transform 屬性來提供 XSLT 信息。

到此這篇關于C#使用XSLT實現(xiàn)xsl、xml與html相互轉換的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • C#如何提取經(jīng)緯度文件中的經(jīng)緯度數(shù)據(jù)

    C#如何提取經(jīng)緯度文件中的經(jīng)緯度數(shù)據(jù)

    近期開發(fā)時需要獲取當前的經(jīng)緯度坐標,下面這篇文章主要給大家介紹了關于C#如何提取經(jīng)緯度文件中經(jīng)緯度數(shù)據(jù)的相關資料,文中通過圖文介紹的非常詳細,需要的朋友可以參考下
    2022-08-08
  • 簡介Winform中創(chuàng)建用戶控件

    簡介Winform中創(chuàng)建用戶控件

    用戶控件可以讓開發(fā)人員對VS控件進行組裝。下面我們來創(chuàng)建一個按鈕的用戶控件我們可以給它添加屬性,并且添加相應鼠標移入、移出事件。
    2013-03-03
  • 淺聊一下C#中內存映射文件的玩法

    淺聊一下C#中內存映射文件的玩法

    內存映射文件是怎么玩的,說實話這東西理論我相信很多朋友都知道,就是將文件映射到進程的虛擬地址,說起來很容易,那如何讓大家眼見為實呢,本文就來和大家簡單聊聊
    2023-06-06
  • 淺析.NET中AsyncLocal的實現(xiàn)原理

    淺析.NET中AsyncLocal的實現(xiàn)原理

    這篇文章主要為大家詳細介紹了.NET中AsyncLocal的具體實現(xiàn)原理,文中的示例代碼講解詳細,具有一定的借鑒價值,如果有講得不清晰或不準確的地方,還望指出
    2023-08-08
  • C#實現(xiàn)六大設計原則之迪米特法則

    C#實現(xiàn)六大設計原則之迪米特法則

    這篇文章介紹了C#實現(xiàn)六大設計原則之迪米特法則的方法,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-02-02
  • C#利用NPOI操作Excel(單元格設置)

    C#利用NPOI操作Excel(單元格設置)

    這篇文章主要為大家詳細介紹了C#利用NPOI操作Excel實現(xiàn)單元格設置,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • unity實現(xiàn)貪吃蛇游戲

    unity實現(xiàn)貪吃蛇游戲

    這篇文章主要為大家詳細介紹了unity實現(xiàn)貪吃蛇游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • Unity查找游戲物體的六種方式詳解

    Unity查找游戲物體的六種方式詳解

    最近學習unity3d做游戲,總結了一些實用的內容,所以下面這篇文章主要給大家介紹了關于Unity查找游戲物體的六種方式,需要的朋友可以參考下
    2021-06-06
  • C#自定義鼠標拖拽Drag&Drop效果之基本原理及基本實現(xiàn)代碼

    C#自定義鼠標拖拽Drag&Drop效果之基本原理及基本實現(xiàn)代碼

    拖拽效果無論是在系統(tǒng)上、應用上、還是在網(wǎng)頁上,拖拽隨處可見,下面通過本文介紹下C#自定義鼠標拖拽Drag&Drop效果之基本原理及基本實現(xiàn)代碼,需要的朋友可以參考下
    2022-04-04
  • C#將國產(chǎn)Linux視頻錄制生成mp4的具體實現(xiàn)

    C#將國產(chǎn)Linux視頻錄制生成mp4的具體實現(xiàn)

    這篇文章主要介紹了C#將國產(chǎn)Linux視頻錄制生成mp4的具體實現(xiàn),文中通過代碼示例介紹的非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下
    2024-08-08

最新評論

赣榆县| 赤壁市| 裕民县| 洛阳市| 宁乡县| 宁远县| 枝江市| 天镇县| 合江县| 兰州市| 平遥县| 南康市| 永兴县| 伊川县| 鲁山县| 辉南县| 荆州市| 黑河市| 怀安县| 和静县| 涟源市| 柳林县| 西平县| 江孜县| 雷山县| 屯门区| 旌德县| 白水县| 徐州市| 汶川县| 宜丰县| 宁河县| 宜州市| 武义县| 叙永县| 于田县| 呼伦贝尔市| 蒙城县| 福泉市| 托里县| 固原市|