次の方法で共有


カスタム XSLT 変換の実装

ITransform2 クラス

BizTalk Server 2020 以降、BizTalk マップではカスタム XSLT 変換エンジンがサポートされています。 カスタム XSLT 変換エンジンを実装するには、アセンブリ Microsoft.XLANGs.BaseTypes.dllで抽象クラス Microsoft.XLANGs.BaseTypes.ITransform2 から派生した XSLT 変換実装を定義します。

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

実装例

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

参照

XSLT カスタム変換エンジン プロパティ

カスタム拡張 XML