Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
XSL.cs
1using System;
2using System.Reflection;
3using System.IO;
4using System.Text;
5using System.Xml;
6using System.Xml.Schema;
7using System.Xml.Xsl;
8
9namespace Waher.Content.Xsl
10{
14 public static class XSL
15 {
23 public static XmlSchema LoadSchema(string ResourceName)
24 {
25 return LoadSchema(ResourceName, Resources.GetAssembly(ResourceName));
26 }
27
36 public static XmlSchema LoadSchema(string ResourceName, Assembly Assembly)
37 {
38 using (Stream f = Assembly.GetManifestResourceStream(ResourceName))
39 {
40 if (f is null)
41 throw new ArgumentException("Resource not found: " + ResourceName, nameof(ResourceName));
42
43 return LoadSchema(f, ResourceName);
44 }
45 }
46
54 public static XmlSchema LoadSchema(Stream Input, string ResourceName)
55 {
56 XmlValidator Validator = new XmlValidator(ResourceName);
57 XmlSchema Result = XmlSchema.Read(Input, Validator.ValidationCallback);
58
59 Validator.AssertNoError();
60
61 return Result;
62 }
63
70 public static XslCompiledTransform LoadTransform(string ResourceName)
71 {
72 return LoadTransform(ResourceName, Resources.GetAssembly(ResourceName));
73 }
74
82 public static XslCompiledTransform LoadTransform(string ResourceName, Assembly Assembly)
83 {
84 using (Stream f = Assembly.GetManifestResourceStream(ResourceName))
85 {
86 if (f is null)
87 throw new ArgumentException("Resource not found: " + ResourceName, nameof(ResourceName));
88
89 return LoadTransform(f);
90 }
91 }
92
98 public static XslCompiledTransform LoadTransform(Stream Input)
99 {
100 using (XmlReader r = XmlReader.Create(Input))
101 {
102 XslCompiledTransform Xslt = new XslCompiledTransform();
103 Xslt.Load(r);
104
105 return Xslt;
106 }
107 }
108
109 #region Validation
110
118 public static void Validate(string ObjectID, XmlDocument Xml, params XmlSchema[] Schemas)
119 {
120 Validate(ObjectID, Xml, string.Empty, string.Empty, Schemas);
121 }
122
132 public static void Validate(string ObjectID, XmlDocument Xml, string ExpectedRootElement, string ExpectedRootElementNamespace,
133 params XmlSchema[] Schemas)
134 {
135 if (!string.IsNullOrEmpty(ExpectedRootElement) &&
136 (Xml.DocumentElement is null ||
137 Xml.DocumentElement.LocalName != ExpectedRootElement ||
138 Xml.DocumentElement.NamespaceURI != ExpectedRootElementNamespace))
139 {
140 throw new XmlSchemaException("Expected root element is " + ExpectedRootElement + " (" + ExpectedRootElementNamespace + ")");
141 }
142
143 foreach (XmlSchema Schema in Schemas)
144 Xml.Schemas.Add(Schema);
145
146 XmlValidator Validator = new XmlValidator(ObjectID);
147 Xml.Validate(Validator.ValidationCallback);
148
149 Validator.AssertNoError();
150 }
151
152 #endregion
153
154 #region Transformation
155
162 public static string Transform(string XML, XslCompiledTransform Transform)
163 {
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;
170
171 XmlWriter XmlWriter = XmlWriter.Create(OutputText, Settings);
172
173 try
174 {
175 Transform.Transform(XmlReader, XmlWriter);
176 }
177 finally
178 {
179 XmlWriter.Flush();
180 OutputText.Flush();
181
182 XmlReader.Close();
183 InputText.Dispose();
184
185 XmlWriter.Close();
186 OutputText.Dispose();
187 }
188
189 return Output.ToString();
190 }
191
192 #endregion
193
194 }
195}
Static class managing loading of resources stored as embedded resources or in content files.
Definition: Resources.cs:15
static Assembly GetAssembly(string ResourceName)
Gets the assembly corresponding to a given resource name.
Definition: Resources.cs:22
Static class managing loading of XSL resources stored as embedded resources or in content files.
Definition: XSL.cs:15
static XmlSchema LoadSchema(string ResourceName, Assembly Assembly)
Loads an XML schema from an embedded resource.
Definition: XSL.cs:36
static XmlSchema LoadSchema(Stream Input, string ResourceName)
Loads an XML schema from an embedded resource.
Definition: XSL.cs:54
static XmlSchema LoadSchema(string ResourceName)
Loads an XML schema from an embedded resource.
Definition: XSL.cs:23
static XslCompiledTransform LoadTransform(string ResourceName)
Loads an XSL transformation from an embedded resource.
Definition: XSL.cs:70
static XslCompiledTransform LoadTransform(string ResourceName, Assembly Assembly)
Loads an XSL transformation from an embedded resource.
Definition: XSL.cs:82
static string Transform(string XML, XslCompiledTransform Transform)
Transforms an XML document using an XSL transform.
Definition: XSL.cs:162
static void Validate(string ObjectID, XmlDocument Xml, params XmlSchema[] Schemas)
Validates an XML document given a set of XML schemas.
Definition: XSL.cs:118
static XslCompiledTransform LoadTransform(Stream Input)
Loads an XSL transformation from an embedded resource.
Definition: XSL.cs:98
static void Validate(string ObjectID, XmlDocument Xml, string ExpectedRootElement, string ExpectedRootElementNamespace, params XmlSchema[] Schemas)
Validates an XML document given a set of XML schemas.
Definition: XSL.cs:132