3

I have create new function app in .net 6.0 , I’m getting an error from XslCompiledTransform.Load once after EnableScript. (this works fine in .net framework 4.6.1) I'll share my code segment here for your reference.

private string TransformXslt(XmlReader document, string stylesheet, object[] extension,
        XSLTParams[] xsltArguments)
    {
        var transform = new XslCompiledTransform(true);

        transform.Load(stylesheet, new XsltSettings(true, true), null);

        var arguments = new XsltArgumentList();
        if (xsltArguments != null)
            for (var i = 0; i < xsltArguments.Length; i++)
            {
                var currentParam = xsltArguments[i];
                arguments.AddParam(currentParam.name, "", currentParam.value);
            }

        for (var index = 0; index < extension.Length; index += 2)
            arguments.AddExtensionObject(
                extension[index] as string,
                extension[index + 1]
            );

        var output = new StringBuilder();

        using (var writer = XmlWriter.Create(output, transform.OutputSettings))
        {
            transform.Transform(document, arguments, writer);
        }

        return output.ToString();
    }

Error

enter image description here

This should support .net 6 as microsoft page XslCompiledTransform.Load Method

but not sure why this failing in transform.Load(stylesheet, new XsltSettings(true, true), null); line.

i should enable scripting because in my xsl i have c# scrips..

enter image description here

3
  • 4
    You might need to use extension objects/functions instead of "script" with .NET core, that feature to embed C# is not supported. Commented Jul 12, 2022 at 9:56
  • 1
    A little follow-up on this topic, does anyone know if there is a possible work around for this? I Really need to be able to execute these templates at .net 6 :) Commented Oct 26, 2022 at 5:58
  • May I know if this was already resolved in .NET 6, or maybe a work around for this? Thanks. Commented Feb 5, 2024 at 6:02

2 Answers 2

3

According to documentation:

Script blocks are supported only in .NET Framework. They are not supported on .NET Core or .NET 5 or later.

Reference: XSLT compile error-PlatformNotSupportedException: Compiling JScript/CSharp scripts is not supported

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

1 Comment

I don't see this is an answer. This just seems to be a clarification on why this is not working anymore, because it is not supported anymore
0

Yes, there is a workaround . You need to re-work your code to use it, but, it is possible.

Extract the C# code from within the XSL sheet and place it into its own class. Compile the class into a DLL, then you can access that DLL from within the XSLT via extension objects.

Your XSL can look like this:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:ext="urn:extension1"
                exclude-result-prefixes="ext">

  <xsl:output omit-xml-declaration="yes" method="xml" encoding="UTF-8" indent="yes"/>

  <xsl:template match="/data">
    <xsl:if test="circle">
      <circles>
        <calc-time>
          <xsl:value-of select="ext:Timestamp()"/>
        </calc-time>
        <xsl:for-each select="circle">
          <circle>
            <xsl:copy-of select="node()"/>
            <circumference>
              <xsl:value-of select="ext:Circumference(./radius)"/>
            </circumference>
          </circle>
        </xsl:for-each>
      </circles>
    </xsl:if>
  </xsl:template>

  <xsl:template match="/*" priority="0">
    <none/>
  </xsl:template>

</xsl:stylesheet>

And via that xmlns:ext namespace, you can invoke logic implemented externally in C# code. to do that from .NET you have to use an "XSL arglist" when you invoke the transform:

  XslCompiledTransform xslt = new XslCompiledTransform();
  xslt.Load(xsltFilename, null /* xsltSettings */, new XmlUrlResolver());

  XsltArgumentList xslArglist = new XsltArgumentList();
  ExtObject obj = new ExtObject();
  xslArglist.AddExtensionObject("urn:extension1", obj);

  try
  {
      // Load the XML source file
      XmlDocument xmldoc = new XmlDocument();
      xmldoc.PreserveWhitespace = true;
      xmldoc.LoadXml(rawContent);
      XPathDocument doc = new XPathDocument(new XmlNodeReader(xmldoc));

      // Create an XmlWriter with the right indentation settings
      XmlWriterSettings xmlWriterSettings = new XmlWriterSettings()
      {
          IndentChars = "  ",
          OmitXmlDeclaration = true,
          Indent = true,
      };

      using (var textWriter = new Utf8StringWriter())
      {
          using (
              var xmlWriter = XmlWriter.Create(textWriter, xmlWriterSettings)
          )
          {
              // Execute the transformation
              xslt.Transform(doc, xslArglist, xmlWriter);
              textWriter.Flush();
              String transformedContent = textWriter.ToString() ?? string.Empty;
          }
      }
  }
  catch (Exception e1)
  {
      Console.Error.WriteLine(e1.ToString());
      return Results.BadRequest();
  }

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.