views:

1407

answers:

16

For a current project the decision has to be made whether to use XML and an XSL-transformation to produce HTML or to directly use HTML-templates.

I'd be interested in arguments for or against the XSL-approach. I understand that in cases where you have to support many different layouts, an XSL-solution has a lot of advantages, but why would you choose it in those cases where you only have to support one target layout?

Edit: We're talking about Java here.

+4  A: 

Going the XSL way will future-proof your application. Meaning, if you decide in the future to add more templates with different layouts you will be able capitalize on those advantages. In my current project we save off the XML used (in an XMLType or CLOB) and allow other applications to access the data and XSL templates to generate documents via a web service. This was an after thought of the original design that was super easy to implement due to our decision to use XML/XSL.

northpole
+1  A: 

In contrast to HTML, there are a lot of XML tools available if you need to do parsing and processing of the templates in any way. So you should choose XML to get the benefits of using tools and libraries for XML.

However, that said, it may just be that XHTML fits your needs, since this gives you full support of XML tools and libraries while still being normal HTML which is correctly processed by modern web browsers. If you need to do post-processing of those later on, you can still apply XSLT to the XHTML data.

Lucero
+4  A: 

Please dont use XML/XSLT for web frontends. I were in projects like this and its horrible. Often you have to first produce the XML from objects or something like that, which doesnt make sense. A second point is, that there are so much good html editors out there for free, but i've found none for XSLT. So editing complex XSLT is no fun. I would recommand to go with html templates and a common template engine.

Mork0075
actually, I find vim really good at editing XML. Also: Visual Studio 2008 is great for XML/XSLT (including debugging)!
Daren Thomas
When working on SingStar, we found <oXygen/>(http://www.oxygenxml.com/) to be a great XML and XSLT editor. It shows three panels with the input xml on the left, the XSLT transform in the middle and the output on the right. Click a line on the left or right panel and all the corresponding lines are highlighted in the other panels (see the XSLT that generated this line + the source XML).
Tom Leys
+4  A: 

XSLT has the advantage of being able to also produce output in other document types (i.e. pdf) and pdf output is very likely nowadays. XML/XSLT does also separate data from the view.

Scoregraphic
+1 Absolutely. FOP for PDF is a good example.
MatthieuF
In all honesty, although you can produce non-xml, it's seldom done -- support is just not good. With perhaps except of PDF.So I would not consider it to be a significant factor here aside from PDF.I also don't think separate of data/view matters a whole lot, if source data itself is not naturally xml; if so, there is already potential separation (input data -> template for presentation)
StaxMan
+1  A: 

I see how the XSL approach cna be handy if your data is already XML.
But usually it isn't. It's somewhere in a database, needs to be generated on the spot or comes from some service.
Creating XML from this source to then be able to create HTML from that XML is useless in my opinion. I would stick with (X)Html templates.

borisCallens
On a completely personal level, I feel XSL is the child of the whole XML-for-everything idea. Face it, XML is not a good data format for ALL solutions.
borisCallens
I absolutely agree -- if source data is in xml, xslt makes more sense. If it isn't, it probably makes little sense.
StaxMan
+1  A: 

Depending on your application, having an XML layer that is then transformed to XHTML via XSLT also meens, that you can write easy WebServices to the XML layer - allowing your customers to consume your sites data...

Having the XML sent to the browser with a transformation link (forgot the exact syntax...) also meens less bandwith needed, as the XSLT file will stay the same and you only need to pass the raw XML it is built from - sort of like using an external CSS style sheet instead of adding the style attributes to your markup ;)

Daren Thomas
+14  A: 

XSLT is a functional programming language and you can use it to create frontends as rich as any templating system. However, you shouldn't — you and your team will go insane.

Both options present the opportunity of transforming objects into a presentation form in a logical sort of way. XSLT is best suited for creating more XML, which might lead you to believe that it's a perfect candidate to use to create XHTML. However, creating XHTML shouldn't be the primary goal — Creating a user experience is. Don't concern yourself with the medium.

Two significant drawbacks to XSLT concern the syntax: Your templates, and the templates that they include, and the templates that those templates include will all be gigantic and verbose. Second, you'll have to do a lot of functional programming, and less-experienced engineers may be confused and terrified when they encounter a recursive template with an accumulating function parameter instead of a simple for loop.

If you're attracted by the beauty of transforming logically-constructed, valid XML entities, consider instead a type-safe templating system that transforms beans instead. Check out Google XML Pages, and create logically-organized, type-safe templates that will be easy for future engineers to pick up and extend.

a paid nerd
+2  A: 

When we have done XSLT in the past, it was to allow the ability to extend our product. The output remained the same, only the presentation layer needed to change. This allowed us a lot of flexibility when we had clients that wanted to "customize" their UI, since all we needed to do was replace the XSLT file. If you foresee needing to make a lot of those kinds of changes, XSLT might be your answer.

However, as stated above, the XSLT syntax and functional programming mentality can make it difficult to effectively produce templates. We found that we liked to stick to the tricks that we learned and when we had client requests that fell outside of what we already knew, no one wanted to volunteer for the ticket. Usually someone eventually figured out how to do the task and our "bag of tricks" got larger, but it was often very cumbersome to figure out new things.

If you don't foresee change the UI ever, or at least not much, XSLT may not be worth the extra effort.

dsrekab
A: 

We use XSLT to generate html in our content management system and it works just fine.

Some hints: Don't try to generate all the page at once from one big hairy XML, you'll go insane. Use the HTML template (plain text/html file with styles, decorations and basic markup) with embedded markers (like, <!--MENU-->, <!--CONTENT-->), and replace markers with xslt-transformation of appropriate data.

Having said that, I doubt you really need xslt if you only going to have one layout, forever.

alamar
+1  A: 

I think you need to examine what the source of your data will be. As mentioned by boris callens earlier, if the you are pulling from a database you will have to transform first to XML, then apply your transformations. Should the data source be RSS or the like, then XSLT is a natural choice.

XPATH and XSLT has a high learning curve and functional programming can be daunting to get your arms around. In time crunch this may not be the right choice.

For front end work JSON has a lighter payload, and is readily supported by jQuery and other Javascript libraries. You may want to consider JSON as the data protocol as the jQuery library is far more accessible to developers and the time to productivity with the framework is far less than with XSLT, embedded Javascript in tags, awful syntax and all the other minutia that come with XML/XPATH/XSLT on the front end.

David Robbins
+1  A: 

I've used XML & XSLT in a previous project, financial web sites, and it worked well for us, but:

  1. We had multiple customers, which varied the number of outputs we had. We could replace the XSLT stylesheet and this made changes to the site easier to manage for the developers
  2. We had a specialist web editor on the team. We gave them example XML & they could edit the stylesheets directly
  3. If there were ever any wording changes that needed to go onto the website yesterday ( it was a bank, this happened surprisingly often), we could just deploy the new XSLT without redeploying the entire site.
  4. Multiple different output formats were needed. We used FOP for transformation to PDF, which is based upon the same sort of technology, so wasn't too hard for us to understand :-)

The main reason I see for using XSLT is if you have multiple sites all based upon the same XML, but requiring different HTML output.

MatthieuF
Points 1 + 3 are easily satisfied by any templating system
Tom Leys
This is true. But there weren't very many of them around at the time.
MatthieuF
+1  A: 

Keep it simple. That's a principle that one gets to appreciate more and more.

Velocity or Freemarker are incredibly flexible and versatile. Your code base will be clear, easily understandable, and it will run much (much) faster than the X monstrosities.

+13  A: 

I created an XML/XSLT-driven UI for an enterprise product about 5 years ago. We're still using it, and I can now look back on my experience and see many pros and cons:

Pros:

  • XSL is a powerful declarative language, useful & fun for experienced developers, and transforms can do pretty amazing things in a few lines of code
  • XSL is designed for use with XML, so if your data is already XML then it makes a lot of sense
  • Separation of concerns (rendering vs. data) is better than many template languages
  • XSL-based rendering can be easily "subclassed". By that I mean: let's say you have data class A with associated template A.xslt. For class B derived from A, you can easily create B.xslt with only the small differences, and include A.xslt for inherited behaviors. This makes it less succeptible to breaking due to changes in A.xslt.
  • The above point also gives you the power to do overrides. For class A with associated A.xslt, we can easily switch the associated template to A-custom.xslt, which is a few small changes plus inheritance of A.xslt. We can do this on the fly in the field and again, the benefit is that A-custom.xslt is only a few lines, not an entire modified copy of the original A.xslt. The small footprint means it's more likely to work with multiple versions of A.xslt.
  • In .NET 2.0, XSLT is compiled and becomes very fast. There may be similar tech for Java. (Most template languages do this now too.)
  • In .NET, it's possible to create an "Object XPath Navigator", which lets you transform your data objects without having to convert them to an XML object. Again there may be similar tech in Java
  • XSLT is smart about HTML & handles escaping, white space issues, etc. well

Cons:

  • XSL is a powerful declarative language, confusing to newer programmers - and fewer people know XSLT well
  • XSL is verbose. XML is often verbose too.
  • XSL transforms are probably slower than "native" templates. Even when compiled there's still more state overhead to XSL than most template languages
  • It's hard to pass parameters to XSLs, you have to either send them in line with your data (forcing you to create extra XML) or via system-specific methods (which may also involve constructing XML data)
  • If you don't have an ObjectXPathNavigator or equivalent, you'll incur significant overhead when turning your data objects into XML for transformation
  • Depending on the capabilities of your transformer, you may also incur buffering overhead as you transform into a string buffer and then send that string to the output device
  • The more advanced your XSLT usage, the less likely it is that your tools will support you (specifically as you start to use includes or faster ways to pass XML data in)

I'll try to update as I think of more issues. I think that looking back now, my verdict would be to stick with a common template language. What were once big issues when I selected XML/XSLT have now been addressed by newer and more mature revisions of the major template engines. We do still benefit greatly from the ability to inherit .xslt files, which is something most template engines don't do well. But in the end the value of having lots of developers providing examples is far greater (compare ASP.NET answers vs XSLT answers on StackOverflow, for instance.)

Hope that helps!

Steve Eisner
+1  A: 

XML + XSLT are really cool. You have the ability to output many types of target formats in the future. But ne aware of embedded HTML in the XML. Firefox XSLT doesn't support "disable-output-escaping". See Bugzilla.

Rene Schulte
+11  A: 

I've done significant development using XSLT and it has been both tremendously successful and a complete failure at two different sites.

A few thoughts before a conclusion:

  • I don't think anyone would argue that XSLT is far more powerful than a template parsing engine, it's a functional language.

  • Although it's not as widely adopted as most procedural languages, it's still a real language that's being used out there for actual projects, people can be hired already with knowledge of XSLT and it's a transferable skill for your current staff.

  • XSLT has also been around for a while now, the implementations are mature, I'm sure this is the case for long running templating engines (like Velocity) but newer engines may be less robust.

  • Whatever template language you decide on it's unlikely to be as well documented as XSLT. Check out any of the Michael Kay Programmer's reference series for an example on how to do a great reference book.

  • Tool support is generally very good ... if you have a budget. XMLSpy and Stylus Studio have both been very useful for me in the past.

  • XSLT is not only hard but, more importantly, different. Most people are not Computer Science graduates formally trained in functional programming. The majority of programmers will write XSLT in a procedural style which will not harness any power of the language and give you a maintenance headache.

  • XSLT transforms can be slow and can take a lot of memory. You may have problems if you have a stylesheet with a large XML input.

I love XSLT but whether you should use it or not comes down to a few points:

Are you committed to XSLT? Do you have serious in-house expertise in XSLT? Are you prepared to get some?

Is your data in XML? Does it make sense in XML? Do you have someone in-house who loves your data enough to make sure it's well structured and there's always an appropriate schema?

Unless the answer to those questions is yes and you have complex data that requires a complex rendering process, I wouldn't consider using XSLT ... especially if there's no experience in the team. Bad XSLT is much, much, much worse than a bad template.

However, it can render complex data in a maintainable fashion which would be impossible using many of today's templating engines.

Mark Worth