Search code examples
asp.net-mvcxsltdeclarative

XSL(like) declarative language as MVC view over strongtyped model?


As a huge XSL fan, I am very happy to use xsl as the view in our proprietary MVC framework on ASP.NET. Objects in the model are serialized under the hood using .NET's xml serializer, and we use quite atomic xsl templates to declare how each object or property should transform.

For example:

  <xsl:template match="/Article">
    <html>
      <body>
        <div class="article">
          <xsl:apply-templates />
        </div>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="Article/Title">
    <h1>
      <xsl:apply-templates />
    </h1>
  </xsl:template>

  <xsl:template match="@*|text()">
    <xsl:copy />
  </xsl:template>

This mechanism allows us to quickly override default matching templates, like having a template matching on the last item in a list, or the selected one, etc. Also, xsl extension objects in .NET allow us just the bit of extra grip that we need. Common shared templates can be split up and included.

However

Even though I can ignore the verbosity downside of xsl (because Visual Studio schema intellisense + snippets really is slick, praise to the VS-team), the downside of not having intellisense over strongtyped objects in the model is really something that's bugging me.

I've seen ASP.NET MVC + user controls in action and really starting to love it, but I wonder;

Is there a way of getting some sort of intellisense over XML that we're iterating over, or do you know of a language that offers the freedom and declarativeness of XSL but has the strongtype/intellisense benefits of say webforms/usercontrols/asp.net.mvc-view?

(I probably know the answer: "no", and I'll find myself using Phil Haack's utterly cool mvc shizzle soon...)


Solution

  • You can used the serialized (xml) form of your objects and edit it with the XML Editor of VS (I use VS2008).

    Then associate an xsd schema to this xml document. Use the schema that xsd,exe generated itself and that it uses in the serialization/deserialization.

    You will see that you get intellisense for free!

    Moreover, if you edit the schema and add

        <xs:annotation>
          <xs:documentation>
            Helpful Explanation.
          </xs:documentation>
        </xs:annotation>
    

    then the XML Editor will not only prompt you the possible elements or attributes names and values, but it will also pop-out the "Helpful Explanation" for every one of them that has the annotation data entered into the xml schema.

    To learn how to associate an xml schema to an xml document either lookup the local VS2008 help or find it on MSDN online, or read it here.

    Hope this helped.

    Cheers,

    Dimitre Novatchev