2using System.Reflection;
6using System.Xml.Schema;
15 public static class XSL
37 public static XmlSchema
LoadSchema(
string ResourceName, Assembly Assembly)
39 using (Stream f = Assembly.GetManifestResourceStream(ResourceName))
42 throw new ArgumentException(
"Resource not found: " + ResourceName, nameof(ResourceName));
55 public static XmlSchema
LoadSchema(
byte[] Input,
string ResourceName)
57 using (MemoryStream ms =
new MemoryStream(Input))
70 public static XmlSchema
LoadSchema(Stream Input,
string ResourceName)
72 XmlValidator Validator =
new XmlValidator(ResourceName);
73 XmlSchema Result = XmlSchema.Read(Input, Validator.ValidationCallback);
75 Validator.AssertNoError();
98 public static XslCompiledTransform
LoadTransform(
string ResourceName, Assembly Assembly)
100 using (Stream f = Assembly.GetManifestResourceStream(ResourceName))
103 throw new ArgumentException(
"Resource not found: " + ResourceName, nameof(ResourceName));
116 using (XmlReader r = XmlReader.Create(Input))
118 XslCompiledTransform Xslt =
new XslCompiledTransform();
134 public static void Validate(
string ObjectID, XmlDocument Xml, params XmlSchema[] Schemas)
136 Validate(ObjectID, Xml,
string.Empty,
string.Empty, Schemas);
148 public static void Validate(
string ObjectID, XmlDocument Xml,
string ExpectedRootElement,
string ExpectedRootElementNamespace,
149 params XmlSchema[] Schemas)
151 if (!
string.IsNullOrEmpty(ExpectedRootElement))
153 if (Xml.DocumentElement is
null)
154 throw new XmlSchemaException(
"Not root element.");
156 if (Xml.DocumentElement.LocalName != ExpectedRootElement)
158 throw new XmlSchemaException(
"Expected root element is " + ExpectedRootElement +
159 " but was " + Xml.DocumentElement.LocalName +
".");
162 if (Xml.DocumentElement.NamespaceURI != ExpectedRootElementNamespace)
164 throw new XmlSchemaException(
"Expected root element namespace is " + ExpectedRootElementNamespace +
165 " but was " + Xml.DocumentElement.NamespaceURI +
".");
169 lock (xmlSchemaSetSynchronization)
171 foreach (XmlSchema Schema
in Schemas)
172 Xml.Schemas.Add(Schema);
174 XmlValidator Validator =
new XmlValidator(ObjectID);
175 Xml.Validate(Validator.ValidationCallback);
177 Validator.AssertNoError();
185 private static readonly
object xmlSchemaSetSynchronization =
new object();
189 #region Transformation
199 StringBuilder Output =
new StringBuilder();
200 TextWriter OutputText =
new StringWriter(Output);
201 TextReader InputText =
new StringReader(XML);
202 XmlReader XmlReader = XmlReader.Create(InputText);
203 XmlWriterSettings Settings = Content.Xml.XML.WriterSettings(
false,
true);
204 Settings.ConformanceLevel = ConformanceLevel.Auto;
206 XmlWriter XmlWriter = XmlWriter.Create(OutputText, Settings);
210 Transform.Transform(XmlReader, XmlWriter);
221 OutputText.Dispose();
224 return Output.ToString();
Static class managing loading of XSL resources stored as embedded resources or in content files.
static XmlSchema LoadSchema(byte[] Input, string ResourceName)
Loads an XML schema from an array of bytes.
static XmlSchema LoadSchema(string ResourceName, Assembly Assembly)
Loads an XML schema from an embedded resource.
static XmlSchema LoadSchema(Stream Input, string ResourceName)
Loads an XML schema from a stream.
static XmlSchema LoadSchema(string ResourceName)
Loads an XML schema from an embedded resource.
static XslCompiledTransform LoadTransform(string ResourceName)
Loads an XSL transformation from an embedded resource.
static XslCompiledTransform LoadTransform(string ResourceName, Assembly Assembly)
Loads an XSL transformation from an embedded resource.
static string Transform(string XML, XslCompiledTransform Transform)
Transforms an XML document using an XSL transform.
static void Validate(string ObjectID, XmlDocument Xml, params XmlSchema[] Schemas)
Validates an XML document given a set of XML schemas.
static XslCompiledTransform LoadTransform(Stream Input)
Loads an XSL transformation from an embedded resource.
static void Validate(string ObjectID, XmlDocument Xml, string ExpectedRootElement, string ExpectedRootElementNamespace, params XmlSchema[] Schemas)
Validates an XML document given a set of XML schemas.
Static class that dynamically manages types and interfaces available in the runtime environment.
static Assembly GetAssemblyForResource(string ResourceName)
Gets the assembly corresponding to a given resource name.