tags:

views:

642

answers:

4

I like .NET webcontrols and you manipulate things, that's common consensus, but XML and XSL is so great, because you have UI logic that is platform & language-independent, so one day I change the app to php, java or whatever and i can reuse all the presentation logic. Moreover, XSL has the possibility to call .NET (or whatever) methods before rendering.

When do you use XML/XSL normally? why no to use it more frequently?

+1  A: 

Not so much in the ASP.NET stuff, but before that (with VB6) I used to use it almost exclusively (at the server) to transform xml to html. I've always found it incredibly versatile. I also use it in my "protocol buffers" project as the code-generator engine: the main framework produces xml, and then I use an xsl transform to spit out the C#. I've heard people say they don't find xsl intuitive, but I really like it, and it is my default ttool when processing xml.

Right now, I'm looking lots at ASP.NET MVC, which doesn't necessarily lend itself to xsl much - although in some ways, there isn't much between <%=foo.Name%> and <xsl:value-of select="Name"/>.

Marc Gravell
+2  A: 

Instead of HTML?

I use it constantly in place of asp.net controls since it affords the separation of concerns for the V and the C in 2.0 that you don't get in .NET 2.0 out of the box.

Obviously there's a million other uses unrelated to asp.net controls.


Edit: a sketch of an implementation

public class xsltmanager
{
    /* constructor (singleton) which defines a file watcher for *.xsl in the path of your choice */

    //just a mutex for thread safety
    private object Mutex = new object();

    //caching XslCompiledTransforms
    private Dictionary<string, XslCompiledTransform> cTransforms = new Dictionary<string, XslCompiledTransform>();

    public XslCompiledTransform fetch(string identifier)
    {    
     if (!this.cTransforms.ContainsKey(identifier))
     {
      lock (this.Mutex)
      {
       if (!this.cTransforms.ContainsKey(identifier))
       {
        XslCompiledTransform xslDoc = new XslCompiledTransform();
        xslDoc.Load(/* file path based on identifier */);

        this.cTransforms.Add(identifier, xslDoc);
       }
      }
     }
     return this.cTransforms[identifier];
    }

    /* other util xslt methods - namespace wash, doc merge, whatever */
}

public class myPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
     //get source data
     XPathDocument xPathDoc = myGetXMLMethod();

     //transform params
     XsltArgumentList oArgs = new XsltArgumentList();

     /* add params as required */

     //fetching and executing the transform directly to the Response here
     xsltmanager.instance.get(@"foo\bar\baz").Transform(xPathDoc, oArgs, Response.OutputStream);
    }
}
annakata
how do you integrate it with .NET?
netadictos
Essentially just transforming against Response.OutputStream in the codebehind.
annakata
if you don't mind, could you provide some code of integration of a .aspx (or whatever) with xml/xsl?thanks in advance
netadictos
A: 

There is a huge overhead on using XML/XSL and there are many disadvantages.

  1. You need a complete XML dataset to ensure that the XSL is only used as a logic engine.
  2. Second, the XSL logic control is poor and is inconsistent between versions.
  3. Third, the combination is quite an heavy process, unsuitable for large sites.

If you are concerned about logic separation, use some templating language (that is not XSL).

Vincent
I reject all of this: XSL is not a logic engine, it's a presentation engine. XSLT's "logic" for templating is extraordinarily powerful and pretty damned consistent within any given framework. Given the language is *designed* for templating it's also very fast. Cache to avoid file IO and use XPathDoc
annakata
retaliatory downvoting? well done you
annakata
This answer's totally contrary to my experience in all particulars. I don't even know what "the XSL logic control is poor" can possibly mean.
Robert Rossney
+2  A: 

To a first approximation, I use XSLT whenever I need to present information as HTML. Just about every time I''ve deviated from this in the last seven years I've regretted it. My brief experience with HTML generation in Python is the only thing I've encountered yet that may possibly be able to replace it.

Robert Rossney