July 10, 2007

Saxon, NET and XInclude

Saxon, famous XSLT 2.0 and XQuery processor, supports XInclude since version 8.9. But in Java version only! When I first heard about it I thought "I have good XInclude implementation for .NET in Mvp.Xml library, let's check out if Saxon on .NET works with XInclude.NET". I did some testing only to find out that they didn't play well together. 

Turned out Saxon (or JAXP port to .NET, don't remember) relies on somewhat rarely used in .NET XmlReader.GetAttribute(int) method (yes, accessing attribute by index), and XIncludingReader had a bug in this method.

Finally I fixed it and so XIncludingReader from recently released Mvp.Xml library v2.3 works fine with Saxon on .NET.

Here is a little sample how to process XML Inclusions in source XML document before XSLT transformation.

using System;
using Saxon.Api;
using Mvp.Xml.XInclude;

class Program
  static void Main(string[] args)
    Processor proc = new Processor();
    XdmNode doc = proc.NewDocumentBuilder().Build(
      new XIncludingReader("d:/test/document.xml"));
    XsltExecutable xslt = proc.NewXsltCompiler().Compile(
      new Uri("d:/test/foo.xsl"));
    XsltTransformer trans = xslt.Load();
    trans.InitialContextNode = doc;
    Serializer ser = new Serializer();