Beispiel #1
0
		/// <summary>
		/// Converts source XML with given transform with applying DSL
		/// </summary>
		/// <param name="xml"> </param>
		/// <returns>XElement with data, converted by transform</returns>
		/// <exception cref="XmlDslLoaderException">
		/// <list type="bullet">
		/// <item><description>cannot find transform file</description></item>
		/// <item><description>any problem with underlined XmlDslDefinition</description></item>
		/// </list>
		/// </exception>
		public XElement Load(XElement xml) {
			if (null == xml.Element("transform")) return xml; //no dsl needed
			var trdef = new XmlDslDefinition().Extract(xml);
			var langfile = Resolver.Resolve(Rootdir+"/" + trdef.LangName + ".xslt", false);
			if (!File.Exists(langfile))
			{
				throw new XmlDslLoaderException("Не возможно найти файл XML языка" + langfile);
			}
			var xsl = new XslCompiledTransform();
			if(trdef.NeedPrepareXslt) {
				var xsltcontent = XElement.Load(langfile);
				xsltcontent = trdef.PrepareXslt(xsltcontent);
				var resolver = new XmlDslRootBasedXmlUrlResolver(langfile);
				xsl.Load(xsltcontent.CreateReader(),XsltSettings.TrustedXslt,resolver);
			}else {
				xsl.Load(langfile, XsltSettings.TrustedXslt, new XmlUrlResolver());
			}
			var args = trdef.CreateArguments();
			var sw = new StringWriter();
			using (var xw = XmlWriter.Create(sw)) {
				xsl.Transform(xml.CreateReader(),args,xw);
				xw.Flush();
			}
			return XElement.Parse(sw.ToString());
		}
		public void setup() {
			_src = MyBxl.Parse(code);
			_def = new XmlDslDefinition().Extract(_src);
		}