0

I am trying to transform XML using XSLT version 2.0 but getting below exception:

Cannot find a script or an extension object associated with namespace 'http://www.w3.org/2001/XMLSchema'.

Later I came to know that Microsoft doesn't support XSLT version 2.0. Refer link. It suggests to use some third party tool. But these tools have some license fee associated. Is there any similar freeware available?

[Edit]

I tried to use SaxonHe

  public static void Func( string xsltFile,string inputFile, string outputFile)
    {

        var  xslt = new FileInfo(xsltFile);
        var input = new FileInfo(inputFile);
        var output = new FileInfo(outputFile);
        // Compile stylesheet
        var processor = new Processor(false);
        var compiler = processor.NewXsltCompiler();
        var executable = compiler.Compile(new Uri(xslt.FullName));


        // Do transformation to a destination
        var destination = new DomDestination();
        using (var inputStream = input.OpenRead())
        {
            var transformer = executable.Load();
            transformer.SetInputStream(inputStream, new Uri(input.DirectoryName));
            transformer.Run(destination);
        }

        destination.XmlDocument.Save(output.FullName);
    }

It's not working giving null reference exception on last statement. Actually destination.XmlDocument is getting null value.

Community
  • 1
  • 1
DevX
  • 725
  • 3
  • 13
  • 26
  • Saxon 9 HE is open source, https://www.nuget.org/packages/Saxon-HE/. – Martin Honnen Jul 14 '16 at 14:14
  • Thanks Martin for your suggestion. I tried to use use Saxon 9 He but its not working for me. I have edited question with details. – DevX Jul 19 '16 at 08:58
  • I can try to help using Saxon with C# but I think you should first state what is the aim, do you want to create a result document on disk? Your code tries to create an `XmlDocument` only to save it to a file it seems, so I wonder whether that is the only way you found to write the result to a file or whether you want to have the result in an `XmlDocument` and as a file as well. – Martin Honnen Jul 19 '16 at 09:24

1 Answers1

0

I think if you want to simply create the transformation result as a file then you don't need a DomDestination, you can use a Serializer, as in

    static void Main(string[] args)
    {
        TestTransform("XSLTFile1.xslt", "XMLFile1.xml", "Result1.xml");
    }

    public static void TestTransform(string xsltFile, string inputFile, string outputFile)
    {

        var xslt = new FileInfo(xsltFile);
        var input = new FileInfo(inputFile);
        var output = new FileInfo(outputFile);

        // Compile stylesheet
        var processor = new Processor(false);
        var compiler = processor.NewXsltCompiler();
        var executable = compiler.Compile(new Uri(xslt.FullName));


        using (var inputStream = input.OpenRead())
        {
            var transformer = executable.Load();
            transformer.SetInputStream(inputStream, new Uri(input.DirectoryName));

            var serializer = new Serializer();

            using (var resultStream = output.OpenWrite())
            {
                serializer.SetOutputStream(resultStream);
                transformer.Run(serializer);
            }
        }


    }
}
Martin Honnen
  • 160,499
  • 6
  • 90
  • 110