<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>onenaught.com &#187; Server Side</title>
	<atom:link href="http://www.onenaught.com/posts/category/server-side/feed" rel="self" type="application/rss+xml" />
	<link>http://www.onenaught.com</link>
	<description>A blog on web standards, accessibility, css, javascript, xslt, and more</description>
	<lastBuildDate>Thu, 07 Oct 2010 09:26:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<image>
		<title>One Naught</title>
		<url>http://www.onenaught.com/wp-content/themes/onenaught/images/onenaught.png</url>
		<link>http://www.onenaught.com</link>
		<width>116</width>
		<height>130</height>
		<description>OneNaught.com</description>
	</image>		<item>
		<title>Why Use XSLT in Server Side Web Frameworks For Output Generation?</title>
		<link>http://www.onenaught.com/posts/8/xslt-in-server-side-web-frameworks</link>
		<comments>http://www.onenaught.com/posts/8/xslt-in-server-side-web-frameworks#comments</comments>
		<pubDate>Mon, 18 Feb 2008 18:02:27 +0000</pubDate>
		<dc:creator>Anup Shah</dc:creator>
				<category><![CDATA[Server Side]]></category>
		<category><![CDATA[XSLT]]></category>
		<category><![CDATA[markup]]></category>
		<category><![CDATA[mvc]]></category>

		<guid isPermaLink="false">http://www.onenaught.com/posts/8/xslt-in-server-wide-web-frameworks</guid>
		<description><![CDATA[<img class="post-img" align="left" alt="" src="/wp-content/uploads/xsl-transformers.jpg" /> Just as web developers want to use standards on the client side, standards such as XSLT on the server side may be an efficient way to create good quality markup and other web output.

It can help avoid the hard coded or hard to edit HTML strings that are often seen in server side templates and scripts.

As an open standard, XSLT is reasonably universal, and skills can be easily transferable. 

Some people don't like it or have had bad experiences with it in the past.

But it can be a very powerful tool in the developer's toolbox. Perhaps it is worth giving it another look?

This article looks at why XSLT could be useful as part of the View in an Model-View-Controller pattern, its benefits and potential drawbacks.]]></description>
			<content:encoded><![CDATA[<h3 id="toc-summary">Summary</h3>
<p><img class="post-img" alt="" src="http://www.onenaught.com/wp-content/uploads/xsl-transformers.jpg" /> This post follows on from previous posts about the way markup is sometimes created.</p>
<p>In a previous post, I worried about some web frameworks that <a href="http://www.onenaught.com/posts/42/aspnet-is-a-leaky-abstraction">abstract away the creation of markup</a>, thus preventing (or making it more difficult for) web developers to create what they need &#8212; the leaky abstraction.</p>
<p>This post explores one way to overcome this, using XSLT.</p>
<p>As this is a rather long post, here are the key points:</p>
<ul>
<li><strong>Creating good quality markup is important</strong> for a variety of reasons (quick loading pages, cross browser compatibility, accessibility, etc).</li>
<li><strong>Some server side frameworks may either generate poor quality markup</strong> or use templating systems that are specific to that framework.</li>
<li>Using XSLT to generate the output can be one way to overcome these limitations, while giving you <strong>full control of the markup that you need to be create</strong>.</li>
<li>Markup generation via <strong>XSLT may be simpler to unit test</strong> as part of a developer’s daily work, which may not be the case (or not necessarily as simple) in other templating systems.</li>
<li>Being <strong>platform agnostic</strong>, XSLT can be an <strong>effective part of a View in an MVC framework</strong> and be applied to different frameworks and platforms, reducing upskilling costs in the long term.</li>
<li><strong>XSLT may not always be appropriate in all cases</strong>, and many are certainly put off by its oddities or perceived limitations, but perhaps it deserves another look?</li>
</ul>
<p>(Some forth-coming posts will provide some code-examples, in particular of unit-testing XSLT and how it might fit into MVC as well as how it can be written in a reusable manner.)</p>
<p><span id="more-8"></span></p>
<h3 id="toc-poor-quality-markup-from-the-server-side">Poor quality markup from the server side.</h3>
<p>A <a href="http://www.onenaught.com/posts/4/should-web-developers-avoid-wysiwyg-editors">previous post on WYSIWYG editors</a> described how the output quality from editors that hide content markup generation can result in markup that is of poor quality, hard to customize/correct/change, affect accessibility (which is a legal requirement in some countries), result in excess bloat, etc. </p>
<p>Some server-side techniques, tools, and frameworks can contribute to this poor markup. This can happen for number of reasons. For example,</p>
<ul>
<li>Popular IDEs may encourage the use of WYSIWYG editors to drop in and drag around HTML controls (or groups of them). As argued in that previous post, WYSIWYG as a paradigm for HTML may actually be inappropriate, (unless you treat WYSIWYG as WYSIWYG-when-css-is-not-enabled).</li>
<li>Popular frameworks may abstract away the creation of HTML, to do it for you. ASP.NET and GWT are such examples. But, as argued in a previous post, such <a href="http://www.onenaught.com/posts/42/aspnet-is-a-leaky-abstraction">abstractions are leaky</a>.</li>
<li>Some popular scripting environments have also encouraged spaghetti code (mixing markup, presentation, and business logic, for example).</li>
</ul>
<p>And so it seems there is a gap between web standards and server-side output generation that is web standards-friendly.</p>
<h4 id="toc-web-application-frameworks-help-immensely">Web application frameworks help immensely</h4>
<p>Various frameworks have been commonplace for a long time now. These help separate out the presentation, business/mid-tier and data access logic.</p>
<p>Most frameworks are not solely about markup generation (if that is their priority at all). Frameworks have been important to address a number of important issues from data access, scalability, performance, transaction management, object-relational mappings, code maintenance, and so on.</p>
<p>These are certainly of value and usually worth using as they have teams of people devoted to them. At their best, they empower the framework user (developers and designers, for example). Yet, on the other hand, some frameworks may take some things too far, thus disempowering framework users.</p>
<h4 id="toc-some-frameworks-still-produce-poor-quality-markup">Some frameworks still produce poor quality markup</h4>
<p>Those of you who have used ASP.NET 1.x will know what a mess its controls create in terms of markup output. (ASP.NET 2 is far better, but can still encourage poor markup or make full control trickier than it needs to be.)</p>
<p>While server side programming using the .NET framework, and C# in particular, are extremely powerful, when it comes to creating web sites, that power is used to typically generate poor quality markup (even though it may be maintainable!).</p>
<p>Some of this stems from the fact that although it tries to hide away the markup creation, <a href="http://www.onenaught.com/posts/42/aspnet-is-a-leaky-abstraction">ASP.NET is a leaky abstraction</a>. (This isn’t just a problem with ASP.NET, but potentially anything that tries to make markup creation a hidden, dark art!)</p>
<h4 id="toc-abstraction-a-good-idea-but-leaky">Abstraction: a good idea but leaky</h4>
<p>Abstraction and loose coupling are certainly important, especially between layers such as presentation, business/middle tier logic and data access. It is a common approach to writing software.</p>
<p>Understandably, developers often like the idea of abstracting away markup/output creation, too. In ASP.NET for example, you could create a simple site without almost writing any HTML. Instead, you instantiate/declare controls such as asp:Label, asp:Panel etc, and you trust .NET to create the markup for you(!).</p>
<p>Interesting attempts are even available so you don&#8217;t have to write JavaScript and you get the benefit of tool support in IDEs for .NET and Java. For example, <a href="http://projects.nikhilk.net/Projects/ScriptSharp.aspx">Script#</a> enables developers to write code in C# (or any .NET language) and then compile it into JavaScript. Google has also been doing such a thing for a long time with its <a href="http://code.google.com/webtoolkit/">Google Web Toolkit</a> (GWT) for Java. Such ideas are certainly very interesting and appealing.</p>
<p>While appealing perhaps (and maybe more to back end developers), a <em>web</em> developer also loses the ability to control how the output is created.</p>
<h3 id="toc-gaining-control-of-how-you-create-the-output">Gaining control of how you create the output</h3>
<h4 id="toc-why-does-it-matter">Why does it matter?</h4>
<p>Being able to control how you create the output ultimately gives you greatest flexibility, especially when you need to create a flexible, rich, yet lean web-based UI.</p>
<p>Using the &#8220;web standards&#8221; approach (i.e. following W3C recommendations) to achieve these things most efficiently has been well thrashed out for years.</p>
<p>And, just as developers take pride in the quality and readability of their server side code, web developers increasingly take pride in the sites and web apps they create from the code perspective, too.</p>
<p>People from anywhere can easily see your markup; less will see your server side code (though that is not an excuse to write poor server side code!).</p>
<p>Although this is by no means the only factor, when hiring we will often look at sites that people have mentioned in their CVs/resumes (or ask for examples to be sure to see what is theirs) and try to understand how they have coded bits.</p>
<h4 id="toc-some-frameworks-give-full-control-but-may-use-specific-templating-languages">Some frameworks give full control but may use specific templating languages</h4>
<p>Templating languages that accompany many popular web development frameworks may be open, but use mechanisms specific to that framework.</p>
<p>But there are clearly advantages to this approach, too (else they wouldn’t be so common).</p>
<p>For example, templates based on HTML, combined with the language of that framework (e.g. PHP, or Python, or Ruby, or C#, etc.) means there is some familiarity for developers to get on with the task at hand.</p>
<h4 id="toc-consultants-and-contractors-moving-between-frameworks">Consultants and contractors moving between frameworks</h4>
<p>If you work as a consultant or contractor for numerous clients, you may find yourself moving from project to project and then having to learn different templating languages, some with more limitations than others.</p>
<p>In some cases it may be that you end up limiting yourself to just advising on markup, or only providing XHTML/CSS/JavaScript template files, which other coders then have to reproduce.</p>
<p>I have talked to many who have been frustrated at this process, especially with .NET, it seems, as their nicely validating, clean, accessible markup becomes something completely unrecognisable, when ASP.NET gets its way! Unnecessary trade-offs then result.</p>
<p>At work, we have recently been hiring both permanent and contract web developers mostly for work with .NET platforms. We have found it hard to find people with exceptional skills at both the front end <em>and</em> .NET back-end. We tend to find either good front end skills but a different back end (PHP, Java, etc.) or good ASP.NET but little or no real front end knowledge.</p>
<p>In some instances, this is not a problem, but other times it has been preferable to have .NET and good client side skills.</p>
<p>XSLT as the templating mechanism can perhaps help achieve the best of both worlds:</p>
<h3 id="toc-xslt--an-open-standard">XSLT — an open standard</h3>
<p>XSLT could help achieve both abstraction and full control of the markup.</p>
<p>XSLT is a templating language for transforming XML documents into another format (another XML document or some other text-based format).</p>
<p>For web development this can be useful to output exactly what you want and thus gain full control of the markup output generation.</p>
<p>You can re-use XSLT, and even create a hierarchy of XSLTs, that follow a Method Template design pattern, similar to a class hierarchy in Object Oriented Programming, whereby the base XSLT is equivalent to an abstract base class, and other XSLTs that import the base one can override various templates. (A future post will demonstrate this.)</p>
<p>It does of course assume you can easily (or without much cost) get your data in XML format, so you can transform it.</p>
<h3 id="toc-xslt-in-mvc">XSLT in MVC</h3>
<p>Web developers are familiar with separation of concerns (semantic HTML markup for good document structure, CSS for style/layout, JavaScript for behaviour) and progressive enhancement (or graceful degradation).</p>
<p>Back-end/mid-tier software developers are also familiar with separation of concerns (n-tier architectures, single responsibility per class, MVC, etc.)</p>
<p>The Model-View-Controller (MVC) pattern for developing user interfaces has been around for a while. It is a decent approach to separating concerns (the model to represent the data, the view to represent the presentation, and the controller to bind them together).</p>
<p>It can also be seen at a high level (a typical n-tier architecture), as well as being used within the presentation tier (the model may interact with the middle tier, the controller will be part of the presentation tier passing the data (model) to the view code to display it).</p>
<p>It is a well-proven pattern in graphical/desktop application development and being successfully applied by many web-based frameworks today.</p>
<p>MVC can also help improve testability. For example, unit testing can sometimes be easier by testing specific views, models and controllers in separation.</p>
<p>Microsoft has recently announced an <a href="http://weblogs.asp.net/scottgu/archive/2007/10/14/asp-net-mvc-framework.aspx">MVC model for ASP.NET development</a>. I understand one of the many drivers is the unit testability.</p>
<p>From an initial read, this sounds quite good. It encourages better separation. Interestingly it also seems not to support their post-back model which most of their controls are built from. It is therefore not clear to me at the moment if this means you write out more HTML (meaning you get more control of what you output) or if you still use packaged controls (which may risk getting HTML that is not appropriate, or hard to control).</p>
<p>But Microsoft is a bit behind on MVC for web development (though that can also mean it can learn from the earlier frameworks). Other popular frameworks have existed for a while that do this for the web, such as <a href="http://www.rubyonrails.org/">Ruby on Rails</a>, <a href="http://www.djangoproject.com/">Django</a>, <a href="http://www.cakephp.org/">CakePHP</a>, and many more etc. (I am quite impressed with the Python-based Django, for example, even though I have only had a cursory exposure to it &#8212; and I usually prefer curly brace type programming languages!)</p>
<p>While these all have their own merits, they all have different ways of constructing the markup.</p>
<p>XSLT, then, could be part of the View part of an MVC framework. (It need not be the <em>only</em> way to create the view, of course.) For example,</p>
<ul>
<li>A Controller can invoke a Model to get data the way it needs to</li>
<li>The Controller can pass the Model to the View</li>
<li>The View can internally serialize that data into XML and transform it with XSLT to the output.</li>
</ul>
<p>In this way, the markup generation is still nicely abstracted away, but inside that abstraction itself, you have full control of what you need to output.</p>
<h3 id="toc-advantagesbenefits-of-an-xslt-based-approach">Advantages/Benefits of an XSLT-based approach</h3>
<p>Here are a few more detailed reasons XSLT can be useful (though, of course, some of these reasons are by no means unique to an XSLT-based approach).</p>
<h4 id="toc-xslt-is-an-open-standard">XSLT is an Open Standard</h4>
<p>People often talk about the benefits of using open standards on the web client side of things (”web standards”, for example), so why not on the server side?</p>
<p>XSLT 1.0 also has reasonably universal support. (Universal 2.0 support would be even better, though we are not there yet, unfortunately.)</p>
<h4 id="toc-better-separation-of-the-view-from-the-model-and-controller-in-mvc">Better Separation of the View from the Model and Controller in MVC</h4>
<p>A follow-up blog post will demonstrate this in PHP and, time-allowing, Mono/ASP.NET.</p>
<h4 id="toc-unit-testable">Unit Testable</h4>
<p>Many framework templates have been tricky, if not impossible, to fully unit test in isolation from other layers and dependent systems.</p>
<p>ASP.NET&#8217;s new MVC model mentioned earlier has unit testing and test driven development as a driver for this approach, but from what I understand, while it makes controllers and models unit testable, the views are still not (please correct me if I am wrong in the comments below!).</p>
<p>From what I have seen of the other View templates in other frameworks, I believe the same constraint applies (but please let me know if I have missed something).</p>
<p>XSLT, however, requiring mostly an XML input (and possibly arguments passed in programmatically), can therefore make it easier to unit test the view, in isolation of a running web server, database, etc. It can therefore be easier to integrate into a developer’s coding process.</p>
<p>XSLT-based unit tests don&#8217;t necessarily need a whole web server set up to execute. Such unit tests, which are typically numerous by their nature, would be quick to run for the developer during development. This is important in the adoption of unit testing so it doesn’t make running them feel like watching paint dry after each modification!</p>
<p>Another follow-up blog post will attempt to demonstrate this.</p>
<h4 id="toc-gain-full-control-of-the-markup-generation">Gain Full Control of the Markup Generation</h4>
<p>As explained above, markup is important. XSLT gives you the ability to output the markup as you want, with little or no trade-off. (Some other frameworks also allow this, so this isn’t a unique advantage, by any means.)</p>
<h4 id="toc-platform-and-framework-agnostic">Platform and Framework Agnostic</h4>
<p>While the suggestion of using XSLT may introduce a problem of yet another technology, it may also enable skills-standardisation across platforms. This would be useful for say web developers that need to work on multiple projects, for different clients, using different technologies.</p>
<p>This of course makes the assumption that XSLT would be widely adopted in this way! If it were, migrating to desirable new frameworks where the View at least is somewhat XSLT-driven may not be as time-consuming (though of course time is still required on the middle-tier and database layers).</p>
<p>I have, for example, migrated an XSLT-driven site based on “classic” ASP, to PHP-driven XSLT, using the same pattern, but where the XSLT hardly changed at all, saving a fair chunk of time.</p>
<p>For a major client of ours at work, their extremely high-traffic .NET 1.1 web site (now moving to .NET 2.0) was architected to by-pass the .NET controls, to avoid all the problems described above, and we transformed XML with XSLT.</p>
<p>Doing this gave us a number of benefits all in one go:</p>
<ol>
<li>We got the lean &#8212; and accessible &#8212; output needed</li>
<li>The &#8220;object-oriented&#8221; XSLT-based approach also allowed us to create reusable template hierarchies</li>
<li>The customer also wanted multiple sub-sites which was achieved with very little effort or duplication of code, while allowing each sub-site to be customised as needed &#8212; not just CSS or database-driven data differences, but template level changes such as markup and layout changes. (Had we attempted this with ASP.NET, we would have had to create Server Controls with lots of configuration and then in each sub-site we would have to wire them all up, which would lead to a lot of repetition.)</li>
<li>In one case, it wasn&#8217;t just separate sub-sites they wanted, but entirely separate web sites/applications (a public facing one, a kiosk-mode one, and a version of the site that could be used by customer support). All this ran off pretty much the same XSLTs with minor adjustments in inherited XSLTs for each web site.</li>
<li>The migration to .NET 2 involved no XSLT change (only C# changes to take advantage of things like generics, the improved XSLT processor, etc.)</li>
</ol>
<p>And the sites comfortably take thousands of orders an hour on very few web servers (though of course performance is a bigger architectural issue; the point here is that XSLT is not a performance hindrance that some believe &#8212; see below for more on this).</p>
<h4 id="toc-portable-skill">Portable skill</h4>
<p>Being both platform and framework agnostic makes this a portable skill. Of course, XSLT is not limited to just web-based scenarios, either, so it may be quite a good skill to have, generally.</p>
<h4 id="toc-repeatable">Repeatable</h4>
<p>Just as OO techniques and design patterns help with repeatability in different contexts, or just as adopting web standards/CSS-based layout helps to create a repeatable process that can be applied across any web site you wish to develop, using an XSLT-approach can apply to many different projects and in different frameworks.</p>
<h4 id="toc-development-productivity-help-inject-key-skills-into-project-delivery">Development Productivity; Help Inject Key Skills into Project Delivery</h4>
<p>I have consulted at a few companies, and read/heard others describe similar scenarios, whereby skilled client side developers are limited to providing templates they throw over to the developers who are not often <em>web</em> developers.</p>
<p>This can result in either extra time explaining the required markup to the developers. Time may also be needed in working out alternatives (and unnecessary trade-offs) because of limitations in the framework of choice, or garbled markup coming out of the development process (ASP.NET, looking at you!).</p>
<p>This past year at work, we have been aggressively hiring for web development positions, looking for a mixture of strong web standards, <em>and</em> server side programming with .NET. We have found it incredibly hard to hire candidates who are exceptional in all these areas (many good <em>web</em> developers seem to avoid .NET for example, and many ASP.NET experts we find know little on web standards).</p>
<p>But we don’t always need everyone to have <em>all</em> those skills, either; the separation afforded by XSLT has allowed us to hire really strong people with parts of these and get them to work in parallel with others. Those well versed in web standards seem to find XSLT easier to adopt, in my experience, especially if full-blown, back-end programming is not their usual area.</p>
<p>Such developers are therefore not excluded from the development process.</p>
<p>Getting the key skills into the project in this way gives a number of benefits, including</p>
<ul>
<li><strong>Greater productivity and adaptability</strong>: as project requirements change even during development, having more skilled, specialist staff than generalists in all these areas can pay off. (Generalists can often be extremely valuable too, not knocking that at all!)</li>
<li><strong>Parallel development</strong>. We have found it incredibly powerful when development phases are tight and mid-tier and client side development have to start somewhat in parallel. Some of our front-end guys who have not got skills in C# and ASP.NET are nonetheless able to fully contribute to the development process, and the project gains from their in-depth knowledge of the client side.</li>
</ul>
<h4 id="toc-encourage-front-and-back-end-developers-to-work-more-closely">Encourage Front and Back-end Developers to Work More Closely</h4>
<p>During <a href="http://www.onenaught.com/posts/36/day1-media-ajax-november-2007">day 1 of @media AJAX</a>, interface designer Mike Stenhouse gave a talk from the perspective of a designer/front end developer having to know more about development, getting more involved in agile development, test-driven, even behaviour-driven development.</p>
<p>Designers understanding more software development processes, getting involved in it – not just throwing templates over the proverbial fence – would help communicate the importance of the “lowly” client side technologies.</p>
<p>At the same time, mid-tier developers get a chance to explain concerns such as architecture, OO, patterns, frameworks etc. This common understanding, even though they need not have to be experts on the other area, would allow them to be more productive.</p>
<p>Of course, Mike Stenhouse’s point isn’t unique to an XSLT-based approach (in fact, I think he was talking about Java or PHP). But one possible advantage an XSLT-based approach may have here is that it might be simpler to learn (controversial statement, maybe!) than some of the framework languages, such as Java, C#, PHP, etc. Where this is the case, XSLT can be a useful way to help bridge the gap.</p>
<h4 id="toc-coding-productivity">Coding productivity</h4>
<p>Using something like ASP.NET controls and simply dropping them onto a canvas seems really productive at first. I was certainly excited back in 2000/2001 as information about ASP.NET started to come out. I remember being really excited about the date control that ASP.NET had demoed years ago! Such a time saver – or so it seemed.</p>
<p>However, if you factor in the time you often need to sort out the mucky markup output, make things accessible, CSS-friendlier, and so on, the overall time, in my experience doesn’t seem that much better (in some cases worse, some cases better) than an XSLT-driven approach.</p>
<p>If you know the output you need to create, I find writing XSLT to be more liberating as you are in full control of your output destiny!</p>
<p>The switch I described earlier from ASP based XSLT-driven site to an PHP based XSLT-driven was also a switch from Windows/IIS to Linux/Apache (I was finally fed up of IIS’s limited configurability, in a hosted environment).</p>
<p>In just two weeks, a site with about 700 pages was converted, fully tested, and up and running again on a new hosting company. There was virtually no XSLT change, and the design pattern (a kind of Page Controller pattern which I might change slightly to MVC if time allows) was just reapplied to PHP. PHP 5’s object oriented capabilities meant further reduction in server side code. (Come to think of it, it wasn’t two weeks – it was a LOT less, as it was just spare time during some of the evenings.)</p>
<p>For work, most of our customers (one of them being amongst the largest in the world in their industry) use .NET. We often by-pass ASP.NET controls and use XSLT to generate the output instead. C#/.NET are still used on the server side, as .NET itself is quite powerful.</p>
<p>For the generation of the UI, XSLT has allowed us to even create multiple web sites simultaneously, as all the XSLTs can be reused. Where creating a separate sub-site in the past has take 6 months or more, our move to the XSLT-based approach has meant it has taken a fraction of that time (and included SEO, accessibility, web standards, etc.).</p>
<p>As another recent example: For my current project, a colleague (an excellent developer in general, though not well-versed in web-standards, yet) is creating a functionally rich and powerful page as part of the overall app we are developing. It has a complex grid of tabular data, with a folder/tree type browser on the side with lots of dynamic stuff possible on that page. Some controls created by a vendor for ASP.NET 2.0 are being used. While they are rich in functionality, the page produces just under 1MB of output!!!</p>
<p>The output from these controls are plagued with excess divs and unnecessary table-based layout (tables for tabular data of course is fine). Styling some of the specific features also becomes tricky (because the markup created is not very good and can&#8217;t be changed).</p>
<p>In addition, on a gut feel, the amount of C# used to prepare the data to bind it to the grid-based controls seems excessive. In the output, there is a lot of unnecessary (and bloated) JavaScript needed to get it to behave as needed, too.</p>
<p>This also (currently) runs like a dog on IE 6 because of all that JavaScript and HTML bloat! I have spent a total of 15 <em>days</em> just trying to get the resizer/splitter control that contains the table and tree to resize properly by battling its JavaScript (to be fair, during that time in the summer, I discovered the excellent <a href="http://jquery.com/">jQuery</a>, which helped reduce that effort considerably but I was still using jQuery to do unnecessary stuff)!</p>
<p>In my own opinion, it would have been much more efficient to have written the whole thing with XSLT and use high quality client-side components from people like jQuery, YUI, Dojo, etc. The trees and tabular data on that page are really well suited for XSLT processing. To be fair, the guy writing the page isn’t as well versed in XSLT/web development but an experienced GUI developer, so the control approach makes perfect sense for him and he has certainly made me appreciate that approach more than I used to. (I do still feel that the amount of code needed feels excessive and the experience so far has strengthened my view that while ASP.NET is powerful on the server side, I prefer to use XSLT to create the markup, where possible.)</p>
<h3 id="toc-drawbacks-or-perceived-drawbacks-to-the-xslt-approach">Drawbacks (or perceived drawbacks) to the XSLT approach</h3>
<p>XSLT is certainly not to everyone’s liking. The right tools may not be there in the favored IDE, or it may just look too weird for some people (it is more like functional programming than procedural or object oriented, so can sometimes take a while to get used to). Here are the disadvantages of XSLT I can think of, and where I can, some ways to overcome those problems.</p>
<h4 id="toc-not-everyone-likes-xslt">Not everyone likes XSLT!</h4>
<p>Many who are used to OO or procedural programming may find this functional programming language strange to get around. Angle bracket coding isn’t to everyone’s taste! And these personal preferences can be important for a team/project. Initially I didn’t find it too intuitive, but after a few days with it, it grew on me.</p>
<p>(And I certainly don&#8217;t hold the position that one must only use XSLT. Different people will prefer different technologies, and are of course free to use them.)</p>
<h4 id="toc-yet-another-technology-to-learn">Yet another technology to learn</h4>
<p>I kind of appreciate this concern, but also in some ways do not. For example, XQuery, or the more generic and powerful LINQ from Microsoft, get many developers excited, and they are willing to learn those if needed. People will invest time in learning CSS, JavaScript, etc.</p>
<p>So, perhaps it could be argued that just like any other technology, <em>if</em> XSLT is determined to be appropriate for the project at hand, then that can be learned too.</p>
<h4 id="toc-xslt-1-0-is-verbose-and-weakly-typed">XSLT 1.0 is Verbose and Weakly Typed</h4>
<p>Some simple things in XSLT 1.0 are a bit verbose to code (though <a href="http://www.onenaught.com/posts/23/xslt-tips-for-cleaner-code-and-better-performance">XSLT 1.0 can often be written more cleanly</a> than what many people write).</p>
<p>XSLT (and XPath) are indeed weak on typing — at least 1.0, though given the nature and purpose of XSLT this is not too limiting a factor as it would be if this were a full-blown language. (Some popular languages for the web are also weakly typed, too, e.g. PHP and JavaScript and the “limited” typing can also result in many capabilities, depending how they are used.)</p>
<p><a href="http://www.xml.com/pub/a/2007/07/12/xquery-and-data-abstraction.html">XSLT 2.0 and XPath 2.0 are more powerful</a> and overcome many of these limitations, adding many more powerful capabilities, allowing for leaner code and improved performance.</p>
<p>However, while XSLT 2.0 is an open standard, many have not supported it. (Microsoft, for example, has a decent implementation of XSLT 1.0 in .NET — and MSXML — but has hinted that <a href="http://blogs.msdn.com/xmlteam/archive/2007/11/16/chris-lovett-interview.aspx">XSLT 2.0 is not a priority for .NET at the moment</a> — see also <a href="http://www.oreillynet.com/xml/blog/2007/11/question_is_microsoft_working_1.html">this discussion</a> for more information.)</p>
<h4 id="toc-xslt-is-a-limited-language">XSLT is a limited language</h4>
<p>In a way, if compared to more general purpose languages such as Java or C#, XSLT (especially 1.0) is very limited in what you do.</p>
<p>That can be off-putting straight away.</p>
<p>However, (using extension objects etc aside), the simplicity of the language can also be considered a strength; it may help architect a better separation of data, presentation and logic. I personally have not felt too much pain from its limitation.</p>
<h4 id="toc-poor-tool-support-compared-to-other-languages">Poor Tool Support Compared to Other Languages?</h4>
<p>Tool support for XSLT does not seem as good as ones for say C# or Java.</p>
<p>With C#, both Visual Studio in-built refactoring or JetBrain’s excellent Resharper makes refactoring a lot easier. Same with Eclipse and other such IDEs for Java and various other languages.</p>
<p>Need to rename a class that is used throughout your super-massive project? No problem, just right-click the definition and rename; the IDE will correctly update everything for you. Want to implement missing implementations of an interface? Right-click and get it to fill out the missing implementations. Want to move a block of code into a separate method? Highlight it, right click and select an option to extract a method. Want to find all the places where a particular method is used? Right click it and select find all instances. And so on.</p>
<p>In addition, many IDEs and plugins for code assistance goes beyond &#8220;intellisense&#8221;, advising on good coding practices, code formatting, etc. This is immensely powerful, especially when establishing team standards.</p>
<p>With XSLT, however, the level of sophistication is not as high, as far as I am aware. For example, finding instances of where a given XSLT template is used seems limited. The best editor I have come across for this purpose is oXygen (let me know if there are better ones!). It is has various refactoring and finding capabilities, but finds don’t support searches for XSLT template modes. (I find using moded templates is generally a cleaner way to code many XSLT.)</p>
<p>A developer is therefore reduced to global finds within their text editor of choice. To be honest, I don’t particularly find this a problem, but I know that some developers, especially when used to using tools like JetBrain’s excellent refactoring tools, will find this backward, and I can’t blame them!</p>
<p>Many IDE vendors, such as Microsoft, don’t have their priorities on XSLT. While somewhat understandable, it does contribute to the chicken-egg situation:</p>
<ul>
<li>IDE tools are not as good for XSLT;</li>
<li>XSLT therefore not as widely adopted in that sphere of development;</li>
<li>IDE tools remain limited for XSLT;</li>
<li>XSLT development seen as backward from an IDE/tool support perspective so usually not looked at.</li>
</ul>
<p>As to what IDEs/tools/editors are useful:</p>
<ul>
<li>A number of people at work like to use <a href="http://www.altova.com/">XML Spy</a> for XSLT development, though I personally don’t like it’s text editor.</li>
<li>I tend to use <a href="http://www.jedit.org/">jEdit</a>, which is a Java-based IDE, so I have used it on Linux, PC and Mac, quite nicely (it is hideous to look at initially, and I have spent a long time customising it! I will explain that in some future blog post!).</li>
<li><a href="http://www.eclipse.org/">Eclipse</a> and the <a href="http://www.aptana.com/">Aptana</a> plugin looks really useful, and I am beginning to look at that more.</li>
<li>Many people also swear by <a href="http://www.oxygenxml.com/">oXygen</a>, so I am looking at that a bit more, too.</li>
<li>But, for my current project, for the past year, at work I have been content with using Visual Studio 2005 (and now 2008) for XSLT. It has an XSLT debugger (and recently a profiler) but I have not needed to use the XSLT debugger yet, so can&#8217;t comment on it.</li>
</ul>
<p>If you know of any other useful XSLT tools, please comment so!</p>
<h4 id="toc-refactoring-in-a-mixed-language-environment-can-be-problematic">Refactoring in a mixed language environment can be problematic</h4>
<p>Suppose you have an XSLT that transforms some XML which comes from a Java or C# serialized object.</p>
<p>If you modify that object in a simple way (rename the class or a public member), in IDEs such as Visual Studio and Eclipse, you can very quickly update all usages of your class/method throughout your project and unit tests, and you are done.</p>
<p>But, imagine you are renaming class members that get serialized in such a way that the name of that class member is the name of the element/attribute in the XML representation. IDEs aren’t going to easily know that and you are likely to have to manually update the XSLTs.</p>
<p>Unit Tested XSLTs may help to some extent to find where changes are needed, but it may still be more work than just working in C# or Java.</p>
<h4 id="toc-getting-data-as-xml-may-not-be-practical">Getting data as XML may not be practical</h4>
<p>Using XSLT assumes you can easily (or without much cost) get your data in XML format, so you can transform it. This may not be possible in many cases.</p>
<p>In many instances, with in-built XML serialization capabilities, such costs are often minimized or minimal. (You are often doing well if this is your biggest problem in your overall system architecture!)</p>
<p>Even if serialization is not possible, XSLT performance can benefit from well structured XML. Is it easy to create that structure from your object model? Depends on the project at hand.</p>
<h4 id="toc-perceived-performance-concerns">Perceived Performance Concerns</h4>
<p>A common question I often get in discussions of XSLT is concerns of performance.</p>
<p>XSLT performance, amongst other things, is also affected by:</p>
<ul>
<li>The XML being transformed – its structure, size, etc.</li>
<li>The efficiency of the XPath expressions (e.g. using ‘//’ or invoking dynamic scripting languages from within XSLT can be performance killers).</li>
<li>XSLT itself (e.g. loading, interpreting and executing XSLT in general, whether or not the XSLT Processor is compiled/cached/reused or not, etc.)</li>
</ul>
<p>As well as addressing the XML and XPath concerns, the XSLT performance concern can also be addressed in many cases.</p>
<p>Using cachable XSLT Processor implementations can dramatically boost performance. (Kind of like XSLT being compiled into the native code, like Java bytecode or .NET IL, etc. and then caching them in memory across sessions, for subsequent reuse). We use this approach on one of UK’s most profitable and biggest online retail web sites, for example.</p>
<p>Also, as a high level language, XSLT has potential to be optimized further at the implementation level resulting in better performance without changes needed to the XSLT itself.</p>
<p>(Extensibility through XSLT extension objects may offer some performance gains if you write extension objects in a compiled language such as Java or C#. A risk may be that you lose some portability — though how much that is a risk depends on your circumstances.) </p>
<h4 id="toc-fragment-caching-is-hard">Fragment Caching is Hard</h4>
<p>In addition to the performance boost from using cachable XSLT Processors, the ultimate caching strategy is being able to cache the entire output of a page. Subsequent requests for the same page require very few server resources.</p>
<p>However, there are times where the entire output is not easy or practical to cache, but fragments of the output could be. An example is a page of personalized data combined with general, cachable data.</p>
<p>XSLT in a View is typically used to generate the whole output, so fragment caching could be hard. Ways to mitigate might include implementing caching strategies in Models (which might be worth doing anyway), pipelining XSLTs, where the output of one transformation is fed into the input for another XSLT allowing cached fragments to be saved temporarily in the process.</p>
<p>In this case, specific templating languages such as that from Django, ASP.NET etc may offer better abilities. XSLT 2.0 may also have some more features in this area (it has better support for multiple XML documents, for example). If XSLT were to ever become even more popular for this usage, then I imagine there would be more advances in this area, too.</p>
<h4 id="toc-it-implies-error-prone-xml-based-templates">It implies error-prone XML-based templates</h4>
<p>Consider this concern from Django&#8217;s design philosophies behind its templating system:</p>
<blockquote cite="http://www.djangoproject.com/documentation/design_philosophies/"><p>
<strong>XML should not be used for template languages</strong></p>
<p>Using an XML engine to parse templates introduces a whole new world of human error in editing templates — and incurs an unacceptable level of overhead in template processing.</p>
<p class="source">&#8211; <cite><a href="http://www.djangoproject.com/documentation/design_philosophies/">Design philosophies</a>, Django, last accessed January 4, 2008</cite></p>
</blockquote>
<p>While the above is not specifically geared towards XSLT, human error is certainly an issue with XML-based templates, or XSLT-authoring.</p>
<p>That error, if it comes at template creation time, is typically caught during the development/debug/test cycles, as malformed XML will error early.</p>
<p>Of course, if content comes from external sources (user generated, from third parties, etc) the well-formedness of that XML cannot be guaranteed. However, these are usually not likely to be within the template itself. It may or may not be possible to cleanse such data, depending on the project.</p>
<p>In addition, as addressed earlier, I do not believe the overhead of template processing is that bad. Cached XSLT processors, as well as other caching strategies can help, for example.</p>
<p>A colleague of mine found an ASP.NET data grid control taking some 2 minutes to bind and render a standard .NET dataset. He changed that into an XSLT transformation instead and the data transformed in around 20 seconds &#8212; clearly an exceptional case compared to most web needs (2 minutes <em>or</em> 20 seconds implies a LOT of data), but XSLT is not automatically slow.</p>
<p>In addition, depending on the application, the generation of the XML may come entirely through serialization of objects, which in languages such as Java and .NET (not sure about python, which Django is in) is reasonably fast.</p>
<p>In a current project I serialize a very large object (about 200K when saved as XML) and transform it into HTML. The HTML is a very complex form of over 300 form fields! (Fear not, we use a tab-like interface and various other techniques to group them!) Using the cached XSLT Processors on my developer box, the transformation takes just 5-10 milliseconds. The far bigger cost is getting the data from the database in the first place (about 50 to 120 milliseconds, but that is on a developer database on the same box as the rest of the stack). The serialization takes about 5 milliseconds. Even when the XSLT processor is not cached, the transformation times are very small (I think about 150-300 milliseconds depending on the data).</p>
<p>That same design philosophy&#8217;s very next point was that you should</p>
<blockquote cite="http://www.djangoproject.com/documentation/design_philosophies/"><p>
<strong>Assume designer competence</strong></p>
<p>The template system shouldn’t be designed so that templates necessarily are displayed nicely in WYSIWYG editors such as Dreamweaver. That is too severe of a limitation and wouldn’t allow the syntax to be as nice as it is. Django expects template authors are comfortable editing HTML directly.</p>
<p class="source">&#8211; <cite><a href="http://www.djangoproject.com/documentation/design_philosophies/">Design philosophies</a>, Django, last accessed January 4, 2008</cite></p>
</blockquote>
<p>I would tend to agree with the bit about assuming designer competence, which is actually why I find XML for templates are not as bad as it may initially seem.</p>
<p>As I mentioned earlier, in my experience, when trying to hire front end developers, we have found they are quite capable at adopting XSLT.</p>
<p>It is not that Django&#8217;s is wrong in my opinion; I am sure there are many for whom XML-based templates are not appropriate. My point here is that there are also many for whom XSLT will be seen as appropriate, too.</p>
<p>(And this is not a criticism of Django, either. I have only looked at it very, very, very briefly and am already impressed with it!)</p>
<h4 id="toc-coding-productivity-is-perceived-to-be-poor-hand-writing-all-that-code-seems-inefficient">Coding Productivity is Perceived to be Poor; Hand writing all that code seems inefficient</h4>
<p>A common concern I hear (usually from middle-tier developers who want to avoid writing HTML) is that it is inefficient to hand write all that code. I can certainly understand their concerns; it is something I shared when I used to do more middle-tier development than front-end.</p>
<p>However, I actually find the opposite can be true: After using it for many years, a few colleagues of mine and I find it is far more productive creating web sites this way, because we know what we need to output, and we don’t spend ages battling tools that generate HTML to make them do stuff they don’t easily want to. We also help minimize, or completely avoid the excessive bloat that many tools create.</p>
<p>In addition, past experiences suggest that many of our XSLT projects seem to have less code than would be needed in say a C#/ASP.NET template (like the tree/grid example mentioned earlier).</p>
<h4 id="toc-unlikely-to-get-vendor-buy-in-if-they-have-vested-interest-in-other-ways">Unlikely to get vendor buy-in if they have vested interest in other ways</h4>
<p>Vendor buy-in may be limited if there is an inherent interest in vendor lock-in.</p>
<p>This is a mixed one too. Take .NET for example. Microsoft is unlikely to do away with its control-based approach and cause more upheaval to its target developer audience by doing this. It also reduces the dependency on Microsoft!</p>
<p>I would think that <em>if</em> this XSLT idea has any large-scale merit, open source platforms/frameworks are more likely to adopt it, though that is not necessarily a given, either.</p>
<h4 id="toc-xslt-for-web-based-views-hasnt-taken-off-yet-so-it-must-be-a-bad-choice">XSLT for web-based Views hasn’t taken off yet, so it must be a bad choice</h4>
<p>I think this point can have merit. However, I think there may be a number of different reasons why XSLT has not taken off.</p>
<p>For example, it may not have been appropriate in the past because XSLT tools were not as mature. While the XSLT tools were maturing, other technologies came through.</p>
<p>In addition, not all data is always easy to represent as, or convert into, XML, and the tools built into languages these days such as serialization may not have been around sufficiently in the past.</p>
<p>But the chicken-egg scenario mentioned above about IDEs not supporting XSLT as much so XSLT not getting as much traction in this area, could also be a factor. And, as hinted above, some vendors may have interests in promoting their own ways of doing things as an open standard may make it easier for someone to move to another platform, if needed.</p>
<p>Of course, developers not liking the language may also be a factor. (It seems to be quite big in the Java world, from what I can tell, though I don’t have much hands-on experience with Java these days – if anyone has some more insights here, please let me know!)</p>
<p>We have used this XSLT approach very successfully on a number of projects, one of which is one of the most prominent online retail web sites in the UK, if not the world. We haven’t really had a chance to write about it. Maybe there are some (many?) others who have also not had a chance to write about similar approaches? The BBC, I understand from some colleagues/friends that have worked there in the past, use it but in a more off-line scenario to pre-generate certain pages/sub-sites. So it can’t be all bad!</p>
<p>In addition, valid (or invalid) reasons to reject it in the past do not always automatically apply in for now or the future, especially in a dynamically changing environment such as the Internet. Times and technology change, so maybe it is at least worth re-validating (not adopting blindly of course!).</p>
<h3 id="toc-in-conclusion">In conclusion</h3>
<p>Despite the concerns, or perceived concerns, I feel the potential advantages make it worth considering.</p>
<p>There are certainly XSLT-based frameworks out there, though not as popular. (<a href="http://cocoon.apache.org/">Apache&#8217;s Cocoon project</a> comes to mind, for example.)</p>
<p>I would think that just as advocates of web standards have pushed hard to get open standards adopted for client side web development, maybe there is a case for XSLT on the server side to aide in that process.</p>
<p>But it also depends on personalities, priorities, and specific requirements, too. Some people really don&#8217;t like XML-based things, which is fair enough as sometimes there are legitimate reasons for it.</p>
<p>Others may not be convinced by the need for controlling markup, thinking it is unimportant or that their favourite vendor will sort it all out for them!</p>
<p>I am not arguing that XSLT should be the only way of doing things. I think it can be useful in many situations, and I have certainly found it to be so. Following the ideas of using web standards on the client side, using XSLT on the server side (at least for the generation of output) appeals to me too. What I therefore hope to do in coming posts is demonstrate some possibilities.</p>
<h3 id="toc-some-examples">Some examples</h3>
<p>To explore this further, as this post is already quite long, in the next few days or weeks (whenever I get the time!) I will add posts to demonstrate</p>
<ul>
<li>Unit testable XSLT </li>
<li>“Object-oriented” XSLT </li>
<li>XSLT as the View part of MVC, or as part of a Page Controller pattern.</li>
</ul>
<p>[You might, rightly, wonder why this is a WordPress blog, and not something I have written using say PHP with XSLT! Fair, but lack of spare time is the main problem: It has taken me two years just to get round to starting this blog! One day I will convert, time allowing!]</p>
<p>In the meanwhile, if you have got this far, thanks for bearing with me!</p>
<p>Do you have similar concerns, or agree/disagree?</p>
<h3 id="toc-image-credits">Image credits</h3>
<p>&#8220;XSL Transformers&#8221; reposted with kind permission from <a href="http://www.flickr.com/photos/nagradim/">NaGraDim</a> from flickr. See the original image at <a href="http://www.flickr.com/photos/nagradim/750087828/">http://www.flickr.com/photos/nagradim/750087828/</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.onenaught.com/posts/8/xslt-in-server-side-web-frameworks/feed</wfw:commentRss>
		<slash:comments>32</slash:comments>
		</item>
		<item>
		<title>PHP 5.3 Gets A Bit More Object Oriented And More</title>
		<link>http://www.onenaught.com/posts/54/php-53-gets-a-bit-more-object-oriented-and-more</link>
		<comments>http://www.onenaught.com/posts/54/php-53-gets-a-bit-more-object-oriented-and-more#comments</comments>
		<pubDate>Sat, 16 Feb 2008 14:54:16 +0000</pubDate>
		<dc:creator>Anup Shah</dc:creator>
				<category><![CDATA[OOP]]></category>
		<category><![CDATA[Server Side]]></category>
		<category><![CDATA[object oriented programming]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.onenaught.com/posts/54/php-53-gets-a-bit-more-object-oriented-and-more</guid>
		<description><![CDATA[<img src='http://www.onenaught.com/wp-content/uploads/php.png' alt="" class="post-img" align="left" /> PHP 5 in general has been a good improvement over PHP 4, but those used to full blown object oriented program languages such as Java or C# may find some OO features still lacking in PHP 5.

PHP 5 has the usual things, such as classes, interfaces, abstract classes, inheritance, etc, but some useful programming constructs have been missing, though PHP 6, under development, aims to rectify that.

However, it seems that many of those features are going to be brought forward to the up-coming PHP 5.3 (which may make it more likely that it will get installed by web hosting companies sooner than they would likely go for PHP 6).

<a href="http://www.sitepoint.com/article/whats-new-php-5-3/2" title="What's new in PHP 5.3?, Sitepoint, February 13, 2008">Sitepoint has an excellent summary of the features</a>. The list of features include:]]></description>
			<content:encoded><![CDATA[<p><img src='http://www.onenaught.com/wp-content/uploads/php.png' alt="" class="post-img" /> PHP 5 in general has been a good improvement over PHP 4, but those used to full blown object oriented program languages such as Java or C# may find some OO features still lacking in PHP 5.</p>
<p>PHP 5 has the usual things, such as classes, interfaces, abstract classes, inheritance, etc, but some useful programming constructs have been missing, though PHP 6, under development, aims to rectify that.</p>
<p>However, it seems that many of those features are going to be brought forward to the up-coming PHP 5.3 (which may make it more likely that it will get installed by web hosting companies sooner than they would likely go for PHP 6).</p>
<p><a href="http://www.sitepoint.com/article/whats-new-php-5-3/2" title="What's new in PHP 5.3?, Sitepoint, February 13, 2008">Sitepoint has an excellent summary of the features</a>. The list of features include:</p>
<p><span id="more-54"></span></p>
<ul>
<li>Namespaces</li>
<li>The <code>use</code> keyword to use namespaces!</li>
<li>Namespace aliases</li>
<li>Class constants</li>
<li>Namespaced functions</li>
<li>The Global namespace</li>
<li>Autoloading namespaced classes</li>
<li>Late static binding</li>
<li>Variable static calls</li>
<li>MySQL native driver</li>
<li>Additional OpenSSL functions</li>
<li>Improved command line parameter support</li>
<li>XSLT profiling</li>
<li>New Error Levels</li>
<li>and more&#8230;</li>
</ul>
<p>But visit the <a href="http://www.sitepoint.com/article/whats-new-php-5-3/2" title="What's new in PHP 5.3?, Sitepoint, February 13, 2008">sitepoint article</a> for more details.</p>
<p>If you want, there are <a href="http://snaps.php.net/">PHP snapshot builds of PHP 5.3 (and 6.0)</a>.</p>
<p>(Those of you that have never coded in PHP may be surprised to see some of these features only surface now, yet, PHP has still managed to drive some of the biggest web sites out there&#8230;! I have only used it for about a year myself, in spare time, but it seems quite powerful even without these things. Can&#8217;t knock it <em>too</em> much then, I guess <img src='http://www.onenaught.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  )</p>
<p>The native MySQL driver will be a good boost for performance, and I will be looking forward to the XSLT profiling in code, while namespaces will be an obvious winner too.</p>
<p>But a lot of PHP programmers are often using it for small scripts and may not often need or want the full power of an OO language. Personally, I prefer OO, so am looking forward to this. Do you use PHP? If so, do you prefer the OO approach and like what is coming?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.onenaught.com/posts/54/php-53-gets-a-bit-more-object-oriented-and-more/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>ASP.NET Is a Leaky Abstraction</title>
		<link>http://www.onenaught.com/posts/42/aspnet-is-a-leaky-abstraction</link>
		<comments>http://www.onenaught.com/posts/42/aspnet-is-a-leaky-abstraction#comments</comments>
		<pubDate>Wed, 02 Jan 2008 18:41:44 +0000</pubDate>
		<dc:creator>Anup Shah</dc:creator>
				<category><![CDATA[General Web Development]]></category>
		<category><![CDATA[Server Side]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[markup]]></category>
		<category><![CDATA[XSLT]]></category>

		<guid isPermaLink="false">http://www.onenaught.com/posts/42/aspnet-is-a-leaky-abstraction</guid>
		<description><![CDATA[<img class="post-img" align="left" src="/wp-content/uploads/mona-unmasked.jpg" alt="A manipulation of the classic Mona Lisa shows her holding a mask revealing a smiling, ugly face." /> ASP.NET is a leaky abstraction because it tries to hide away some of the details of HTML markup generation for you when sometimes you need to know about the underlying markup.

In doing so, it is too easy to create ASP.NET sites that violate web accessibility guidelines and contain unnecessary markup bloat. In some cases, ASP.NET makes it really difficult to create the exact output you need. But there are a some options to address this problem.]]></description>
			<content:encoded><![CDATA[<p><img class="post-img" src="http://www.onenaught.com/wp-content/uploads/mona-unmasked.jpg" alt="A manipulation of the classic Mona Lisa shows her holding a mask revealing a smiling, ugly face." /> At work, we have been using .NET and C# since around 2002. Writing from a mid-tier development perspective, I have to admit I have liked it. However, writing as a web developer using <em>ASP</em>.NET (Microsoft&#8217;s attempt to abstract away the creation of HTML so that it can target non-web developers/mid-tier/VB-like developers to use their IDEs and platform) is another story!</p>
<p>ASP.NET has been problematic for many web developers/designers trying to do the &#8220;right thing&#8221;, such as</p>
<ul>
<li>Use web standards</li>
<li>Output semantically meaningful markup</li>
<li>Develop to standards, rather than browsers for cross-browser development</li>
<li>Use progressive enhancement concepts to cater for as many user preferences and technology limitations as possible</li>
<li>Create sites that are as accessible as possible without unnecessary trade-offs with the visual design</li>
<li>Create sites that are friendly for SEO</li>
<li>Create lean and optimal markup for quicker download</li>
<li>etc.</li>
</ul>
<p>(ASP.NET 1 was far worse, and version 2 allows many of the above to happen, but not all, and not always easily.)</p>
<p>Describing the problems caused by the poor quality and bloated markup and the weird/wonderful/complex/ugly/hacky ways to work around this seems to take ages. Some fans of ASP.NET don&#8217;t like to hear it when going into these details, and react defensively. And, to be fair, these problems are not unique to ASP.NET, either.</p>
<p>But recently, I rediscovered a summary explanation to help explain <em>why</em> ASP.NET is often problematic from a web developer&#8217;s perspective:</p>
<p><strong>ASP.NET is a <a href="http://www.joelonsoftware.com/articles/LeakyAbstractions.html">leaky abstraction</a></strong>:</p>
<p>That term, &#8220;leaky abstraction&#8221;, was coined some time ago (end of 2002) by Joel Spolsky, a prominent writer on software development and he was noting the problems with ASP.NET way back then.</p>
<blockquote cite="http://www.joelonsoftware.com/articles/LeakyAbstractions.html"><p>
In teaching someone about ASP.NET programming, it would be nice if I could just teach them that they can double-click on things and then write code that runs on the server when the user clicks on those things. Indeed ASP.NET abstracts away the difference between writing the HTML code to handle clicking on a hyperlink (&lt;a&gt;) and the code to handle clicking on a button. Problem: the ASP.NET designers needed to hide the fact that in HTML, there&#8217;s no way to submit a form from a hyperlink. They do this by generating a few lines of JavaScript and attaching an onclick handler to the hyperlink. The abstraction leaks, though. If the end-user has JavaScript disabled, the ASP.NET application doesn&#8217;t work correctly, and if the programmer doesn&#8217;t understand what ASP.NET was abstracting away, they simply won&#8217;t have any clue what is wrong.</p>
<p class="source">&#8211; <cite>Joel Spolsky, <a href="http://www.joelonsoftware.com/articles/LeakyAbstractions.html">The Law of Leaky Abstractions</a>, November 11, 2002</cite></p>
</blockquote>
<p>I think this is a decent summary description that can be useful for constructive discussion on what to do about it (if anything).</p>
<p><span id="more-42"></span></p>
<h3 id="toc-abstraction-without-understanding-problems">Abstraction without understanding = problems</h3>
<p>Of course, this is not just an ASP.NET issue, but a general problem in software development, as Spolsky added:</p>
<blockquote cite="http://www.joelonsoftware.com/articles/LeakyAbstractions.html"><p>
The law of leaky abstractions means that whenever somebody comes up with a wizzy new code-generation tool that is supposed to make us all ever-so-efficient, you hear a lot of people saying &#8220;learn how to do it manually first, then use the wizzy tool to save time.&#8221; Code generation tools which pretend to abstract out something, like all abstractions, leak, and the only way to deal with the leaks competently is to learn about how the abstractions work and what they are abstracting. So the abstractions save us time working, but they don&#8217;t save us time learning.</p>
<p>And all this means that paradoxically, even as we have higher and higher level programming tools with better and better abstractions, becoming a proficient programmer is getting harder and harder.</p>
<p class="source">&#8211; <cite>Joel Spolsky, <a href="http://www.joelonsoftware.com/articles/LeakyAbstractions.html">The Law of Leaky Abstractions</a>, November 11, 2002</cite></p>
</blockquote>
<p>This helps explain:</p>
<dl>
<dt>Why non-web developers or IE-only web developers like ASP.NET</dt>
<dd>It is an abstraction, so you don&#8217;t worry about it</dd>
<dt>Why web developers/designers despise ASP.NET</dt>
<dd>The abstraction leaks, horribly at times, and you <em>want</em> to worry about what is being abstracted</dd>
</dl>
<p>What is still interesting is that as a <em>software</em> developer (not necessarily a web specialist), Joel has understood some of the problems <em>web</em> developers and designers will have with .NET even though many Microsoft converts and ASP.NET fans do not!</p>
<p>Many software developers look down on web developers, thinking things like HTML and CSS are beneath them (its &#8220;just&#8221; the UI, or that JavaScript is not a real programming language, etc).</p>
<p>While those attitudes are generally changing, many software/mid-tier/non-web developers I come across still hold some of these views and don&#8217;t want to write anything in these languages, preferring the safety and familiarity of their IDE! (To be fair, some of these concerns are quite valid, too, and the principle for supporting the abstraction is actually often good; it is just that with web technology at the level it is at now, some of that abstract necessarily has to leak&#8230;)</p>
<h3 id="toc-negative-perception-of-ie-and-asp-net-for-web-developers">Negative perception of IE and ASP.NET for Web Developers</h3>
<p>It is interesting to note at various conferences like @media, @media AJAX, and on various blogs by noted web developers and designers, Microsoft gets a lot of stick for its IE web browser (as it is recognized as the poorest in quality of all the modern browsers, with the most bugs, biggest divergence from web standards and causing the most headaches for modern web development).</p>
<p>Indeed, since winning the &#8220;browser war&#8221; IE has stagnated and because of its dominance, web technologies have hardly changed in the last 10 years (we are still on HTML 4.01/XHTML 1.0, CSS 2.1 etc). As I posted earlier, <a href="http://www.onenaught.com/posts/44/microsofts-internet-explorer-slows-down-web-development">IE has slowed down web development progress</a> in recent years.</p>
<p>Combine that with the poor quality markup typically generated by ASP.NET, there has been a lot of criticism from the web development community aimed at Microsoft for either not caring about standards (or, as with ASP.NET 2 and <a href="http://www.onenaught.com/posts/46/microsofts-internet-explorer-8-to-speed-up-web-development">IE 8</a>) caring about it as an after thought or when threatened by rivals.</p>
<p>Here is quite a telling quote summarizing some of the implications:</p>
<blockquote cite="http://blogs.msdn.com/ie/archive/2007/11/30/the-first-year-of-ie7.aspx"><p>
I sympathize with your situation, but you desperately need to get your seniors and peers to understand what&#8217;s happening, and to assign your team way more resources.  IE is a first-impression MSFT product for most web developers, and yet you all continue to underestimate the dramatic spillover effect this poor developer experience has had and will continue to have on your other products and services.</p>
<p>Let me drive this point home. I am a front-end programmer and a co-founder of a start-up. I can tell you categorically that my team:</p>
<p>- Won&#8217;t download and play with Silverlight<br />
- Won&#8217;t build a Live widget<br />
- Won&#8217;t consider any MSFT search or ad products in the future</p>
<p>And the reason is because of IE &#8211; because MSFT disregards its most important relationship with us.  Until this relationship is repaired, nothing else stands a chance.</p>
<p>Please fight the good fight and drive this point home in your org.  Good luck.</p>
<p class="source">&#8211; <cite>Anonymous commenter to <a href="http://blogs.msdn.com/ie/archive/2007/11/30/the-first-year-of-ie7.aspx">The First Year of IE7</a>, November 30, 2007</cite></p>
</blockquote>
<p>It may be seem a bit harsh, as some developers and technical managers at Microsoft certainly seem to &#8220;get it&#8221;, but for whatever reason it has happened and left a sour taste in the mouths of many web designers and developers.</p>
<h3 id="toc-leaky-abstractions-lead-to-productivity-loss-and-unnecessary-trade-offs">Leaky abstractions lead to productivity loss and <em>unnecessary</em> trade-offs</h3>
<p>As I blogged earlier, <a href="http://www.onenaught.com/posts/4/should-web-developers-avoid-wysiwyg-editors">the WYSIWYG paradigm that Visual Studio and other such IDEs encourage, is often inappropriate for web development</a> as it encourages bad practice such as non-semantic markup.</p>
<p>The <em>perceived</em> productivity gains of such editors vanish when needing to deal with typical web related concerns (semantic markup, accessibility, progressive enhancement, SEO, etc). In fact, what often occurs is <em>unnecessary</em> trade-offs. E.g.</p>
<ul>
<li>A back-end developer uses an ASP.NET control that offers quick development with sophisticated capabilities just by dragging it onto a canvas and setting a few properties.</li>
<li>A web developer then claims that control is poor from all the web development perspectives and lists a number of things to change.</li>
<li>The back-end developer resists (especially if it comes from a &#8220;lowly&#8221; web developer!).</li>
<li>Trade-offs are then made (or not).</li>
<li>Reworking in full (if embraced at all) takes a long time.</li>
</ul>
<p>Even though ASP.NET 2.0 is far better than 1.x, the abstractions still leak; it is too easy to create sites that either</p>
<ul>
<li>Rely on JavaScript</li>
<li>Violate, or easily encourage ignoring, basic accessibility</li>
<li>Encourage further leaky abstractions because of the ease at which various ASP.NET controls can be created (the idea is good, but the fundamental leakage means that it will carry through to various controls).</li>
</ul>
<h3 id="toc-so-what-can-people-do">So what can people do?</h3>
<p>As an overall thought: <strong>by its nature, abstracting away HTML is almost guaranteed to result in a leaky abstraction</strong>.</p>
<p>Instead, it may be better to <strong>embrace the reasons for the leak, and provide cleaner separation while empowering people to work with the markup</strong>, given it is so crucial from so many perspective.</p>
<p>So here are some examples of a few ways to deal with the challenge, all with their merits for different purposes. Your mileage with each option will vary according to need. In addition, it is not a complete list; there may be other ways to achieve what you need:</p>
<h4 id="toc-strict-discipline-with-the-controls-you-choose-to-use">Strict discipline with the controls you choose to use</h4>
<p>The controls idea is certainly nice. ASP.NET and others (e.g. Java Server Faces) have put a lot of resources into this approach, and is certainly appealing from various perspectives (packaging components, reusability, productising components, etc).</p>
<p>Despite the problems, trying to stick with the <em>principle</em> of controls based abstraction may still be worth trying to pursue.</p>
<p>If so, I think it would require being VERY careful about third party controls you use, or purchase.</p>
<p>The other problem I see with this is in the case of medium/large teams; this strict discipline is unlikely to hold for too long, and eventually a developer with less experience on web development (or no inclination to do the web bit) will insist on using some poor quality controls that offer perceived productivity gains (&#8220;they can fix the problems you highlight to them!&#8221;, or &#8220;it looks okay&#8221;, or &#8220;people with accessibility needs don&#8217;t use the web, so it doesn&#8217;t matter!&#8221;).</p>
<p>Your mileage on this option will vary, depending on your team and project, etc.</p>
<h4 id="toc-inherit-and-override-problematic-controls">Inherit and override problematic controls</h4>
<p>Don&#8217;t like the way asp:label outputs its HTML? Override its Render() method. Same with any other control.</p>
<p>Sounds nice and easy enough, but this option is likely to run into trouble when someone invariably will want to use or integrate a composite, possibly third party, control written elsewhere that cannot be overridden (easily). So ASP.NET adaptors may be a better alternative than this one.</p>
<h4 id="toc-asp-net-control-adaptors">ASP.NET Control Adaptors</h4>
<p>Also known as CSS Adaptors (which reveals a fundamental misunderstanding; developers want semantic markup for all sorts of reasons, not just creating CSS-based layouts).</p>
<p>This approach tries to give developers the opportunity to clean up the HTML output by overriding the default implementation.</p>
<p>The kind of problems I see with this approach:</p>
<ul>
<li>Code examples I have seen that look awful &#8212; lots of string concatenations, obscure configuration settings, etc.</li>
<li>It may not always be easy to deploy a quick change after initial development (need to recompile a .NET Assembly, for example).</li>
</ul>
<p>Despite the potential problems, it may still be a useful option if there aren&#8217;t any other alternatives.</p>
<p>(Does this approach help prove the abstraction fails?)</p>
<h4 id="toc-do-nothing-wait-for-microsoft-to-fix-it">Do nothing (wait for Microsoft to fix it)</h4>
<p>It may seem odd to entertain an option of &#8220;do nothing&#8221;, but I often think this is an option that should be considered in many situations, if not just for completeness but because pondering on this may let you think out of the box, so to speak.</p>
<p>Why should this option be entertained for ASP.NET? Well, you could take the approach of:</p>
<ul>
<li>It&#8217;s not my problem; I just want to create cool apps</li>
<li>Microsoft will always fix it; we can just wait</li>
<li>If we get sued (accessibility compliance) or our bosses/managers complain, we can just blame Microsoft</li>
<li>The abstraction might be leaky now, but that doesn&#8217;t mean it always will &#8212; look at the improvement of ASP.NET 2 vs ASP.NET 1.x</li>
<li>I&#8217;m <em>so</em> fed up I have lost the will to live and do anything else other than just be assimilated <img src='http://www.onenaught.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </li>
<li>Problems? What problems? (Already assimilated a long time ago <img src='http://www.onenaught.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  )</li>
</ul>
<p>And so on.</p>
<p>Needless to say, I would disagree with many of these perspectives. For example,</p>
<ul>
<li>I feel as a web developer, it is out responsibility to use the tools available to create sites that are as accessible to as wide an audience as possible.</li>
<li>Claiming ignorance, or actively avoiding issues you know are there somehow does not feel right.</li>
<li>Microsoft may certainly fix these issues over time. Reading blogs and interviews of key people in ASP.NET and Internet Explorer teams certainly gives some hope for the future, but it depends on whether you think you can wait or not (and whether you have enough information on when they will release specific fixes that are important to you).</li>
<li>The controls paradigm might always be leaky regardless of what is done. I think it would still be important to know the output that is being created in order to fully understand the implications of what you are creating. If there are problems with the site, you are likely to have to sort it out, not Microsoft (or any other vendor/framework you go for).</li>
</ul>
<p>For many web developers who know exactly what they need, having a tool do it for you, and to a lower quality, will be infuriating. Knowing it can be done better (not just for one&#8217;s own ego, but because it is good for the user/customer) can be compelling enough to want to do something about it.</p>
<p>Indeed, a principle behind a number of open source frameworks has been to try and make the web better, and keep it open because it is felt that open standards and related principles will lead to greater choice, diversity and vibrancy of the web. (Indeed, a lot of &#8220;Web 2.0&#8243; and the social web has come about because of open source technologies and ideas.)</p>
<h4 id="toc-use-an-open-source-alternative">Use an open source alternative</h4>
<p>Those loyal to Microsoft will not like to hear this, but this is a decent option in many scenarios.</p>
<p>As Simon Willison, co-creator of the popular <a href="http://www.djangoproject.com/">Django web framework</a> said a few years ago, one of the key benefits of the open source approach is <a href="http://simonwillison.net/2005/Nov/1/destiny/">you are in charge of your own destiny</a>.</p>
<p>(I&#8217;m not going to debate the open vs closed source paradigms here; that is not the purpose of this blog. I will just say that I didn&#8217;t use to buy the arguments of open source in the past, but in the last 3 or 4 years I have come to respect it a LOT more &#8212; I have not regretted moving to Linux at home recently, one bit, for example, and looking at the types of projects flourishing is quite encouraging.)</p>
<h4 id="toc-dont-use-asp-net-but-dont-throw-the-baby-net-out-with-the-bath-water-asp-net">Don&#8217;t use ASP.NET, but don&#8217;t throw the baby (.NET) out with the bath water (ASP.NET)</h4>
<p>Okay, so for some scenarios, completely changing platforms may not be an option.</p>
<p>In addition, I have to admit, programming in .NET (C# 2, etc) is nice.</p>
<p>Although Microsoft may be losing credibility with web developers because of the poor quality Internet Explorer browser and because of the problems of ASP.NET from a web designer&#8217;s perspective, .NET more generally is actually quite good.</p>
<p>Unlike on the Internet Explorer side of things, the MSDN blogs seem to be more active regarding on-going developments on .NET. <a href="http://weblogs.asp.net/scottgu/">Scott Guthrie, for example, has an immensely useful and popular blog</a> on all things about Visual Studio, ASP.NET etc. (Scott Guthrie is General Manager of development teams that build the .NET CLR and the core .NET Base Class Libraries, ASP.NET, Silverlight, IIS 7.0, Visual Studio Tools for ASP.NET, WPF and Silverlight.)</p>
<p>For some projects we have successfully used .NET but by-passed the ASP.NET controls, using XSLT instead.</p>
<h4 id="toc-xslt-an-open-standard">XSLT &#8212; an open standard</h4>
<p>Using XSLT to generate the output can be one way to overcome limitations of proprietary frameworks where you lack control of the output (ASP.NET) or where other frameworks may be open source but you have to learn their templating conventions.</p>
<p>XSLT can also make the markup generation simpler to unit test as part of a developer’s daily work, which is not always the case (or not necessarily as simple) in other templating systems, because you don&#8217;t have to be dependent on a running web server and all the set up that would come with it (thus making unit tests quick to run as well, increasing the likelihood of them being used and maintained).</p>
<p>Being platform agnostic, XSLT can be an effective part of a View in an MVC framework and be applied to different frameworks and platforms, reducing up-skilling costs in the long term.</p>
<p>If you consult or contract for a number of customers/clients, it would be nice if there was one common skill that is portable across all these frameworks (just like HTML/CSS/JavaScript typically is on the client side). XSLT could be such a thing, potentially, all the while giving you full control of the markup that you need to be create.</p>
<p>In my experience, and those of some of my colleagues at work, using XSLT to by-pass .NET controls has actually been more productive from an overall development perspective because we save time that would otherwise be needed battling the controls trying to force or work alternative markup options, building accessibility, etc. (And we have architected/developed sites for one of the largest online retailers in the world, so the strategies are not merely wishful thinking, but reasonably proven).</p>
<p>XSLT may not always be appropriate in all cases, and many are certainly put off by its oddities or perceived limitations. But, perhaps XSLT deserves another look?</p>
<p>(Indeed, a follow-up post does just that, starting by looking at <a href="http://www.onenaught.com/posts/8/xslt-in-server-side-web-frameworks">XSLT in server side web frameworks</a>.)</p>
<p>Do you find these problems with ASP.NET for your sites? How do you overcome them, or do you just stick with it?</p>
<h3 id="toc-image-credits">Image credits</h3>
<p>&#8220;Mona Unmasked&#8221; reposted with kind permission from <a href="http://www.flickr.com/photos/gx9/">gravityx9</a> from flickr. See the original image at <a href="http://www.flickr.com/photos/gx9/354387041/">http://www.flickr.com/photos/gx9/354387041/</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.onenaught.com/posts/42/aspnet-is-a-leaky-abstraction/feed</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
	</channel>
</rss>

