欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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# 實(shí)現(xiàn)繪制PDF嵌套表格案例詳解

    C# 實(shí)現(xiàn)繪制PDF嵌套表格案例詳解

    嵌套表格,顧名思義,就是在一張表格中的特定單元格中再插入一個(gè)或者多個(gè)表格,本文將為大家介紹C#繪制PDF嵌套表格的代碼示例,需要的同學(xué)可以參考一下
    2021-11-11
  • C#實(shí)現(xiàn)外排序的示例代碼

    C#實(shí)現(xiàn)外排序的示例代碼

    本文介紹了C#中的外排序技術(shù),以及如何使用C#實(shí)現(xiàn)外排序算法,通過使用排序算法,可以對(duì)大量數(shù)據(jù)進(jìn)行排序,并且可以有效地處理超大數(shù)據(jù)集,感興趣的可以了解下
    2023-11-11
  • C#加密解密類實(shí)例程序

    C#加密解密類實(shí)例程序

    這篇文章主要介紹了C#加密解密類實(shí)例程序,大家參考使用吧
    2013-12-12
  • c#窗體傳值用法實(shí)例詳解

    c#窗體傳值用法實(shí)例詳解

    這篇文章主要介紹了c#窗體傳值用法,以實(shí)例形式較為詳細(xì)的分析了C#窗體傳值的各種常用技巧,需要的朋友可以參考下
    2015-06-06
  • C#基于NPOI操作Excel

    C#基于NPOI操作Excel

    這篇文章介紹了C#基于NPOI操作Excel的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-04-04
  • C#數(shù)字圖象處理之圖像灰度化方法

    C#數(shù)字圖象處理之圖像灰度化方法

    這篇文章主要介紹了C#數(shù)字圖象處理之圖像灰度化方法,涉及C#基于Bitmap類操作圖像的相關(guān)技巧,需要的朋友可以參考下
    2015-04-04
  • C#實(shí)現(xiàn).net頁面之間傳值傳參方法匯總

    C#實(shí)現(xiàn).net頁面之間傳值傳參方法匯總

    這篇文章主要介紹了C#實(shí)現(xiàn).net頁面之間傳值傳參方法,實(shí)例匯總了幾類常見的傳值傳參的方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2014-10-10
  • c#中switch case的用法實(shí)例解析

    c#中switch case的用法實(shí)例解析

    這篇文章主要介紹了c#中switch case的用法實(shí)例解析,對(duì)于C#的初學(xué)者來說有必要熟練掌握,需要的朋友可以參考下
    2014-08-08
  • C#設(shè)計(jì)模式之外觀模式介紹

    C#設(shè)計(jì)模式之外觀模式介紹

    外觀模式:為子系統(tǒng)中的一組接口提供一個(gè)一致的界面,此模式定義了一個(gè)高層的接口,這個(gè)借口使得這子系統(tǒng)容易使用
    2012-10-10
  • C#過濾sql特殊字符串的方法

    C#過濾sql特殊字符串的方法

    這篇文章介紹了C#過濾sql特殊字符串的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-07-07

最新評(píng)論