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

解析在.net中使用XSLT轉(zhuǎn)換xml文檔的示例詳解

 更新時(shí)間:2013年05月18日 16:46:03   作者:  
本篇文章是對(duì)在.net中使用XSLT轉(zhuǎn)換xml文檔的示例進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
XSL即可擴(kuò)展的樣式表文件。 可以格式化xml的顯示,也可以將xml轉(zhuǎn)換成需要的另一種格式。
學(xué)習(xí)XSL必須熟悉XPath。XSL和XPath一樣簡(jiǎn)單強(qiáng)大,容易學(xué)習(xí)。
1. XSL既然可以格式化xml的顯示樣式,我們先來看如何在xml中引用xsl文件
如下代碼示例:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="url.xsl"?>
只需在xml文件的文檔聲明后面添加<?xml-stylesheet type=”text/xsl” href=”url.xsl”?>即可
2. XSL的格式
XSL也是一個(gè)標(biāo)準(zhǔn)的xml文件,它以xml文檔聲明開始,根元素必須是xsl:styleshee,同時(shí)根元素必須有version屬性指定xsl的版本,和xmlns:xsl=” http://www.w3.org/1999/XSL/Transform”指定xsl命名空間,如下示例
<?xml version="1.0" encoding="encoding”?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
3. Xsl要點(diǎn) 如下示例xml
復(fù)制代碼 代碼如下:

<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet  type="text/xsl" href="pets-templates.xsl"?>
<pets>
  <pig color="blue" weight="100">
    <price>100</price>
    <desc>this is a blue pig</desc>
  </pig>
  <cat color="red" weight="9">
    <price>80</price>
    <desc>this is a red cat</desc>
  </cat>
  <dog color="green" weight="15">
    <price>80</price>
    <desc>this is a green dog</desc>
  </dog>
  <cat color="green" weight="15">
    <price>80</price>
    <desc>this is a green cat</desc>
  </cat>

 
  <dog color="blue" weight="10">
    <price>100</price>
    <desc>this is a blue dog</desc>
  </dog>
  <dog color="red" weight="9">
    <price>80</price>
    <desc>this is a red dog</desc>
  </dog>
</pets>

上面的xml在通過xsl格式化之后的顯示效果如下:


1) xsl:template定義匹配節(jié)點(diǎn)的轉(zhuǎn)換模板,屬性match=”xpath expression”用來定義模板匹配的元素
如下定義匹配根節(jié)點(diǎn)的模板

復(fù)制代碼 代碼如下:

<xsl:template match=”/”>
</xsl:template>

2) xsl:for-each循環(huán)顯示select=”xpath expression”選擇節(jié)點(diǎn)的轉(zhuǎn)換 (類似編程語言中的foreach語句),
如下示例,選擇了pets下面的子元素,并循環(huán)顯示子元素的幾點(diǎn)名字:
復(fù)制代碼 代碼如下:

<xsl:for-each select=”/pets/*”>
<xsl:value-of select=”name()”/>
</xsl:for-each>

3) xsl:if 元素條件顯示節(jié)點(diǎn)(類似編程語言中的if語句)注意小于號(hào)大于號(hào)要分別用&lt;和&gt;替代
復(fù)制代碼 代碼如下:

<xsl:if test=”@weight &lt; 10”>
<i>its weight is less than 10 km</i>
</xsl:if>

4) xsl:choose 多分支條件顯示 (類似編程語言中的switch語句)
復(fù)制代碼 代碼如下:

<xsl:choose >
 <xsl:when test=”name() = ‘pig'”>
<i>this is a pig</i>
 </xsl:when>
<xsl:otherwise>
  <i>this is not a pig</i>
</xsl:otherwise>
</xsl:choose>

5) xsl:value-of 顯示選擇節(jié)點(diǎn)或者屬性的值
選擇子節(jié)點(diǎn)price
<xsl:value-of select=”pets/*/price”/>
選擇屬性weight
<xsl:value-of select=”pets/*/@weight”/>
6) xsl:attribute 構(gòu)造xml節(jié)點(diǎn)的屬性
用來向節(jié)點(diǎn)添加屬性,例如:
<font>
<xsl:attribute name=”color”><xsl:value-of select=”pets/*/@color”/></xsl:attribute>
</font>
將輸出<font color=”red”></font>
7) xsl:apply-templates 應(yīng)用模板
如果xml文件結(jié)構(gòu)比較復(fù)雜,可以定義多個(gè)template,然后使用<xsl:apply-templates>標(biāo)簽應(yīng)用模板,xsl:apply-templates 可以指定屬性select=”xpath”來選擇應(yīng)用的模板,或者不指定select表示選擇當(dāng)前節(jié)點(diǎn)的模板。
請(qǐng)看下面示例xslt文件pets-templates.xsl
完整的示例xsl文件:pets.xsl
復(fù)制代碼 代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <html>
      <head>
        <META http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title>lovely pets</title>
        <style type="text/css">
          ul{margin:10px 0 10px 0;padding:0;width:400px;text-align:left;}
          li{height:60px;display:block;list-style:none;padding:4px;border:1px solid #f0f0f0;margin:5px;}
        </style>
      </head>
      <body>
        <center>
        <h1>lovely pets</h1>
        <ul>
          <xsl:for-each select="pets/*">
            <li>
              <img align="right">
                <xsl:choose>
                  <xsl:when test="name() = 'dog'">
                    <xsl:attribute name="src">http://estar-tv.com/images/comprofiler/gallery/dog.gif</xsl:attribute>
                  </xsl:when>
                  <xsl:when test="name() = 'pig'">
                    <xsl:attribute name="src">http://www.icosky.com/icon/thumbnails/Animal/Farm/Pig%20Icon.jpg</xsl:attribute>
                  </xsl:when>
                  <xsl:otherwise>
                    <xsl:attribute name="src">http://farm1.static.flickr.com/14/buddyicons/22211409@N00.jpg?1143660418</xsl:attribute>
                  </xsl:otherwise>
                </xsl:choose>
              </img>
              <font>
                <xsl:attribute name="face">Courier</xsl:attribute>
                <xsl:attribute name="color">
                  <xsl:value-of select="@color"/>
                </xsl:attribute>
                <xsl:value-of select="name()"/>
              </font> said: "<xsl:value-of select="desc"/>"
              weight:<xsl:value-of select="@weight"/>

              <xsl:if test="@weight < 10">
                <p>
                  <i>its weight is less than 10 km</i>
                </p>
              </xsl:if>

 
            </li>
          </xsl:for-each>
        </ul>
        </center>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

完整示例文件 pets-templates.xsl:
復(fù)制代碼 代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <html>
      <head>
        <META http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title>lovely pets</title>
        <style type="text/css">
          ul{margin:10px 0 10px 0;padding:0;width:400px;text-align:left;}
          li{height:60px;display:block;list-style:none;padding:4px;border:1px solid #f0f0f0;margin:5px;}
        </style>
      </head>
      <body>
        <center>
          <h1>lovely pets</h1>
          <ul>
            <xsl:apply-templates select="pets" />
          </ul>
        </center>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="pets">  
    <xsl:apply-templates select="dog"></xsl:apply-templates>
    <xsl:apply-templates select="pig"></xsl:apply-templates>
    <xsl:apply-templates select="cat"></xsl:apply-templates>
  </xsl:template>

  <xsl:template match="dog">
    <xsl:for-each select=".">
      <li>
        <img align="right">
          <xsl:attribute name="src">http://estar-tv.com/images/comprofiler/gallery/dog.gif</xsl:attribute>        
        </img>
        <font>
          <xsl:attribute name="face">Courier</xsl:attribute>
          <xsl:attribute name="color">
            <xsl:value-of select="@color"/>
          </xsl:attribute>
          dog
        </font> said: "<xsl:value-of select="desc"/>"
        weight:<xsl:value-of select="@weight"/>

        <xsl:if test="@weight < 10">
          <p>
            <i>its weight is less than 10 km</i>
          </p>
        </xsl:if>
      </li>
    </xsl:for-each>
  </xsl:template>

 

  <xsl:template match="pig">
    <xsl:for-each select=".">
      <li>
        <img align="right">
          <xsl:attribute name="src">http://www.icosky.com/icon/thumbnails/Animal/Farm/Pig%20Icon.jpg</xsl:attribute>
        </img>
        <font>
          <xsl:attribute name="face">Courier</xsl:attribute>
          <xsl:attribute name="color">
            <xsl:value-of select="@color"/>
          </xsl:attribute>
          pig
        </font> said: "<xsl:value-of select="desc"/>"
        weight:<xsl:value-of select="@weight"/>

        <xsl:if test="@weight < 10">
          <p>
            <i>its weight is less than 10 km</i>
          </p>
        </xsl:if>
      </li>
    </xsl:for-each>
  </xsl:template>

 
  <xsl:template match="cat">
    <xsl:for-each select=".">
      <li>
        <img align="right">
          <xsl:attribute name="src">http://farm1.static.flickr.com/14/buddyicons/22211409@N00.jpg?1143660418</xsl:attribute>
        </img>
        <font>
          <xsl:attribute name="face">Courier</xsl:attribute>
          <xsl:attribute name="color">
            <xsl:value-of select="@color"/>
          </xsl:attribute>
          cat
        </font> said: "<xsl:value-of select="desc"/>"
        weight:<xsl:value-of select="@weight"/>

        <xsl:if test="@weight < 10">
          <p>
            <i>its weight is less than 10 km</i>
          </p>
        </xsl:if>
      </li>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>

在c#.net中使用XslCompiledTransform轉(zhuǎn)換xml文檔,XslTransform也可以使用,但是這個(gè)類已經(jīng)被微軟標(biāo)記為過時(shí),最好不要再用了,如下代碼示例:
復(fù)制代碼 代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;

namespace UseXslt
{
    class Program
    {
        static void Main(string[] args)
        {
            //聲明XslTransform類實(shí)例
            System.Xml.Xsl.XslCompiledTransform trans = new System.Xml.Xsl.XslCompiledTransform();

            string xsltFile = @"X:\about.net\System.Xml\example\pets.xsl";
            using (StreamReader rdr = new StreamReader(xsltFile))
            {
                using (XmlReader xmlRdr = XmlReader.Create(rdr))
                {
                    //載入xsl文件
                    trans.Load(xmlRdr);
                }
            }
            string inputFile = @"X:\about.net\System.Xml\example\pets.xml";
            string outputFile = @"X:\about.net\System.Xml\example\pets-out.htm";
            //轉(zhuǎn)化源文件輸出到輸出文件outputFile
            trans.Transform(inputFile, outputFile);
        }
    }
}

有一點(diǎn)需要注意,使用XslCompiledTransform轉(zhuǎn)換出來的文件,是一個(gè)html格式的,這個(gè)類會(huì)自動(dòng)在html的head標(biāo)簽中添加一個(gè)未關(guān)閉的meta標(biāo)簽 <META http-equiv="Content-Type" content="text/html; charset=utf-8">;微軟幫我們想的太多了。
Xslt還可以指定參數(shù),定義變量,有關(guān)這些方面請(qǐng)查看相關(guān)文檔。

相關(guān)文章

  • Entity?Framework?Core關(guān)聯(lián)刪除

    Entity?Framework?Core關(guān)聯(lián)刪除

    關(guān)聯(lián)刪除通常是一個(gè)數(shù)據(jù)庫術(shù)語,用于描述在刪除行時(shí)允許自動(dòng)觸發(fā)刪除關(guān)聯(lián)行的特征;即當(dāng)主表的數(shù)據(jù)行被刪除時(shí),自動(dòng)將關(guān)聯(lián)表中依賴的數(shù)據(jù)行進(jìn)行刪除,或者將外鍵更新為NULL或默認(rèn)值。本文將為大家具體介紹一下Entity?Framework?Core關(guān)聯(lián)刪除,需要的可以參考一下
    2021-12-12
  • ASP.NET讓FileUpload控件支持瀏覽自動(dòng)上傳功能的解決方法

    ASP.NET讓FileUpload控件支持瀏覽自動(dòng)上傳功能的解決方法

    這篇文章主要介紹了ASP.NET讓FileUpload控件支持瀏覽自動(dòng)上傳功能的解決方法,很實(shí)用的技巧,需要的朋友可以參考下
    2014-07-07
  • .NET?6開發(fā)TodoList應(yīng)用之實(shí)現(xiàn)DELETE請(qǐng)求與HTTP請(qǐng)求冪等性

    .NET?6開發(fā)TodoList應(yīng)用之實(shí)現(xiàn)DELETE請(qǐng)求與HTTP請(qǐng)求冪等性

    這篇文章主要介紹了在.NET6開發(fā)中如何實(shí)現(xiàn)DELETE請(qǐng)求以及HTTP請(qǐng)求冪等性的,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2021-12-12
  • .net中string無重復(fù)數(shù)字的實(shí)現(xiàn)方法

    .net中string無重復(fù)數(shù)字的實(shí)現(xiàn)方法

    今天做項(xiàng)目的時(shí)候,用js獲得了勾選的checkbox放在了hiddenfile里,然而hiddenfile的值變成了類似:“1,1,1,3,3,2,4,5,5,5”,后臺(tái)獲取的時(shí)候,只保留不重復(fù)的數(shù)字,于是想了一想;直接上代碼了。
    2013-04-04
  • asp.net 身份驗(yàn)證(最簡(jiǎn)單篇)

    asp.net 身份驗(yàn)證(最簡(jiǎn)單篇)

    在創(chuàng)建網(wǎng)站中,常常會(huì)使用到身份驗(yàn)證。asp.net中內(nèi)置了幾種身份驗(yàn)證的方式,如Windows、Froms、Passport等。這幾種身份驗(yàn)證的方式各有不同。
    2009-05-05
  • asp.net Grid 導(dǎo)出Excel實(shí)現(xiàn)程序代碼

    asp.net Grid 導(dǎo)出Excel實(shí)現(xiàn)程序代碼

    看了FineUI中的將Grid導(dǎo)出為Excel的實(shí)現(xiàn)方法,實(shí)際上是可以非常簡(jiǎn)單??磥砗茈y的問題,變換一種思路就可以非常簡(jiǎn)單
    2012-12-12
  • ASP.NET導(dǎo)出word實(shí)例

    ASP.NET導(dǎo)出word實(shí)例

    本文主要介紹了ASP.NET導(dǎo)出word的實(shí)例方法。具有很好的參考價(jià)值。下面跟著小編一起來看下吧
    2017-03-03
  • 詳解.Net緩存之MemoryCahe

    詳解.Net緩存之MemoryCahe

    這篇文章主要介紹了.Net緩存之MemoryCahe的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用.net技術(shù),感興趣的朋友可以了解下
    2021-05-05
  • ASp.net 文本框(TextBox)計(jì)算,判斷輸入的是否是數(shù)字

    ASp.net 文本框(TextBox)計(jì)算,判斷輸入的是否是數(shù)字

    ASp.net文本計(jì)算,文本框數(shù)字輸入檢測(cè),文本框的TextChanged事件,同時(shí)在屬性的Auto Post Back設(shè)置為True
    2009-07-07
  • 在.net core中實(shí)現(xiàn)字段和屬性注入的示例代碼

    在.net core中實(shí)現(xiàn)字段和屬性注入的示例代碼

    這篇文章主要介紹了在.net core中實(shí)現(xiàn)字段和屬性注入的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08

最新評(píng)論