0

client side - react js server side - dot net

XSLT version - 2.0

hi, requirement is to transform an XML file to a html file using an XSLT stylesheet to display to the user in the client side. But problem is I could not find a way to transform it properly.

What I tried so far,

  1. Tried linking the stylesheet in the xml file and opening it in the browser so that the transformation will be done by the browser automatically but this did not work as expected. In chrome it's just a blank window and in firefox it displays the text with no styling. I also found out that browsers still do not support xslt 2.0 transformation so I assume that is the issue.

----------------------xml data--------------------------------

Above shows how I linked it. Tried both type="text/xslt" and type="text/xsl".

  1. Tried transform in the server side (.net 7 /c#).

    XslCompiledTransform transform = new XslCompiledTransform(); using(XmlReader reader = XmlReader.Create(new StringReader(xsltString))) { transform.Load(reader); } StringWriter results = new StringWriter(); using(XmlReader reader = XmlReader.Create(new StringReader(inputXml))) { transform.Transform(reader, null, results); } return results.ToString();

Above method did not give any error but no content in the resulting file. Later found out that XslCompiledTransform does not support XSLT 2.0, it only supports 1.0. So I tried a 3rd party library Saxon-HE.

            var xslt = new FileInfo(@"E:\xmltesting\stylesheet-ubl.xslt");
            var input = new FileInfo(@"E:\xmltesting\invoice32.xml");
            var output = new FileInfo(@"E:\xmltesting\test.html");

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

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

Above method gives exception at below line,

var executable = compiler.Compile(new Uri(xslt.FullName));

System.TypeInitializationException: 'The type initializer for 'sun.util.calendar.ZoneInfoFile' threw an exception.' Inner Exception MissingMethodException: Method not found: 'Void System.IO.FileStream..ctor(System.String, System.IO.FileMode, System.Security.AccessControl.FileSystemRights, System.IO.FileShare, Int32, System.IO.FileOptions)'.

Could not find much related to this exception.

  1. Since transforming from the server-side doesn't look that promising atm moved back to client side transformation. I am currently looking into saxon-js...but still no luck.

Anyone have an idea on how to go about this?. Thanks.

8
  • Which version of Saxon-HE is that exactly, are you using it with the .NET framework on Windows or are you trying to use .NET 7 (core) and if so, on which platform are you getting that exception? Commented Feb 14, 2023 at 11:39
  • Also, as for trying to use Saxon-HE, do you get that exception with any attempt to run XSLT 2 through it or only with your particular XSLT code? Commented Feb 14, 2023 at 11:43
  • @MartinHonnen Saxon-HE version is 10.8.0 and am using it with .NET 7 framework on windows 10 and 11 both Commented Feb 14, 2023 at 11:48
  • @MartinHonnen I have tried with only 1 XSLT 2.0 stylesheet which is the one that is needed to transform the xml into the desired html. Commented Feb 14, 2023 at 11:50
  • Saxon HE 10 from Saxonica is compatible with the Microsoft .NET framework, the latest version of that is 4.8. NET 7 is no .NET framework but latest (cross platform) .NET Core release, I kind of wonder how you managed to use a .NET framework only package like Saxon HE 10 with .NET 7 at all. As for running the stylesheet through Saxon HE 10.8 on Windows, before writing your own code, have you tried to run it through Saxon's HE .NET command line Transform.exe -s:input.xml -xsl:ubl.xslt, to test whether that work? Commented Feb 14, 2023 at 12:00

2 Answers 2

1

Martin's answer has shown you the options for running the transformation server-side using Saxon on .NET.

But you also asked about the options for running the transformation client-side in the browser; for that, please take a look at SaxonJS.

Sign up to request clarification or add additional context in comments.

Comments

0

If you want to run XSLT 2 or 3 stylesheets with .NET 7 you can do so using the commercial SaxonCS package (https://www.nuget.org/packages/SaxonCS, latest versions are 11.5 and 12.0) or using the IKVM cross compiled version of Saxon HE 11.5 (https://www.nuget.org/packages/SaxonHE11s9apiExtensions); the following is code using the IKVM cross compiled Saxon HE 11.5 in .NET 7:

using net.liberty_development.SaxonHE11s9apiExtensions;
using net.sf.saxon.s9api;

var processor = new Processor(false);

var xsltCompiler = processor.newXsltCompiler();

var xsltExecutable = xsltCompiler.Compile(new FileInfo("ubl.xslt"));

var xslt30Transformer = xsltExecutable.load30();

xslt30Transformer.Transform(new FileInfo("invoice-sample.xml"), processor.NewSerializer(new FileInfo("invoice-sample.html")));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.