방법: 어셈블리를 사용하여 XSLT 변환 수행
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인 두 개의 어셈블리가 만들어집니다. 이것이 기본 동작이며, 아무 것도 지정하지 않으면 클래스 및 어셈블리 이름은 기본 스타일시트 이름으로 지정됩니다.
xsltc /settings:script+ Transform.xsl
다음 명령에서는 명시적으로 클래스 이름을 Transform으로 설정합니다.
xsltc /settings:script+ /class:Transform Transform.xsl
코드를 컴파일할 때 컴파일된 어셈블리를 참조로 포함하려면
Visual Studio의 솔루션 탐색기에서 참조를 추가하거나 명령줄을 사용하여 어셈블리를 포함할 수 있습니다.
C#을 사용하는 명령줄의 경우 다음을 사용합니다.
csc myCode.cs /r:system.dll;system.xml.dll;Transform.dll
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를 참조하십시오.