-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathXsltCompiledEngine.cs
More file actions
812 lines (712 loc) · 27.6 KB
/
XsltCompiledEngine.cs
File metadata and controls
812 lines (712 loc) · 27.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Xsl;
using System.Xml.XPath;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Saxon.Api;
namespace XsltDebugger.DebugAdapter;
public enum StepMode
{
Continue, // No stepping, only break on breakpoints
Into, // Step into templates/function calls
Over, // Step over templates/function calls
Out // Run until returning from current depth
}
public class XsltCompiledEngine : BaseXsltEngine
{
private const string DebugNamespace = "urn:xslt-debugger";
public static readonly HashSet<string> InlineInstrumentationTargets = new(StringComparer.OrdinalIgnoreCase)
{
"template",
"if",
"for-each",
"when",
"otherwise",
"element",
"attribute",
"comment",
"processing-instruction",
// XSLT 2.0/3.0 specific elements
"iterate",
"for-each-group",
"analyze-string",
"matching-substring",
"non-matching-substring",
"try",
"catch"
};
public static readonly HashSet<string> ElementsDisallowingChildInstrumentation = new(StringComparer.OrdinalIgnoreCase)
{
"apply-templates",
"call-template",
"copy",
"copy-of",
"value-of",
"number",
"sort",
"message",
"text",
"attribute", // xsl:attribute can only contain text/value-of, not debug calls
// Structural control element that only allows xsl:when/xsl:otherwise children
"choose",
// XSLT 2.0/3.0 specific elements that don't allow child instrumentation
"next-iteration",
"break",
"sequence",
"perform-sort"
};
// Note: Shared fields moved to BaseXsltEngine (lines 71-83 removed)
public override async Task StartAsync(string stylesheet, string xml, bool stopOnEntry)
{
// Run the compiled engine on a background thread to avoid blocking the DAP message loop
if (XsltEngineManager.TraceEnabled)
{
XsltEngineManager.NotifyOutput("[trace] Compiled.Start: scheduling");
}
await Task.Run(() =>
{
try
{
if (XsltEngineManager.TraceEnabled)
{
XsltEngineManager.NotifyOutput("[trace] Compiled.Start: entered");
}
if (string.IsNullOrWhiteSpace(stylesheet) || string.IsNullOrWhiteSpace(xml))
{
XsltEngineManager.NotifyOutput("Launch request missing stylesheet or xml path.");
XsltEngineManager.NotifyTerminated(1);
return;
}
_currentStylesheet = NormalizePath(stylesheet);
var inputPath = NormalizePath(xml);
var xslt = new XslCompiledTransform();
XDocument xdoc;
try
{
xdoc = XDocument.Load(stylesheet, LoadOptions.SetLineInfo);
}
catch (Exception ex)
{
XsltEngineManager.NotifyOutput($"Failed to load XSLT stylesheet: {ex.Message}");
XsltEngineManager.NotifyTerminated(1);
return;
}
if (xdoc.Root == null || !IsXsltStylesheet(xdoc.Root))
{
XsltEngineManager.NotifyOutput("The specified file is not a valid XSLT stylesheet.");
XsltEngineManager.NotifyTerminated(1);
return;
}
// Validate engine compatibility
try
{
XsltCompiledEngine.ValidateEngineCompatibility(stylesheet, XsltEngineType.Compiled);
}
catch (Exception ex)
{
XsltEngineManager.NotifyOutput($"Engine validation failed: {ex.Message}");
XsltEngineManager.NotifyTerminated(1);
return;
}
// Extract and register stylesheet namespaces for XPath evaluation in watch expressions
ExtractAndRegisterNamespaces(xdoc);
// XSLT 1.0 with XslCompiledTransform
XNamespace msxsl = "urn:schemas-microsoft-com:xslt";
var scripts = xdoc.Descendants(msxsl + "script").ToList();
var args = new XsltArgumentList();
var messageHandler = new CompiledMessageHandler();
args.XsltMessageEncountered += messageHandler.OnMessageEncountered;
var extNamespace = xdoc.Root?.GetNamespaceOfPrefix("ext")?.NamespaceName;
if (!string.IsNullOrWhiteSpace(extNamespace))
{
args.AddExtensionObject(extNamespace, new RoslynEvaluator());
}
else
{
var evalNs = "urn:my-ext-eval";
args.AddExtensionObject(evalNs, new RoslynEvaluator());
xdoc.Root?.SetAttributeValue(XNamespace.Xmlns + "myeval", evalNs);
}
foreach (var script in scripts)
{
var language = script.Attribute("language")?.Value ?? "C#";
var implements = script.Attribute("implements-prefix")?.Value;
var code = script.Value;
if (language.IndexOf("c#", StringComparison.OrdinalIgnoreCase) >= 0 &&
!string.IsNullOrWhiteSpace(code) &&
!string.IsNullOrWhiteSpace(implements))
{
var extensionObj = CompileAndCreateExtensionObject(code);
var ns = xdoc.Root?.GetNamespaceOfPrefix(implements)?.NamespaceName;
var namespaceUri = !string.IsNullOrWhiteSpace(ns) ? ns : $"urn:my-ext-{implements}";
args.AddExtensionObject(namespaceUri, extensionObj);
}
}
if (scripts.Count > 0)
{
xdoc.Descendants(msxsl + "script").Remove();
}
EnsureDebugNamespace(xdoc);
Xslt1Instrumentation.InstrumentStylesheet(xdoc, _currentStylesheet, DebugNamespace, addProbeAttribute: false);
Xslt1Instrumentation.InstrumentVariables(xdoc, DebugNamespace, addProbeAttribute: false);
// Note: XsltCompiledEngine is XSLT 1.0 only, no xsl:function support
// DEBUG: Save instrumented XSLT for inspection
if (XsltEngineManager.IsTraceEnabled)
{
var debugPath = Path.Combine(Path.GetDirectoryName(_currentStylesheet) ?? ".", "out",
Path.GetFileNameWithoutExtension(_currentStylesheet) + ".instrumented.xslt");
Directory.CreateDirectory(Path.GetDirectoryName(debugPath) ?? ".");
xdoc.Save(debugPath);
XsltEngineManager.NotifyOutput($"[trace] Saved instrumented XSLT to: {debugPath}");
}
var settings = new XsltSettings(enableDocumentFunction: false, enableScript: false);
args.AddExtensionObject(DebugNamespace, new XsltDebugExtension(this, _currentStylesheet));
using (var reader = xdoc.CreateReader())
{
xslt.Load(reader, settings, new XmlUrlResolver());
}
if (stopOnEntry && XsltEngineManager.DebugEnabled)
{
PauseForBreakpoint(_currentStylesheet, 0, DebugStopReason.Entry, null);
}
using var xmlReader = XmlReader.Create(inputPath);
var stylesheetDir = Path.GetDirectoryName(_currentStylesheet) ?? Directory.GetCurrentDirectory();
var outDir = Path.Combine(stylesheetDir, "out");
Directory.CreateDirectory(outDir);
var stylesheetFileName = Path.GetFileNameWithoutExtension(_currentStylesheet);
var outPath = Path.Combine(outDir, $"{stylesheetFileName}.out.xml");
if (XsltEngineManager.IsLogEnabled)
{
XsltEngineManager.NotifyOutput($"Writing transform output to: {outPath}");
}
if (string.IsNullOrWhiteSpace(outPath))
{
XsltEngineManager.NotifyOutput("Output path for transform is empty; skipping write.");
}
else
{
using var fs = File.Create(outPath);
using var writer = XmlWriter.Create(fs, xslt.OutputSettings ?? new XmlWriterSettings { Indent = true });
xslt.Transform(xmlReader, args, writer);
}
if (XsltEngineManager.IsLogEnabled)
{
XsltEngineManager.NotifyOutput("Transform completed successfully.");
}
XsltEngineManager.NotifyTerminated(0);
}
catch (Exception ex)
{
XsltEngineManager.NotifyOutput($"Transform failed: {ex}");
XsltEngineManager.NotifyTerminated(1);
}
finally
{
lock (_sync)
{
_pauseTcs?.TrySetResult(true);
_pauseTcs = null;
}
}
});
}
public override Task ContinueAsync()
{
lock (_sync)
{
_nextStepRequested = false;
_stepMode = StepMode.Continue;
_pauseTcs?.TrySetResult(true);
_pauseTcs = null;
}
return Task.CompletedTask;
}
public override Task StepOverAsync()
{
lock (_sync)
{
_nextStepRequested = true;
_stepMode = StepMode.Over;
_targetDepth = _callDepth;
_stepOriginFile = _currentStopFile;
_stepOriginLine = _currentStopLine;
_pauseTcs?.TrySetResult(true);
_pauseTcs = null;
}
return Task.CompletedTask;
}
public override Task StepInAsync()
{
lock (_sync)
{
_nextStepRequested = true;
_stepMode = StepMode.Into;
_targetDepth = _callDepth;
_stepOriginFile = string.Empty;
_stepOriginLine = -1;
_pauseTcs?.TrySetResult(true);
_pauseTcs = null;
}
return Task.CompletedTask;
}
public override Task StepOutAsync()
{
lock (_sync)
{
_nextStepRequested = true;
_stepMode = StepMode.Out;
_targetDepth = _callDepth - 1; // Stop when we return to parent depth
_stepOriginFile = _currentStopFile;
_stepOriginLine = _currentStopLine;
_pauseTcs?.TrySetResult(true);
_pauseTcs = null;
}
return Task.CompletedTask;
}
// Note: SetBreakpoints method moved to BaseXsltEngine
internal void RegisterBreakpointHit(string file, int line, XPathNavigator? contextNode = null, bool isTemplateEntry = false, bool isTemplateExit = false)
{
if (line < 0)
{
return;
}
var normalized = NormalizePath(file);
// Track call depth for template entry/exit
lock (_sync)
{
if (isTemplateEntry)
{
_callDepth++;
}
else if (isTemplateExit)
{
_callDepth = Math.Max(0, _callDepth - 1);
}
}
// Always update the context for evaluation, even when not pausing
var clonedContext = CloneContextNavigator(contextNode);
XsltEngineManager.UpdateContext(clonedContext);
// Track current line for inline C# method logging
XsltEngineManager.UpdateCurrentLine(normalized, line);
// Check if we hit a user-set breakpoint
if (!isTemplateExit && IsBreakpointHit(normalized, line))
{
PauseForBreakpoint(normalized, line, DebugStopReason.Breakpoint, clonedContext);
return;
}
// Check if we should stop based on step mode
if (ShouldStopForStep(normalized, line, isTemplateExit))
{
PauseForBreakpoint(normalized, line, DebugStopReason.Step, clonedContext);
}
}
private bool ShouldStopForStep(string file, int line, bool isTemplateExit)
{
lock (_sync)
{
if (!_nextStepRequested)
{
return false;
}
var shouldStop = false;
switch (_stepMode)
{
case StepMode.Continue:
shouldStop = false;
break;
case StepMode.Into:
// Stop at any line (including deeper calls)
shouldStop = !isTemplateExit;
break;
case StepMode.Over:
// Stop once we return to the original depth, but skip synthetic template exits
shouldStop = !isTemplateExit && _callDepth <= _targetDepth &&
(!string.Equals(file, _stepOriginFile, StringComparison.OrdinalIgnoreCase) || line != _stepOriginLine);
break;
case StepMode.Out:
// Stop when we've returned to or above the original depth (allow template exit to satisfy step out)
shouldStop = _callDepth <= _targetDepth;
break;
default:
shouldStop = false;
break;
}
if (shouldStop)
{
_nextStepRequested = false;
}
return shouldStop;
}
}
// Note: IsBreakpointHit method moved to BaseXsltEngine
private void PauseForBreakpoint(string file, int line, DebugStopReason reason, XPathNavigator? context)
{
if (!XsltEngineManager.DebugEnabled)
{
return;
}
TaskCompletionSource<bool>? localTcs;
lock (_sync)
{
_pauseTcs = new TaskCompletionSource<bool>();
localTcs = _pauseTcs;
}
XsltEngineManager.NotifyStopped(file, line, reason, context);
lock (_sync)
{
_currentStopFile = file;
_currentStopLine = line;
}
try
{
localTcs?.Task.Wait();
}
catch
{
// Ignore interruption
}
}
// Note: NormalizePath method moved to BaseXsltEngine
private static void ExtractAndRegisterNamespaces(XDocument xdoc)
{
if (xdoc.Root == null)
{
return;
}
var namespaces = new Dictionary<string, string>();
// Extract all namespace declarations from the root element
foreach (var attr in xdoc.Root.Attributes())
{
if (attr.IsNamespaceDeclaration)
{
var prefix = attr.Name.LocalName == "xmlns" ? string.Empty : attr.Name.LocalName;
var uri = attr.Value;
// Skip XSLT namespace and debug namespace
if (uri != "http://www.w3.org/1999/XSL/Transform" &&
uri != DebugNamespace)
{
// For default namespace (no prefix), also register with "default" prefix
// This allows XPath expressions to use "default:ElementName" syntax
if (string.IsNullOrEmpty(prefix))
{
namespaces[prefix] = uri;
namespaces["default"] = uri;
}
else
{
namespaces[prefix] = uri;
}
}
}
}
XsltEngineManager.RegisterStylesheetNamespaces(namespaces);
}
private void EnsureDebugNamespace(XDocument doc)
{
if (doc.Root == null)
{
return;
}
var existing = doc.Root.GetNamespaceOfPrefix("dbg");
if (existing == null || existing.NamespaceName != DebugNamespace)
{
doc.Root.SetAttributeValue(XNamespace.Xmlns + "dbg", DebugNamespace);
}
}
private static XPathNavigator? CloneContextNavigator(XPathNavigator? context)
{
if (context == null)
{
return null;
}
try
{
var originalClone = context.Clone();
var pathToContext = GetXPathToNavigator(originalClone);
// Move to the document root and capture the XML backing the navigator
originalClone.MoveToRoot();
if (!originalClone.MoveToFirstChild())
{
return context.Clone();
}
while (originalClone.NodeType != XPathNodeType.Element)
{
if (!originalClone.MoveToNext())
{
return context.Clone();
}
}
var xml = originalClone.OuterXml;
var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);
var navigator = xmlDoc.CreateNavigator();
if (navigator == null)
{
return context.Clone();
}
if (!string.IsNullOrEmpty(pathToContext) && pathToContext != "/")
{
var nsManager = BuildNamespaceManager(xmlDoc);
var positioned = navigator.SelectSingleNode(pathToContext, nsManager);
if (positioned != null)
{
return positioned.Clone();
}
}
return navigator;
}
catch
{
try
{
return context.Clone();
}
catch
{
return null;
}
}
}
private static XmlNamespaceManager BuildNamespaceManager(XmlDocument xmlDoc)
{
var manager = new XmlNamespaceManager(xmlDoc.NameTable);
if (xmlDoc.DocumentElement == null)
{
return manager;
}
try
{
var navigator = xmlDoc.CreateNavigator();
if (navigator != null && navigator.MoveToFirstChild())
{
foreach (var kvp in navigator.GetNamespacesInScope(XmlNamespaceScope.All))
{
var prefix = kvp.Key ?? string.Empty;
try { manager.AddNamespace(prefix, kvp.Value); }
catch { /* ignore duplicates */ }
}
}
}
catch
{
// Ignore namespace extraction issues; fall back to empty manager
}
return manager;
}
private static string GetXPathToNavigator(XPathNavigator navigator)
{
try
{
var pathParts = new List<string>();
var current = navigator.Clone();
while (current.NodeType != XPathNodeType.Root)
{
switch (current.NodeType)
{
case XPathNodeType.Element:
{
var position = 1;
var sibling = current.Clone();
while (sibling.MoveToPrevious())
{
if (sibling.Name == current.Name)
{
position++;
}
}
pathParts.Insert(0, $"{current.Name}[{position}]");
break;
}
case XPathNodeType.Attribute:
{
pathParts.Insert(0, $"@{current.Name}");
break;
}
case XPathNodeType.Text:
{
pathParts.Insert(0, "text()");
break;
}
default:
pathParts.Insert(0, current.NodeType.ToString());
break;
}
if (!current.MoveToParent())
{
break;
}
}
return pathParts.Count > 0 ? "/" + string.Join("/", pathParts) : "/";
}
catch
{
return "/";
}
}
private static List<string> ExtractUsingStatements(string code)
{
var usings = new List<string>();
var lines = code.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var line in lines)
{
var trimmed = line.Trim();
if (trimmed.StartsWith("using ", StringComparison.Ordinal))
{
usings.Add(trimmed);
}
}
return usings;
}
private object CompileAndCreateExtensionObject(string code)
{
var classCode = code.Contains("class", StringComparison.OrdinalIgnoreCase)
? code
: $"public class InlineXsltExt {{ {code} }}";
// Build prelude with only the using statements not already present in the code
var requiredUsings = new[]
{
"using System;",
"using System.Collections;",
"using System.Collections.Generic;",
"using System.Globalization;",
"using System.Linq;",
"using System.Text;",
"using System.Xml;",
"using System.Xml.XPath;",
"using System.Xml.Xsl;",
"using XsltDebugger.DebugAdapter;",
"using static XsltDebugger.DebugAdapter.InlineXsltLogger;"
};
var existingUsings = ExtractUsingStatements(classCode);
var prelude = string.Join(Environment.NewLine,
requiredUsings.Where(u => !existingUsings.Contains(u, StringComparer.OrdinalIgnoreCase)));
var sourceCode = string.Concat(prelude, Environment.NewLine, classCode);
// Instrument inline C# methods when debugging is enabled
if (XsltEngineManager.DebugEnabled)
{
sourceCode = InlineCSharpInstrumenter.Instrument(sourceCode);
}
var syntaxTree = Microsoft.CodeAnalysis.CSharp.CSharpSyntaxTree.ParseText(sourceCode);
var references = new List<MetadataReference>
{
MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
MetadataReference.CreateFromFile(typeof(XsltArgumentList).Assembly.Location),
MetadataReference.CreateFromFile(typeof(System.Xml.XmlDocument).Assembly.Location),
MetadataReference.CreateFromFile(typeof(System.Linq.Enumerable).Assembly.Location),
MetadataReference.CreateFromFile(typeof(System.Runtime.GCSettings).Assembly.Location),
MetadataReference.CreateFromFile(typeof(XsltEngineManager).Assembly.Location),
MetadataReference.CreateFromFile(typeof(InlineXsltLogger).Assembly.Location)
};
var coreLibDirectory = Path.GetDirectoryName(typeof(object).Assembly.Location);
if (!string.IsNullOrEmpty(coreLibDirectory))
{
var runtimeAssemblyPath = Path.Combine(coreLibDirectory, "System.Runtime.dll");
if (File.Exists(runtimeAssemblyPath))
{
references.Add(MetadataReference.CreateFromFile(runtimeAssemblyPath));
}
}
var compilation = Microsoft.CodeAnalysis.CSharp.CSharpCompilation.Create(
"InlineXsltExtAssembly",
new[] { syntaxTree },
references,
new Microsoft.CodeAnalysis.CSharp.CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
using var ms = new MemoryStream();
var result = compilation.Emit(ms);
if (!result.Success)
{
throw new Exception("Failed to compile inline C# script: " + string.Join("\n", result.Diagnostics));
}
ms.Seek(0, SeekOrigin.Begin);
var assembly = Assembly.Load(ms.ToArray());
var allTypes = assembly.GetTypes();
var usableTypes = allTypes
.Where(t =>
!t.IsAbstract &&
!t.IsInterface &&
!t.IsGenericType &&
!t.IsGenericTypeDefinition &&
!Attribute.IsDefined(t, typeof(System.Runtime.CompilerServices.CompilerGeneratedAttribute), inherit: false))
.ToList();
Type? chosen = usableTypes
.FirstOrDefault(t => t.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly).Length > 0)
?? usableTypes.FirstOrDefault(t => !t.IsNested)
?? usableTypes.FirstOrDefault()
?? allTypes.FirstOrDefault(t => !t.IsAbstract && !t.IsInterface);
if (chosen == null)
{
throw new Exception("Compiled assembly does not contain a usable type for XSLT extension.");
}
return Activator.CreateInstance(chosen) ?? throw new Exception("Failed to instantiate compiled extension type.");
}
public static bool IsXsltStylesheet(XElement root)
{
var ns = root.Name.NamespaceName;
return ns == "http://www.w3.org/1999/XSL/Transform" && (root.Name.LocalName == "stylesheet" || root.Name.LocalName == "transform");
}
public static decimal GetXsltVersion(XElement root)
{
if (!IsXsltStylesheet(root))
{
return 0;
}
var versionAttr = root.Attribute("version");
if (versionAttr != null && decimal.TryParse(versionAttr.Value, out var version))
{
return version;
}
// Default version if not specified
return 1.0m;
}
public static bool HasInlineCSharp(XDocument doc)
{
var msxsl = "urn:schemas-microsoft-com:xslt";
return doc.Descendants(XName.Get("script", msxsl)).Any();
}
public static void ValidateEngineCompatibility(string stylesheet, XsltEngineType engineType)
{
XDocument xdoc;
try
{
xdoc = XDocument.Load(stylesheet, LoadOptions.SetLineInfo);
}
catch (Exception ex)
{
throw new Exception($"Failed to load XSLT stylesheet for validation: {ex.Message}");
}
if (xdoc.Root == null || !IsXsltStylesheet(xdoc.Root))
{
throw new Exception("The specified file is not a valid XSLT stylesheet.");
}
var version = GetXsltVersion(xdoc.Root);
var hasInlineCSharp = HasInlineCSharp(xdoc);
if (engineType == XsltEngineType.Compiled)
{
// Compiled engine supports XSLT 1.0 and inline C#
// Also usable for XSLT 2.0/3.0 with limited feature support
if (version >= 2.0m)
{
XsltEngineManager.NotifyOutput($"Warning: Using 'compiled' engine with XSLT {version}. XSLT 2.0+ features may not be fully supported. Consider using 'saxon' engine for XSLT 2.0/3.0.");
}
}
else if (engineType == XsltEngineType.SaxonNet)
{
// Saxon engine supports XSLT 2.0/3.0 but not inline C#
if (version < 2.0m)
{
XsltEngineManager.NotifyOutput($"Warning: Using Saxon engine with XSLT {version}. Saxon is optimized for XSLT 2.0/3.0. Consider using 'compiled' engine for XSLT 1.0.");
}
if (hasInlineCSharp)
{
throw new Exception("Saxon engine does not support inline C# scripts. Use 'compiled' engine for stylesheets with msxsl:script elements.");
}
}
}
}