示例#1
0
        /// <summary>
        /// Single action transform
        /// </summary>
        /// <param name="template">path for XSLT style-sheet</param>
        /// <param name="inputSource"></param>
        /// <param name="input">source XML file</param>
        /// <param name="output">resulting text content</param>
        public void Transform(string template, string inputSource, IXPathNavigable input, string output)
        {
            template = PathEx.FixUpPath(template);
            inputSource = PathEx.FixUpPath(inputSource);
            output = PathEx.FixUpPath(output);
            var sandbox = PathEx.FixUpPath(_sandbox);

            var xsltArgumentList = new XsltArgumentList().AddExtensions(_extensions);

            xsltArgumentList.XsltMessageEncountered += (sender, eventArgs) => Console.WriteLine($"\t\t[{Thread.CurrentThread.ManagedThreadId}]{eventArgs.Message}");

            XNamespace ns = this.GetXmlNamespace();
            xsltArgumentList.AddParam("files", "", new XElement(ns + "file",
                new XAttribute(nameof(template), Path.GetFullPath(template)),
                new XAttribute(nameof(input), Path.GetFullPath(inputSource)),
                new XAttribute(nameof(input) + "Type", input.GetType().AssemblyQualifiedName),
                new XAttribute(nameof(output), Path.GetFullPath(output)),
                new XAttribute(nameof(output) + "Path", Path.GetDirectoryName(Path.GetFullPath(output))),
                new XAttribute("sandbox", Path.GetFullPath(sandbox))
                ).ToXPathNavigable().CreateNavigator());

            var xslt = new XslCompiledTransform(false);
            using var xmlreader = XmlReader.Create(template, new XmlReaderSettings
            {
                DtdProcessing = DtdProcessing.Parse,
                ConformanceLevel = ConformanceLevel.Document,
                NameTable = new NameTable(),
            });
            var xsltSettings = new XsltSettings(false, false);

            xslt.Load(xmlreader, xsltSettings, null);

            var outputDir = Path.GetDirectoryName(output);
            if (!Directory.Exists(outputDir)) Directory.CreateDirectory(outputDir);
            using var resultStream = File.Create(output);

            var currentDirectory = Environment.CurrentDirectory;
            try
            {
                var localOutfolder = Path.GetDirectoryName(output);
                if (!Directory.Exists(localOutfolder))
                {
                    Directory.CreateDirectory(localOutfolder);
                }
                Environment.CurrentDirectory = localOutfolder;

                var inputNavigator = input.CreateNavigator();
                inputNavigator.MoveToRoot();

                //var x = xslt.GetType();
                //var pf = x.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
                //var pfc = pf.First(i => i.Name == "_command");
                //var pfv = pfc.GetValue(xslt);

                xslt.Transform(inputNavigator, xsltArgumentList, resultStream);
            }
            finally
            {
                Environment.CurrentDirectory = currentDirectory;
            }
        }