nXSLTC - an experimental XSLT compiler for .NET 3.5

| 2 Comments | 1 TrackBack

So here is nXSLTC.exe v1.0b - an experimental XSLT compiler for .NET 3.5 (Visual Studio "Orcas"). Get it here (free open-source). I probably shouldn't be building this tool, but I have my reasons.

Why not? As a matter of fact, Mike Champion has announced back in February that Microsoft will provide XSLTC.exe tool in the next Visual Studio "Orcas", but apparently it didn't make it into March CTP, at least I didn't manage to find it. It probably will be included into the next drop.

Why yes? First - this is the best way of learning new technology and then - I plan to build something bigger, which Microsoft won't be doing - XSLT project for Visual Studio, so I need this code anyway.

Ok, so nXSLT.exe. It's a command line XSLT compiler for .NET 3.5. It compiles one or more XSLT stylesheets into DLL. These compiled stylesheets can be later used to transform XML documents using XslCompiledTransform class.

The usage is obvious:

nxsltc <source files> [options]
where possible options include:
  /out:<file>            Specifies the output file name.
  /debug[+|-]            Emit debugging information.
  /nowarn                Disable all warnings.
  /namespace:<string>    Specifies namespace for compiled stylesheets.
  /help                  Display this usage message. (Short form: /?)
  /nologo                Do not display compiler copyright banner.

Here is a little sample. Consider this dummy XSLT stylesheet (catalog2html.xslt):

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="catalog">
    <table>
      <xsl:apply-templates/>
    </table>
  </xsl:template>
  <xsl:template match="book">
    <tr>
      <td>
        <xsl:value-of select="title" />
      </td>
      <td>
        <xsl:value-of select="price" />
      </td>
    </tr>
  </xsl:template>
</xsl:stylesheet>

Let's compile it:

nxsltc.exe catalog2html.xslt /namespace:Foo.Bar /out:stylesheets.dll /debug+

The compilation result is stylesheets.dll, which contains our compiled stylesheet as Foo.Bar.catalog2html class:

How do you run such compiled stylesheet? Easy. XslCompiledTransform class has new overloaded Load() method  in .NET 3.5, which accepts Type:

XPathDocument doc = new XPathDocument("books.xml"); 
XslCompiledTransform xslt = new XslCompiledTransform(); 
xslt.Load(typeof(Foo.Bar.catalog2html)); 
xslt.Transform(doc, null, Console.Out); 

Above obviously assumes you have added a reference to the stylesheets.dll into your project. Alternatively you can load dll with compiled stylesheets dynamically using Assembly.Load().

I didn't tested performance yet, but it feels like a rocket, very fast. More to come. Get nXSLTC.exe v1.0b here. Enjoy!

PS. And sorry to Microsoft XML Team for spoiling their XSLTC.exe ;)

Related Blog Posts

1 TrackBack

TrackBack URL: http://www.tkachenko.com/cgi-bin/mt-tb.cgi/690

I was reading Scott's post on Reflector Addins and had this idea... Now (well, not now but in the next .NET version - Orcas) that XSLT can be compiled into dll, it must be time to think about XSLT decompiler (and appropriate Reflector addin of course).... Read More

2 Comments

Thanks for update, Sergey!

Yes, I'll add support for compiled stylesheets into nxslt.exe.

Thanks for doing this. We missed the Beta 1 train and I tried to publish XSLTc.exe in sources but this happened to be too complicated.
As soon as I found that you publish nXSLTC I dropped my efforts.
One free Idea for you: add special options to nXSLT.exe to allow execution of compiled stylesheets.
XMLTc.exe expected to show up somewhere in SDK in Beta 2 of VS.

Leave a comment