tags:

views:

1037

answers:

12

What it is about XSLT that people find irritating? Is it the syntax (which is pretty unusual) or just the way XSLT works in general? Are there features that are lacking?

I did a little bit of XSLT (around 800 lines) a while ago and found it not that bad. So why the general animosity against it?

A: 

It's an example of turning XML into a programming language. Yuck. I wish people wouldn't do that. We have perfectly good programming languages already, and they are far better at it than XML.

RichieHindle
Yeah but doing an XML->HTML translation the client side with JavaScript is yucky too.
Byron Whitlock
But for it's application, transforming XML, I don't find it that much out of place. Even though it isn't quite natural.
Gab Royer
Bryon, then don't. Make your "ajax" CGI return HTML and jam that straight into the innerHTML property.
Joshua
+6  A: 

I personally dislike XSLT because it seems to combine several things that are generrally disliked in the developer community:

  • it uses magic strings (XPATH) that look like noise aka perl reg exs.
  • xml tags which can make statements verbose - aka xml programming language.
mP
XSLT uses regexes (not Perl ones by the way, they're far more limited) only in the same way as e.g. Python does - i.e. they're provided via standard library functions. You're definitely not forced to use them. Or do you actually mean XPath?
Pavel Minaev
My note regarding reg exs was unclear. I meant to say XPATH looks like noise, and is a paint to read a bit like complex regexs.
mP
I don't get the magic strings, why do you call XPATH magic strings?
Gab Royer
Because when they become longer than few chars they are not instantly understood. They are so contrived that you can't tell for sure what they do until you mentally debug.
mP
+5  A: 

I've worked with XSLT before and I didn't much care for it because I found it extremely verbose for the simple task I wanted to perform.

Just out of curiosity, what did your 800 lines of XSLT do?

Bill the Lizard
I was parsing shopping websites so it was quite handy for transforming the HTML into XML listing all available products.
Gab Royer
I did something similar, taking XML from Amazon and converting it to HTML. I'm pretty sure those are the two most popular use cases for XSLT. :)
Bill the Lizard
Verbosity is only a problem is you write code in notepad, if you use Visual Studio you should take advantage of code snippets, write <xsl:c , then select 'choose' and hit tab twice.
Max Toro
@maxtoroq: Verbosity is a problem when you're *reading* code more than when you're writing it.
Bill the Lizard
@Bill: I don't think more verbose is less readable, not always at least. e.g. 'define function' or 'function' are much more readable than 'def'. Anyways, XSLT 2.0 is less verbose.
Max Toro
+11  A: 

I like xslt, and use it quite a bit. As long as you think in terms of functional programming (i.e. set-once variables, similar to F# etc), then it is hugely versatile. I use it regularly for data transformation, presentation (in particular [x]html), and versatile code generation.

Definitely highly programming related; nobody except a programmer would grok it - but a very powerful tool.

I have a few xslt (split over a few xsl:import/xsl:include files) that is substantially more than the 800 you mention in the post... it really can (when used correctly) be a fully featured environment.

Notes:

  • best used at the server; client-side support is hit'n'miss
  • a few key things missed in 1.0; regex; case-insensitivity; etc
  • can be tricky if whitespace is important

One particularly useful feature of xslt (as a separate file) is that it makes it possible to change the transform without rebuilding any code. The code-gen example is from an open source project I run; I know of several users who have dipped in and tweaked the code-gen for their local standards. One use even went as far as writing the transform for an entire second language - and all without touching the binaries.

Marc Gravell
ditto. it is very good for data transformations and I've had some success using it for code generation. I do find that you need the exslt extensions for complex tasks though.
Hamish Smith
@Hamish Smith - Never heard of exslt before thanks!
Gab Royer
Regarding the data transformation, it is exactly what I used it for, I was parsing websites and XSLT proved to be quite handy at this task.
Gab Royer
+2  A: 

I personally think it is very suitable for certain types of programming problems. To me, in certain situations, it is much easier to maintain a form using XSLT versus having to rewrite/recompile/redeploy code changes. While XSLT is not the only way to accomplish that, I haven't found any other solutions for those cases that is much cleaner and easier.

It has its place. Like everything else, when misused, it becomes a garbled mess of code, just as any language would. When used correctly, it can be a good supplement or solution to a programming problem.

Cody C
+2  A: 

XSLT is very powerful, so long as what you want to do with it matches what it's good for. However, maintaining someone else's XSLT can be a bit daunting. It's a programming language but it's also an XML file, so it can be hard to understand, even when laid out cleanly and adequately commented.

Steven Sudit
+2  A: 

XSLT is a really powerful tool in the developer arsenal. I use it all the time for code generation. Performance counters, data access layer, REST interfaces, you name it. anything repetitive.

As a language it sure has its quirks, but as a tool is invaluable.

Remus Rusanu
+3  A: 

Many programmers don't have any experience with Functional Programming. XSLT, in many ways, resembles Functional Programming and a new and foreign paradigm to learn.

Learning an unfamiliar programming paradigm can be challenging, let alone learning an unfamiliar programming paradigm expressed in XML.

Code written in a Functional Programming language is typically minimalistic. XML is rarely minimalistic. So folks who know Functional Programming and appreciate its minimalism have to give up that minimalism.

Tim Stewart
The functional aspect of XSLT was one of the thing that bugged me the most at the beggining! True.
Gab Royer
+1  A: 
  1. XSLT is both functional and imperative at the same time. This trips up a lot of people. they have match and for loops with variables.
  2. It is easy to write bad code in it. But if you follow good patterns you can do some really neat things very easily.

Check out http://www.worldofwarcraft.com/index.xml and http://www.wowarmory.com/index.xml if you have an XSLT-capable browser (FF 3 is good). They are totally written in client side XSLT with underlying XML. It makes scraping those sites REALLY easy and nice and they are forced to keep the data and presentation separate. A great example is their character pages http://www.wowarmory.com/character-achievements.xml?r=Mal%27Ganis&amp;cn=Vosk&amp;gn=Juggernaut

Paul Tarjan
In what sense is it iterative?
Gab Royer
Think he means recursive?
Benjol
he means 'imperative': http://en.wikipedia.org/wiki/Imperative_programming
Val
heh. thanks ;) I did mean imperative.
Paul Tarjan
+16  A: 

I think people find it difficult to get their heads around XSLT (and bitch about it) because it is functional and declarative in nature, unlike c# or java programming. Navigating around documents can end up being complicated when XPATH statements get clever - though this is a feature of XPATH rather than XSLT. XPATH typically gets complex when you don't know at design time the exact structure of a document so you start querying siblings, descendents and ancestors. This is when people inheriting a complex XSLT start considering career changes!

With XSLT it is very much 'right tool for the right job'. It is designed to transform an xml document into another xml document extremely quickly and efficiently. XSLT is almost certainly the best tool to use for this purpose because of its extensibility, the fact that it has been written for this purpose, widespread support for it in xml processors across the board, and in case i didnt mention it already, performance. Common use-cases:

  1. converting an xml document purely containing data into a document exposing a user-interface such as an xhtml document
  2. converting an xml document into a different structure to suit someone elses schema e.g. Biz2Biz communications

A great implementation of the xslt technology is the apache-cocoon project which transforms xml documents into multiple output formats including html, excel, chart images, pdf's with an extensible plugin architecture. We use it a lot for our reporting platform and it works very well. When developers start with it, they find the same familiar issues. Once they get over them, they would typically be writing what i am here.

I once worked with a guy who didnt want to work with (and learn) XSLT and ended up presenting a demo to the client which took over 20 seconds to render a page. When i finally persuaded him to use an XSLT transform instead of his dumb DOM code it took under a second.

dbez
The irony here is that XSLT isn't really _functional_: it doesn't have functions as first-class values! (well, alright, there are hacks like [FXSL](http://fxsl.sourceforge.net/articles/FuncProg/0.html#Starting_point), but they're still hacks).The real catch is that XSLT is _pure_: no assignments or mutations whatsoever. That's what confuses so many people.
Pavel Minaev
@Pavel-Minaev: Seems you are not aware of XSLT 2.x.
Dimitre Novatchev
+1  A: 

I find that the people that complain about XSLT are the ones that misuse it. For example, I think using it as an HTML templating language for a CMS is a terrible idea, unless your data is in XML already. Those people might complain that XSLT is ugly, or verbose, or whatever, but that's because they are using it for the wrong reasons.

musicfreak
+1  A: 

Our Library CMS largely consists of html stylesheets to do almost everything. Our data is XML natively of course. Some of our programmers don't get the functional programming paradigm. Your first experiences might lead to complex templates misusing the iterative features of XSLT. The first thing you have to tell a programmer is not to use the for each statement or travel the xpath axes If they learn to refrain they may learn to understand the concepts of templates.

PetervanBoheemen