Implementazione personalizzata della trasformazione XSLT

Classe ITransform2

A partire da BizTalk Server 2020, il motore di trasformazione XSLT personalizzato è supportato per la mappa BizTalk. È possibile implementare il motore di trasformazione XSLT personalizzato definendo l'implementazione della trasformazione XSLT derivata dalla classe astratta Microsoft.XLANGs.BaseTypes.ITransform2 nell'assembly Microsoft.XLANGs.BaseTypes.dll.

    public abstract class ITransform2
    {
       // This is not required, user can implement if they want their transform support custom extension.
       // These 3 parameters passed in are from "Custom Extension XML", in which user can provide namespace, assembly name, class name of the extension object, here user should create the extension behavior, like extension object creation, and registry.
        public virtual void RegisterExtension(string namespaceUri, string assemblyName, string className);
               
        // Load XSLT string.
        public abstract void Load(string xslt);

        // Transform input stream into out string.
        // Notice BizTalk actually doesn't support xslt arguments for now, it is reserved for future usage.
        public abstract void Transform(Stream input, IDictionary<XmlQualifiedName, object> xsltArguments, Stream results);
    }

Implementazione di esempio

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Xml;
using Microsoft.BizTalk.ScalableTransformation;
using Microsoft.XLANGs.BaseTypes;
using Saxon.Api;

namespace CustomTransform
{
    public class SaxonEEXsltTransform : ITransform2
    {
        protected bool legacyWhitespaceBehavior;
        protected Processor processor;
        protected XsltCompiler compiler;
        protected Xslt30Transformer transformer;

        public SaxonEEXsltTransform()
        {
            this.legacyWhitespaceBehavior = Microsoft.BizTalk.ScalableTransformation.BTSXslTransform.LegacyWhitespaceBehavior;

			// You have to put your license file in the designated place if you set "licensedEdition" as true. 
            this.processor = new Processor(true);

            this.processor.SetProperty(FeatureKeys.STRIP_WHITESPACE, this.legacyWhitespaceBehavior ? "all" : "none");
            this.compiler = processor.NewXsltCompiler();
        }

        public override void RegisterExtension(string namespaceUri, string assemblyName, string className)
        {
            Assembly assembly = Assembly.Load(assemblyName);
            Object obj = assembly.CreateInstance(className);

            ExtensionFunction function = obj as ExtensionFunction;
            if (function != null)
            {
                this.processor.RegisterExtensionFunction(function);
            }

            ExtensionFunctionDefinition functionDefinition = obj as ExtensionFunctionDefinition;
            if (functionDefinition != null)
            {
                this.processor.RegisterExtensionFunction(functionDefinition);
            }

            if (function == null && functionDefinition == null)
            {
                throw new ArgumentException(string.Format("Invalid extension class {0}, it should be of type {1} or {2}.", className, typeof(ExtensionFunction).Name, typeof(ExtensionFunctionDefinition).Name));
            }
        }

        public override void Load(string xslt)
        {
            XsltExecutable executable = this.compiler.Compile(new StringReader(xslt));
            this.transformer = executable.Load30();
        }

        public override void Transform(Stream input, IDictionary<XmlQualifiedName, object> xsltArguments, Stream results)
        {
            if (xsltArguments != null)
            {
                Dictionary<QName, XdmValue> parameters = new Dictionary<QName, XdmValue>();
                foreach (XmlQualifiedName name in xsltArguments.Keys)
                {
                    parameters[new QName(name)] = new XdmExternalObjectValue(xsltArguments[name]);
                }

                this.transformer.SetStylesheetParameters(parameters);
            }

            this.transformer.InputXmlResolver = new XmlUrlResolver();
            Serializer serializer = processor.NewSerializer();
            serializer.SetOutputStream(results);
            this.transformer.ApplyTemplates(input, serializer);
            serializer.Close();
        }
    }
}

Vedere anche

Proprietà del motore di trasformazione personalizzata XSLT

XML estensione personalizzato