DevX
DevX

Reputation: 725

XML transformation using XSLT version 2

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.

Upvotes: 0

Views: 1011

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167716

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);
            }
        }


    }
}

Upvotes: 0

Related Questions