次の方法で共有


方法 : アセンブリを使用して XSLT 変換を実行する

更新 : November 2007

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.dll および Transform_Script1.dll という名前で 2 つのアセンブリが作成されます (これが既定の動作です。他に指定しなければ、既定のクラス名とアセンブリ名はメインのスタイル シートの名前になります)。

    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 変換を実行する方法を次の例に示します。
Imports System
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
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");
    } 
}

上記の例で、コンパイル済みアセンブリへのダイナミック リンクを作成するには、

xslt.Load(typeof(Transform))

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

に置き換えます。Assembly.Load メソッドの詳細については、「Load」を参照してください。

参照

概念

コマンド ラインからのビルド

参照

XSLT コンパイラ (xsltc.exe)

XslCompiledTransform

その他の技術情報

XSLT 変換