Search code examples
c#.netprogressive-web-appsblazorblazor-client-side

Blazor - 'msxsl:script' element is not supported on this framework because it does not support run-time code generation


I have a problem with using XslCompiledTransform in Blazor Webassembly.

blazor.webassembly.js:1 crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100] Unhandled exception rendering component: 'msxsl:script' element is not supported on this framework because it does not support run-time code generation

Error in google chrome console

public async Task<string> TransformSomething(string xml, string kluczPakietu, string kodPakietu)
        {
            var xslFile = await _httpClient.GetByteArrayAsync("transform.xsl");

            //change bytearray to string - valid is working here.
            string transformataHTML = System.Text.Encoding.Default.GetString(xslFile);

            
            XDocument oldDocument = XDocument.Parse(xml);
            var newDocument = new XDocument();


            
            using (var stringReader = new StringReader(transformataHTML))
            {
                using (XmlReader xsltReader = XmlReader.Create(stringReader))
                {
                    XsltArgumentList argsList = new XsltArgumentList();
                    argsList.AddParam("SVG", "", "enabled");
                    argsList.AddParam("klucz", "", kluczPakietu);
                    argsList.AddParam("kod", "", kodPakietu);
                    XsltSettings xsltSettings = new XsltSettings();
                    xsltSettings.EnableDocumentFunction = true;
                    xsltSettings.EnableScript = true;

                    var transformer = new XslCompiledTransform();


                    XslCompiledTransform xslt = new XslCompiledTransform();
                    xslt.Load(xsltReader);

                    //here is exception
                    transformer.Load(xsltReader, xsltSettings, new XmlUrlResolver());
                    //transformer.Load(xsltReader);
                    using (XmlReader oldDocumentReader = oldDocument.CreateReader())
                    {
                        using (XmlWriter newDocumentWriter = newDocument.CreateWriter())
                        {
                            transformer.Transform(oldDocumentReader, argsList, newDocumentWriter);
                        }
                    }
                }
            }

            return newDocument.ToString();
        }

Is it possible to do it in Blazor Webassembly? In Blazor Server-side the script works.


Solution

  • XslCompiledTransform would try to generate new .NET code at run-time and would expect a JIT to compile it to native code for the underlying machine. Since WebAssembly does not support this (just like other platforms that require AOT like iOS), you cannot use it.

    Use the XslTransform class instead of XslCompiledTransform to work with XSLT on these platforms.

    Update: Also note that scripts inside XSLT always need run-time code generation, so even if XslCompiledTransform would fall back to non-compiled transform, scripts are only implemented for run-time code generation at the moment:

    The XslCompiledTransform class supports embedded scripts using the msxsl:script element. When the style sheet is loaded, any defined functions are compiled to Microsoft intermediate language (MSIL) by the Code Document Object Model (CodeDOM) and are executed during run time. The assembly generated from the embedded script block is separate than the assembly generated for the style sheet.

    Source