Web Log di Adrian Florea

"You know you've achieved perfection in design, not when you have nothing more to add, but when you have nothing more to take away." Antoine de Saint-Exupery
posts - 440, comments - 2715, trackbacks - 3944

My Links

Archives

Post Categories

Image Galleries

.RO Blogs

.RO People

.RO Sites

Blogs

Furls

Links

vinCitori

Le magie di XslCompiledTransform

Appena ho visto la nuova XslCompiledTransform (in 2.0) è stato colpo di fulmine. E da questo esempietto fatto al volo, solo per rendere l'idea, capirete subito perché:

using System;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Xsl;
 
class Test
{
      static void Main()
      {
            string methodName = "Foo";
            StringBuilder methodCode = new StringBuilder();
 
            methodCode.AppendLine  ("// ritorna la stringa Ciao ragazzi!");
            methodCode.AppendFormat("public string {0}()", methodName);
            methodCode.AppendLine  ("{");
            methodCode.AppendLine  ("   StringBuilder sb = new StringBuilder();");
            methodCode.AppendLine  ("   sb.Append(\"Ciao \");");
            methodCode.AppendLine  ("   sb.Append(\"ragazzi!\");");
            methodCode.AppendLine  ("   return sb.ToString();");
            methodCode.AppendLine  ("}");
 
            Console.WriteLine(ExecuteMethodCode(methodCode.ToString(), methodName));
      }
 
      static string ExecuteMethodCode(string methodCode, string methodName)
      {
            StringBuilder xsl = new StringBuilder();
            StringBuilder result = new StringBuilder();
 
            string prefix = "my-prefix";
            string urn = "my-urn";
 
            xsl.AppendLine("<?xml version=\"1.0\" ?>");
            xsl.AppendLine("<xsl:stylesheet version=\"1.0\"");
            xsl.AppendLine("xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"");
            xsl.AppendLine("xmlns:msxsl=\"urn:schemas-microsoft-com:xslt\"");
            xsl.AppendFormat("xmlns:{0}=\"urn:{1}\">", prefix, urn);
            xsl.AppendFormat("<msxsl:script language=\"C#\" implements-prefix=\"{0}\">", prefix);
            xsl.AppendFormat("<![CDATA[{0}]]>", methodCode);
            xsl.AppendLine("</msxsl:script>");
            xsl.AppendLine("<xsl:template match=\"/\">");
            xsl.AppendFormat("<xsl:value-of select=\"{0}:{1}()\"/>", prefix, methodName);
            xsl.AppendLine("</xsl:template>");
            xsl.AppendLine("</xsl:stylesheet>");
 
            XmlDocument dummyXml = new XmlDocument();
            dummyXml.CreateXmlDeclaration("1.0", null, null);
            dummyXml.CreateElement(new Guid().ToString());
 
            XmlReader reader = XmlReader.Create(new StringReader(xsl.ToString()));
            XmlTextWriter writer = new XmlTextWriter(new StringWriter(result));
 
            XslCompiledTransform compiledXsl = new XslCompiledTransform();
            compiledXsl.Load(reader, XsltSettings.TrustedXslt, null);
            compiledXsl.Transform(dummyXml, writer);
 
            return result.ToString();
      }
}

Cosa stampa a console? Ciao ragazzi! naturalmente... Bellissimo!

Print | posted on mercoledì 5 ottobre 2005 18:46 | Filed Under [ Carillon .NET ]

Powered by:
Powered By Subtext Powered By ASP.NET