溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么在.net中使用XSLT對xml文檔進行轉換

發布時間:2021-02-23 16:59:07 來源:億速云 閱讀:187 作者:Leah 欄目:開發技術

怎么在.net中使用XSLT對xml文檔進行轉換?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

XSL即可擴展的樣式表文件。 可以格式化xml的顯示,也可以將xml轉換成需要的另一種格式。
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也是一個標準的xml文件,它以xml文檔聲明開始,根元素必須是xsl:styleshee,同時根元素必須有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要點 如下示例xml

復制代碼 代碼如下:


<?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格式化之后的顯示效果如下:

怎么在.net中使用XSLT對xml文檔進行轉換
1) xsl:template定義匹配節點的轉換模板,屬性match=”xpath expression”用來定義模板匹配的元素
如下定義匹配根節點的模板

復制代碼 代碼如下:


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


2) xsl:for-each循環顯示select=”xpath expression”選擇節點的轉換 (類似編程語言中的foreach語句),
如下示例,選擇了pets下面的子元素,并循環顯示子元素的幾點名字:

復制代碼 代碼如下:


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


3) xsl:if 元素條件顯示節點(類似編程語言中的if語句)注意小于號大于號要分別用&lt;和&gt;替代

復制代碼 代碼如下:


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


4) xsl:choose 多分支條件顯示 (類似編程語言中的switch語句)

復制代碼 代碼如下:


<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 顯示選擇節點或者屬性的值
選擇子節點price
<xsl:value-of select=”pets/*/price”/>
選擇屬性weight
<xsl:value-of select=”pets/*/@weight”/>
6) xsl:attribute 構造xml節點的屬性
用來向節點添加屬性,例如:
<font>
<xsl:attribute name=”color”><xsl:value-of select=”pets/*/@color”/></xsl:attribute>
</font>
將輸出<font color=”red”></font>
7) xsl:apply-templates 應用模板
如果xml文件結構比較復雜,可以定義多個template,然后使用<xsl:apply-templates>標簽應用模板,xsl:apply-templates 可以指定屬性select=”xpath”來選擇應用的模板,或者不指定select表示選擇當前節點的模板。
請看下面示例xslt文件pets-templates.xsl
完整的示例xsl文件:pets.xsl

復制代碼 代碼如下:


<?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>
        <h2>lovely pets</h2>
        <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:

復制代碼 代碼如下:


<?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>
          <h2>lovely pets</h2>
          <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轉換xml文檔,XslTransform也可以使用,但是這個類已經被微軟標記為過時,最好不要再用了,如下代碼示例:

復制代碼 代碼如下:


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類實例
            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";
            //轉化源文件輸出到輸出文件outputFile
            trans.Transform(inputFile, outputFile);
        }
    }
}


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

關于怎么在.net中使用XSLT對xml文檔進行轉換問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女