|
eholder -> XslCompiledTransform and document() function (16 July 2007)
|
XslCompiledTransform.Transform appears to be trying to validate documents I load indirectly via the document() function. I only want to validate my source XML document against my schema. My XSL code simply calls document("c:/RefDoc.xml") to look up data in an external table. I don't really want to validate RefDoc.xml, though I don't mind doing so if I can tell it which schema to use. Here is my code (C# 2.0): public class XsltTransform { private string m_schemaValidationErrors = ""; public XsltTransform() {} public void transform(string sourcePath, string schemaPath, string xsltPath, string resultPath) { try { // Set up an XmlReader to read the source XML document. XmlReaderSettings settings = new XmlReaderSettings(); settings.ConformanceLevel = ConformanceLevel.Auto; settings.IgnoreWhitespace = true; settings.IgnoreComments = true; // Removing the following 3 statements allows it to run w/o validation. settings.Schemas.Add(null, schemaPath); settings.ValidationType = ValidationType.Schema; settings.ValidationEventHandler += new ValidationEventHandler(settings_ValidationEventHandler); XmlReader reader = XmlReader.Create(sourcePath, settings); // Set up a processor to run the XSL transform. // Since the XSLT code uses the "document()" function, enable that. // Also, for the same reason, we need an XmlResolver. XsltSettings xsltSettings = new XsltSettings(); xsltSettings.EnableDocumentFunction = true; XmlUrlResolver xur = new XmlUrlResolver(); XslCompiledTransform myXslTransform = new XslCompiledTransform(); myXslTransform.Load(xsltPath, xsltSettings, xur); // Now, perform the transform. XmlTextWriter writer = new XmlTextWriter(resultPath, null); myXslTransform.Transform(reader, null, writer, xur); writer.Close(); } catch (Exception e) { System.Console.WriteLine(e); } } void settings_ValidationEventHandler(object sender, ValidationEventArgs e) { m_schemaValidationErrors += e.Message; } } The error I am getting is: System.Xml.Xsl.XslTransformException: An error occurred while loading document 'RefDoc.xml'. See InnerException for a complete description of the error. ---> System.Xml.Schema.XmlSchemaValidationException: The 'RefDocRoot' element is not declared.
|
|
|
|