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

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

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

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>

一、轉(zhuǎn)為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、轉(zhuǎn)換

將books.xml按照books.xsl定義的格式轉(zhuǎn)換成out.html

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

3、結(jié)果

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>

二、轉(zhuǎn)為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、轉(zhuǎn)換XsltArgumentList.AddExtensionObject

在以下示例中,樣式表使用 XSLT 擴(kuò)展對(duì)象要轉(zhuǎn)換的書籍價(jià)格。

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

三 、調(diào)用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、轉(zhuǎn)換

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

有時(shí)候你可能希望把帶有其他內(nèi)容的轉(zhuǎn)換后的 HTML 輸出和 Web 控件組合在一起,XML 控件在頁面獨(dú)立的部分顯示 XSL 轉(zhuǎn)換后的結(jié)果:

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

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

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

相關(guān)文章

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

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

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

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

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

    淺聊一下C#中內(nèi)存映射文件的玩法

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

最新評(píng)論

周至县| 江西省| 滨州市| 乐清市| 铜梁县| 会东县| 凤翔县| 平乐县| 长白| 汕头市| 册亨县| 嘉兴市| 五常市| 阿克苏市| 昌黎县| 钟山县| 长葛市| 中西区| 广昌县| 温泉县| 大新县| 盘锦市| 滦平县| 西畴县| 尚义县| 寻乌县| 临猗县| 健康| 景东| 桓仁| 廊坊市| 洪雅县| 平江县| 南阳市| 乌拉特后旗| 靖边县| 板桥市| 攀枝花市| 三江| 靖边县| 塔河县|