如何:使用組件執行 XSLT 轉換

注意

只有 .NET Framework 才支援指令碼區塊。 .NET Core 或 .NET 5 或更新版本加以支援。

XSLT 編譯器 (xsltc.exe) 會編譯 XSLT 樣式表並產生組件。 然後此組件可以直接傳遞到 XslCompiledTransform.Load(Type) 方法中。

將 XML 和 XSLT 檔案複製到本機電腦

  • 將 XSLT 檔案複製到本機電腦,並將它命名為 Transform.xsl。

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  
      xmlns:msxsl="urn:schemas-microsoft-com:xslt"  
      xmlns:user="urn:my-scripts">  
      <msxsl:script language="C#" implements-prefix="user">  
        <![CDATA[  
      public string discount(string price){  
        char[] trimChars = { '$' };  
        //trim leading $, convert price to type double  
        double discount_value = Convert.ToDouble(price.TrimStart(trimChars));  
        //apply 10% discount and round appropriately  
        discount_value = .9*discount_value;  
        //convert value to decimal and format as currency  
        string discount_price = discount_value.ToString("C");  
        return discount_price;  
      }  
      ]]>  
      </msxsl:script>  
      <xsl:template match="catalog">  
        <html>  
          <head></head>  
          <body>  
            <table border="1">  
              <tr>  
                <th align="left">Title</th>  
                <th align="left">Author</th>  
                <th align="left">Genre</th>  
                <th align="left">Publish Date</th>  
                <th align="left">Price</th>  
              </tr>  
              <xsl:for-each select="book">  
                <tr>  
                  <td>  
                    <xsl:value-of select="title"/>  
                  </td>  
                  <td>  
                    <xsl:value-of select="author"/>  
                  </td>  
                  <td>  
                    <xsl:value-of select="genre"/>  
                  </td>  
                  <td>  
                    <xsl:value-of select="publish_date"/>  
                  </td>  
                  <xsl:choose>  
                    <xsl:when test="genre = 'Fantasy'">  
                      <td>  
                        <xsl:value-of select="user:discount(price)"/>  
                      </td>  
                    </xsl:when>  
                    <xsl:otherwise>  
                      <td>  
                        <xsl:value-of select="price"/>  
                      </td>  
                    </xsl:otherwise>  
                  </xsl:choose>  
                </tr>  
              </xsl:for-each>  
            </table>  
          </body>  
        </html>  
      </xsl:template>  
    </xsl:stylesheet>  
    
  • 將 XML 檔案複製到本機電腦,並將它命名為 books.xml

    <?xml version="1.0"?>  
    <catalog>  
       <book id="bk101">  
          <author>Gambardella, Matthew</author>  
          <title>XML Developer's Guide</title>  
          <genre>Computer</genre>  
          <price>$44.95</price>  
          <publish_date>2000-10-01</publish_date>  
       </book>  
       <book id="bk102">  
          <author>Ralls, Kim</author>  
          <title>Midnight Rain</title>  
          <genre>Fantasy</genre>  
          <price>$5.95</price>  
          <publish_date>2000-12-16</publish_date>  
       </book>  
       <book id="bk103">  
          <author>Corets, Eva</author>  
          <title>Maeve Ascendant</title>  
          <genre>Fantasy</genre>  
          <price>$5.95</price>  
          <publish_date>2000-11-17</publish_date>  
       </book>  
       <book id="bk106">  
          <author>Randall, Cynthia</author>  
          <title>Lover Birds</title>  
          <genre>Romance</genre>  
          <price>$4.95</price>  
          <publish_date>2000-09-02</publish_date>  
       </book>  
       <book id="bk107">  
          <author>Thurman, Paula</author>  
          <title>Splish Splash</title>  
          <genre>Romance</genre>  
          <price>$4.95</price>  
          <publish_date>2000-11-02</publish_date>  
       </book>  
    </catalog>  
    

在啟用指令碼的情況下編譯樣式表

下列命令會建立名為 Transform.dllTransform_Script1.dll 的兩個組件。 這是預設行為。 除非另外指定,否則類別和組件的名稱會預設為主要樣式表的名稱。

xsltc /settings:script+ Transform.xsl  

下列命令會明確將類別名稱設定為 Transform:

xsltc /settings:script+ /class:Transform Transform.xsl  

當您在編譯程式碼時,將編譯的組件當做參考併入

  1. 您可以在 Visual Studio 中包含組件,其方式是在 [方案總管] 內或是從命令列加入參考。

  2. 如果是使用 C# 的命令列,請使用以下程式碼:

    csc myCode.cs /r:system.dll;system.xml.dll;Transform.dll  
    
  3. 如果是使用 Visual Basic 的命令列,請使用以下程式碼:

    vbc myCode.vb /r:system.dll;system.xml.dll;Transform.dll  
    

在程式碼中使用編譯的組件

下列範例將示範如何使用編譯的樣式表執行 XSLT 轉換。

using System;
using System.Xml.Xsl;

class Example
{
    static void Main()
    {
        //Create a new XslCompiledTransform and load the compiled transformation.
        XslCompiledTransform xslt = new XslCompiledTransform();
        xslt.Load(typeof(Transform));

        // Execute the transformation and output the results to a file.
        xslt.Transform("books.xml", "discount_books.html");
    }
}
Imports System.Xml.Xsl

Module Module1

    Sub Main()
        'Create a new XslCompiledTransform and load the compiled transformation.
        Dim xslt As New XslCompiledTransform()
        xslt.Load(GetType(Transform))

        'Execute the transform and output the results to a file.
        xslt.Transform("books.xml", "discount_books.html")
    End Sub

End Module

若要動態連結至已編譯的組件,請將

xslt.Load(typeof(Transform));  

取代為

xslt.Load(System.Reflection.Assembly.Load("Transform").GetType("Transform"));  

如需 Assembly.Load 方法的詳細資訊,請參閱 Load

另請參閱