방법: 어셈블리를 사용하여 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을(를) 참조하세요.

참고 항목