0

I am programming in C#. I was previously using the following command line to convert an xml with a xsl and output it as a html.

java -jar "C:\Data\saxon-he-9.4.0.7.jar" View.xml Stylesheet.xsl -o:output.html

However, I am now trying to use the Saxon .Net API to do the same process using the following code:

            var xslt = new FileInfo(@"C:\\Data\\Stylesheet.xsl");
            var input = new FileInfo(@"C:\\Data\\View.xml");
            var output = new FileInfo(@"C:\\Data\\test.html");

            // Compile stylesheet
            var processor = new Processor();
            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);
            }

            // Save result to a file (or whatever else you wanna do)
            destination.XmlDocument.Save(output.FullName);

However I recieve the error: "An unhandled exception of type 'Saxon.Api.DynamicError' occurred in saxon9he-api.dll" When running the line "transformer.Run(destination);"

The following screenshots are from the Visual Studio's Locals Debugging:

$exception {"XSLT 1.0 compatibility mode is not available in this configuration"} Saxon.Api.DynamicError

transformer {Saxon.Api.XsltTransformer} Saxon.Api.XsltTransformer

Luke Phillips
  • 3,372
  • 1
  • 11
  • 15

1 Answers1

0

The first thing you need to do is to get more specific information about the nature of the error. Catching the exception and printing the exception message would be a good start. But Saxon will have written diagnostics to the standard error output, which probably ends up in some log file somewhere, depending on how your application is configured and run. If you can't track it down, try redirecting it as described here: How to capture a Processes STDOUT and STDERR line by line as they occur, during process operation. (C#)

Once you've established the actual error, edit the question and we can start investigating what's wrong if it's not obvious.

A common cause of problems when writing to a DomDestination is that your result tree isn't well-formed, e.g, it has text nodes or multiple elements at the top level. It's not clear why you are writing to a DomDestination - if you just want to produce serialized XML, then write to a Serializer.

LATER

Now you've found the error message ("XSLT 1.0 compatibility mode is not available in this configuration") it should be fairly clear. When a stylesheet specifies version="1.0" and is run with an XSLT 2.0 or 3.0 processor, it runs in a compatibility mode where certain things behave differently (for example xsl:value-of ignores all but the first selected item). This compatibility mode, from Saxon 9.8 onwards, is not available in Saxon-HE. You need to do one of three things: upgrade to Saxon-PE or -EE; revert to an earlier Saxon-HE version; or convert your stylesheet to XSLT 2.0 (which basically means (i) change the value of the version attribute (ii) test that it still works.)

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • Thanks Michael, I added screenshots are from the Visual Studio's Locals Debugging is that enough information for you? I got the code off: https://www.geekality.net/2012/09/24/c-getting-started-with-saxon-he-xslt-transformation/ I want to produce an HTML output file from the XSL transformation of an XML document. – Luke Phillips Oct 03 '17 at 01:04
  • Cutting and pasting code off random blogs on the internet without actually understanding it and then asking on StackOverflow why it doesn't work is not really the most effective approach to programming that I know of... – Michael Kay Oct 03 '17 at 07:16