2using System.Reflection;
6using System.Xml.Schema;
14 public static class XSL
36 public static XmlSchema
LoadSchema(
string ResourceName, Assembly Assembly)
38 using (Stream f = Assembly.GetManifestResourceStream(ResourceName))
41 throw new ArgumentException(
"Resource not found: " + ResourceName, nameof(ResourceName));
54 public static XmlSchema
LoadSchema(Stream Input,
string ResourceName)
56 XmlValidator Validator =
new XmlValidator(ResourceName);
57 XmlSchema Result = XmlSchema.Read(Input, Validator.ValidationCallback);
59 Validator.AssertNoError();
82 public static XslCompiledTransform
LoadTransform(
string ResourceName, Assembly Assembly)
84 using (Stream f = Assembly.GetManifestResourceStream(ResourceName))
87 throw new ArgumentException(
"Resource not found: " + ResourceName, nameof(ResourceName));
100 using (XmlReader r = XmlReader.Create(Input))
102 XslCompiledTransform Xslt =
new XslCompiledTransform();
118 public static void Validate(
string ObjectID, XmlDocument Xml, params XmlSchema[] Schemas)
120 Validate(ObjectID, Xml,
string.Empty,
string.Empty, Schemas);
132 public static void Validate(
string ObjectID, XmlDocument Xml,
string ExpectedRootElement,
string ExpectedRootElementNamespace,
133 params XmlSchema[] Schemas)
135 if (!
string.IsNullOrEmpty(ExpectedRootElement) &&
136 (Xml.DocumentElement is
null ||
137 Xml.DocumentElement.LocalName != ExpectedRootElement ||
138 Xml.DocumentElement.NamespaceURI != ExpectedRootElementNamespace))
140 throw new XmlSchemaException(
"Expected root element is " + ExpectedRootElement +
" (" + ExpectedRootElementNamespace +
")");
143 foreach (XmlSchema Schema
in Schemas)
144 Xml.Schemas.Add(Schema);
146 XmlValidator Validator =
new XmlValidator(ObjectID);
147 Xml.Validate(Validator.ValidationCallback);
149 Validator.AssertNoError();
154 #region Transformation
164 StringBuilder Output =
new StringBuilder();
165 TextWriter OutputText =
new StringWriter(Output);
166 TextReader InputText =
new StringReader(XML);
167 XmlReader XmlReader = XmlReader.Create(InputText);
168 XmlWriterSettings Settings = Content.Xml.XML.WriterSettings(
false,
true);
169 Settings.ConformanceLevel = ConformanceLevel.Auto;
171 XmlWriter XmlWriter = XmlWriter.Create(OutputText, Settings);
175 Transform.Transform(XmlReader, XmlWriter);
186 OutputText.Dispose();
189 return Output.ToString();
Static class managing loading of resources stored as embedded resources or in content files.
static Assembly GetAssembly(string ResourceName)
Gets the assembly corresponding to a given resource name.
Static class managing loading of XSL resources stored as embedded resources or in content files.
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 an embedded resource.
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.