问题
I have some legacy XSLT scripts that incorporate VBScript in them. They are run on an old system and I can't change that system.
I am required to make a change to an XSLT to transform a file differently now.
I built a simple .NET project to test my XSLT transform:
[STAThread]
public static void Main(string[] args)
{
var transform = new XslCompiledTransform(true);
//foreach (var file in System.Reflection.Assembly
// .GetExecutingAssembly().GetManifestResourceNames()
// )
//{
// Console.WriteLine(file);
//}
//Console.ReadKey();
transform.Load(XmlTextReaderFromEmbeddedResource("MyXSLTFile"),
new XsltSettings() { EnableDocumentFunction = true, EnableScript = true }, new XmlUrlResolver());
transform.Transform(
XmlTextReaderFromEmbeddedResource("MySourceXML"),
ToXmlTextWriter("MyOutput.xml"));
}
private static XmlTextReader XmlTextReaderFromEmbeddedResource(string resourceName)
{
var resource = typeof(Transform)
.Assembly.GetManifestResourceStream
(resourceName);
return new XmlTextReader(resource);
}
private static XmlTextWriter ToXmlTextWriter(string fileName)
{
return new XmlTextWriter(fileName, Encoding.UTF8);
}
this works, procedurally. However, the scripts in the XSLT, being VBScript, aren't playing well with .NET. Specifically, I have a segment:
dim gRegEx
set gRegEx = New RegExp
which bombs the transform as:
Type 'RegExp' is not defined.
There's lots of articles about how to convert this to a .NET object, but this needs to go back to a legacy machine that will be expecting VBScript.
How can I write this so that it will work in both environments?
回答1:
The simplest approach to avoid using XslCompiledTransform. Instead use COM-Interop to utalise MSXML3 or MSXML6 to perform the transform. That way its much more likely that the transform as performed hosted in .NET will do the same when the transform is performed in classic ASP.
Edit:
Example.
Create a Windows Console C# Project
Add a reference to the COM Component Name "Microsoft XML, v3.0".
Include this code:-
class Program
{
static void Main(string[] args)
{
try
{
var dom = new MSXML2.DOMDocument30();
dom.async = false;
dom.load(args[0]);
var xslt = new MSXML2.DOMDocument30();
xslt.async = false;
xslt.load(args[1]);
File.WriteAllText(args[2], dom.transformNode(xslt));
Console.WriteLine("Done");
}
finally
{
Console.ReadKey();
}
}
}
Now in the project properties Debug tab add the three command line parameters, the file path to the input XML, the XSLT file to transform it and the output file path.
You can now incoporate this in a larger .NET project that can make use of an XSL designed for use by ASP Classis and/or that contains older MS script code.
来源:https://stackoverflow.com/questions/10126541/using-asp-objects-in-net-max-compatibility