How to: Perform an XSLT transformation by using an assembly


Script blocks are supported only in .NET Framework. They are not supported on .NET Core or .NET 5 or later.

The XSLT compiler (xsltc.exe) compiles XSLT style sheets and generates an assembly. The assembly can be passed directly into the XslCompiledTransform.Load(Type) method.

To copy the XML and XSLT files to your local computer

  • Copy the XSLT file to your local computer and name it Transform.xsl.

    <xsl:stylesheet version="1.0" xmlns:xsl=""  
      <msxsl:script language="C#" implements-prefix="user">  
      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;  
      <xsl:template match="catalog">  
            <table border="1">  
                <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>  
              <xsl:for-each select="book">  
                    <xsl:value-of select="title"/>  
                    <xsl:value-of select="author"/>  
                    <xsl:value-of select="genre"/>  
                    <xsl:value-of select="publish_date"/>  
                    <xsl:when test="genre = 'Fantasy'">  
                        <xsl:value-of select="user:discount(price)"/>  
                        <xsl:value-of select="price"/>  
  • Copy the XML file to your local computer and name it books.xml.

    <?xml version="1.0"?>  
       <book id="bk101">  
          <author>Gambardella, Matthew</author>  
          <title>XML Developer's Guide</title>  
       <book id="bk102">  
          <author>Ralls, Kim</author>  
          <title>Midnight Rain</title>  
       <book id="bk103">  
          <author>Corets, Eva</author>  
          <title>Maeve Ascendant</title>  
       <book id="bk106">  
          <author>Randall, Cynthia</author>  
          <title>Lover Birds</title>  
       <book id="bk107">  
          <author>Thurman, Paula</author>  
          <title>Splish Splash</title>  

To compile the style sheet with the script enabled

The following command creates two assemblies named Transform.dll and Transform_Script1.dll. This is the default behavior. Unless otherwise specified, the name of the class and the assembly defaults to the name of the main style sheet.

xsltc /settings:script+ Transform.xsl  

The following command explicitly sets the class name to Transform:

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

To include the compiled assembly as a reference when you compile your code

  1. You can include an assembly in Visual Studio by adding a reference in the Solution Explorer, or from the command line.

  2. For the command line with C#, use the following:

    csc myCode.cs /r:system.dll;system.xml.dll;Transform.dll  
  3. For the command line with Visual Basic, use the following

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

To use the compiled assembly in your code

The following example shows how to execute the XSLT transformation by using the compiled style sheet.

using System;
using System.Xml.Xsl;

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

        // 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()

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

End Module

To dynamically link to the compiled assembly, replace




For more information on the Assembly.Load method, see Load.

See also