20 ms·
Why I like XSLT
- malandrew 12y agorepost: https://news.ycombinator.com/item?id=8695638 https://news.ycombinator.com/item?id=8695638 but there was no prior discussion there.
- padraic7a 12y agoAh my apologies, I should have done a better search. Thought it would be worth posting since I have seen it discussed a bit in my area of work [libraries / digital humanities]. xslt really seems to get acceptance from non-programmers who want it to perform a specific functions [like convert some hand crafted xml into html] and hatred from programmers who point out lots of problems with it as a language. I guess I would never even have thought of it as a language until I heard these complaints - I saw it more as a toolkit for, well, performing very specific functions.
- malandrew 12y agoNo problem. It's good to point out the reposts since it helps identify urls that should be investigated to figure out why the deduping algo didn't catch it. Good article though.
- sctb 12y agoA small number of reposts is OK if the story hasn't received significant attention: https://news.ycombinator.com/newsfaq.html https://news.ycombinator.com/newsfaq.html
- catshirt 12y agoused a ton of xslt. it's great for what it's good for (transforming xml). of course, if i had to do it all over again i wouldn't be using xml in the first place.
- serve_yay 12y agoWow, I never thought I'd see someone say that. I found it to be an endless nightmare when I had to use it. (We thought t would be really cool to store links and other stuff in XML and then transform that into our ASP.NET pages, so we could change the XML file without changing the website. It got complicated over time.) The point about the syntax is a good one, but I don't like the idea that people should just equivalently like any syntax that expresses the same basic ideas. I can't say why exactly but something just feels fundamentally wrong with writing code in XML. I felt the same way when I had to write MSBuild scripts, which are XML. I guess it could be trying to do imperative things in a declarative language, but that doesn't seem to be a problem for Lispers.
- dao- 12y ago> We thought t would be really cool to store links and other stuff in XML and then transform that into our ASP.NET pages Made my day.
- mannkind 12y agoI completely agree; I currently maintain an old web-app that uses xmL/XSLT and it is a complete nightmare to do anything useful.
- ams6110 12y agoI used to use it in the browser (IE with MSXML, back in the early/middle part of the last decade) for web apps. XML from the server, XSLT in the browser to generate the UI. You could re-sort, filter, drill-down, etc. all without ever hitting the server again. Just different transforms on the same data. I loved it. Never felt so productive. Rich responsive apps years before JSON and Jquery and all the subsequent JS frameworks started gaining traction. Syntax is verbose, but felt that the power was worth it. Never found a declarative, functional way to do the same kind of transforms on JSON data, but I'm sure it must exist by now.
- tchvil 12y agoNothing came close to the speed of XSLT when IE was ruling. I loved it, and enabled us to build very fast web apps. But then came JSON, Firefox and other browsers that made XSLT/XML look a less supported and ageing tech. I tried to write a js equivalent in 2008. And after a couple of iterations it finally ended up with this: http://gitHub.com/pure/pure http://gitHub.com/pure/pure It is declarative. With CSS selectors instead of XPATH. Do not mix at all with HTML. And fast rendering.
- joseacta 12y agoWhat I like about them the most of the separation. You're actually manipulating the data (XML) and displaying it to end user in HTML without going through the formalities of coding. Being using them since 1999 too. Still use them today although more work is being done with JSON/JS.
- dragonwriter 12y ago> What I like about them the most of the separation. You're actually manipulating the data (XML) and displaying it to end user in HTML without going through the formalities of coding. How is writing XSLT not coding?
- solomone 12y agoThat's it's biggest problem. If you ever had to look at a giant XSLT file and tried to debug some issue, you can understand why it never become popular.
- crdoconnor 12y agoIt is. That's the truly awful thing about it - it is a turing complete language that kind of pretends not to be. This is why templating languages are better at helping you maintain separation of concerns - they physically won't let you put business logic in the view layer because they aren't turing complete. XSLT will.
- dragonwriter 12y agoNot being Turing-complete doesn't prevent you from putting business logic in the view layer, it just means that you are likely to run into some limitations doing that (as there are some computable functions that can't be done in the templating language so that you probabaly will run into some business logic that can't be done in it.) This might further discourage putting business logic in the view layer, but, OTOH, it just as easily result in developers starting to go down the logic-in-view road, running into things that don't work there, and then ending up with some business logic in the view and some business logic in more appropriate layers. And there's really nothing inherent in view transformations vs. business logic that necessarily makes the latter sufficiently served by a weaker computational model than a Turing complete one, so not having a Turing complete language for the view can result in view transformations in other layers or constrain the flexibility of presentation if that isn't done.
- jlebrech 12y agocomponents could be achieved using xslt, you have a high level app spec, and you transform all the components.
- bokchoi 12y agoThe improved syntax is interesting. Are there any better front-ends to XSLT? A quick google turned up SLAX, but I wonder if there are others. https://github.com/Juniper/libslax https://github.com/Juniper/libslax
- bri3d 12y agoI interned at a company with an XML+XSLT based MVC framework once. I actually thought it worked really well and generally agree with the article. A lot of more "modern" template and transform solutions are aiming for the same goals: sandboxed, side-effect free, limited logic, and so on. I especially like the power of combining XSLT with Batik and FOP. You can take backend data and present it as an interactive table, a visualization, or a PDF report really easily. I will say that the syntax is awfully obtuse, though. I find that support for various powerful-but-obtuse 1990s/2000s solution stacks splits along an IDE-user line: those who use IDEs are generally comfortable working with XML and XSLT in Java while text-editor devotees tend to hate that class of solution.
- mercurial 12y agoIt's not so much obtuse as verbose. It's proof that hell is paved with good intentions: for simple transformations, it feels pretty natural. Unfortunately, once you need something a little more complex, it flies straight into a wall with "XML is not a programming language" written on it. "Function" calls with horribly verbose syntax, etc. Some solutions let you use a real programming language and have the XSL processor trigger your callbacks, but I've never experimented with it. Alternatively, something Perl's Twig let you manipulate the DOM without too much of a headache, and is preferable to XSLT in many cases.
- kevin_thibedeau 12y agoI got tired of updating my resume in Word then Manually generating PDF and massaging a plain text export into something decent. Now I write one document in ReST that is converted to Docbook (with XSLT) then run the Docbook through XSLT to get the meat of a DocX, XSLT to get an XSL-FO derived PDF, and XSLT to get an HTML derived plain text. Obtuse but powerful.
- kstrauser 12y agoI get the same results with Markdown and Pandoc. Check it out sometime.
- 12y ago
- monksy 12y agoI hate to promote my own site (http://theexceptioncatcher.com http://theexceptioncatcher.com) but I found that it made it a lot easier to separate the design of the site from the content. Most of my websites rarely ever change, so having a dynamic site is over kill. XSLT/XML helps to keep it as a static site, and I have the opportunity to change/fix the design at will. Also, I've managed to setup a system to generate my resume via an XML/XSLT transform. I haven't published how I do that (or the public version [which is out of date]). It's a tool and it can be very useful if you understand what you're doing. Additionally, there is Unix support for XSLT processing.
- brianpgordon 12y agoThere are _so many_ template engines out there. Using XSLT to generate HTML is one of the most obtuse, obsolete ways you could possibly generate static pages. https://en.wikipedia.org/wiki/Comparison_of_web_template_engines https://en.wikipedia.org/wiki/Comparison_of_web_template_eng...
- monksy 12y agoThe problem with a lot of those options is that the transformation happens on the server. I'm not really interested in doing that, with exception to googlebot. I want to keep everything static. It's hard to exploit a website that is all static.
- Animats 12y agoI've used XSLT for APIs that return XML. This allows looking at the output of the API in a human-readable format. This is convenient as a debug tool, and anything that returns an XML page should have a link to the XLST for it.
- jahewson 12y agoAlternatively, you could build your API using a human-readable format and dispense with the XSLT. For more complex debugging there's always the Accept header in HTTP which allows you to return a HTML page for browsers using your server-side template engine of choice.
- baldeagle 12y agoI bought into xslt2... which I think was only ever implimented in the saxon parser, written by they guy that wrote large chunks of the spec. My chain was simple, MS Access DB -> XML (via hand written vba) -> parser -> 4 documents with information that was consistent. I then handed it over to a programmer to replace the Access DB / XML / Parser with the server versions, and he never did get it quite right. For my next project I chose JSON, on the idea that a beautiful solution doesn't mean much if it can't be productionized. Still sad about xslt though, lots of potential for making good looking pdfs and webpages that were consistent.
- elmarschraml 12y agoXSLT is one of those cases where the idea and the intention is great, but the actual implementation is horrible. I would love to see someone propose a modern alternative to XSLT, that works for the same use cases, but avoids XSLTs mistakes.
- Someone1234 12y agoAs someone who has written a lot of XSL I completely agree. Just having a standard format which is XML-based which is able to take in XML and spit out: different XML, plain text (e.g. CSV, HTML, bespoke UTF-8 formats, etc), and so on is a wonderful idea and XSL does in fact accomplish it. But, yes, writing XSL is extremely painful. Much of it needlessly so. Although XSL 2 did fix some of my whines (DateTime library?!). I'm still legitimately annoyed (irrationally so) that indexes start at 1 and not 0 in XSL. The way template matching works is clunky. I think the first answer in this Stackoverflow question illistrates my point well: https://stackoverflow.com/questions/3127108/xsl-xsltemplate-match https://stackoverflow.com/questions/3127108/xsl-xsltemplate-...
- dharbin 12y agoSomebody can invent the coffeescript of XSLT! That way, we can keep the compatibility and ubiquity the article talks about, and get the benefit of not writing code-as-XML.
- jerf 12y ago"I would love to see someone propose a modern alternative to XSLT, that works for the same use cases, but avoids XSLTs mistakes." Use an XPath library for your favorite language, and write in your favorite language. XPath is the vast majority of the good in XSL, and there's no reason to put up with the steaming pile of the rest of it just for that bit anymore, and hasn't been for a while. If XSL hasn't been replaced, well, it's because it's been replaced by too many great languages for doing the same work rather than too few.
- gavinpc 12y agoTotally agreed. I'd love to see this in Clojure. I will stop using XSLT as soon as there is a better way to... transform XML. It is a pure functional language you can use to make web pages, yet it is really automata non grata, if you will, at least to the extent that many people want to kill XML itself. I wish Microsoft had implemented XSLT 2.0. It seems to me that Michael Kay has almost singlehandedly kept the whole thing alive, but running Saxon in .NET over IKVM (or in Javascript over GWT) is a bit too high a tax for me.
- smhinsey 12y agoXSLT definitely has its flaws and I'm not currently using it, but I am currently building an app whose UI is heavily data-driven, meaning a screen might display a dynamic list of items, each with a totally different editing/viewing/previewing experience. You can dance around the edges of this with current templating engines to the point where you have a workable solution for a specific case, but honestly nothing really compares to the generalizing power of template matching where all you'd need to do is wire up the appropriate XPath selector to a new template and there you go. This level of abstraction does not even appear to be on the radar of the templating libraries I'm familiar with but I'd love to hear about those that do support it if they exist.
- g8oz 12y agoSo much promise but a clunky abomination imo. I personally wasted a lot of time with it about 10 years ago. I've noticed that technologies that are easier to work with in a text editor eventually win out.
- whichdan 12y agoI'm very interested in working on a Javascript framework where you work directly with XML (for data and event binding) and let XSLT handle transforming it to HTML -- imagine React, except you work with XML instead, and treat it as an interface for your HTML. Email me if you'd like to chat about it!
- mslot 12y agoI guess JSX is a decent start.
- HillRat 12y agoYou can do some interesting things by embedding XSLT and XML into script tags, then wiring them up with MutationObservers -- I just hacked together a quick-and-ugly example of a Knockout-style reactive interface, and it works just fine. The big drawbacks are that your interface is still pretty noisy (though you can preprocess some of those XML headers and CDATA section tags into the page if necessary), and using XPath or DOM functions to update the XML isn't really very fun. Still, though, it'd be interesting to see what could be done with it, even if it's probably not a front end I'd like to wrestle with for daily development.
- whichdan 12y agoThe cool thing is that jQuery/Sizzle can use normal DOM selectors on an XML document, so there is a little flexibility there, and like you said, MutationObserver is pretty excellent. Getting rid of the noise is my biggest concern. Ideally, a developer should never have to know about or touch the HTML that's being generated. It's a lofty goal, for sure.
- sideproject 12y agoahhh, XSLT, haven't heard that name in awhile. For my CS honours thesis, I created XSLT debugging program - my research paper is here http://ww2.cs.mu.oz.au/~jbailey/papers/wise.ps http://ww2.cs.mu.oz.au/~jbailey/papers/wise.ps I miss it :) but now that I'm doing lots of web development & javascript, I do wonder that it is somewhat "slow" or "heavy" than other approaches. Nonetheless, it was an awesome experience diving deep into the technology.
- tomlongson 12y agoIf I never heard the acronym XSLT again it would be too soon.
- snarfy 12y ago> He can have it.
- mlvljr 12y ago"XSLT? I has it." http://www.youtube.com/watch?v=CjWYbcbpiWA http://www.youtube.com/watch?v=CjWYbcbpiWA :)
- intrasight 12y agoFirst, I don't understand what's with the complaints about the syntax. It's just a flavor of XML. If you work with XML all day, then XSLT looks just fine. But even if you don't like the syntax, so what - there aren't any alternatives to XSLT. Those of you that know and love XSLT will understand when I say I wish I could be cast into an alternative universe where XSLT continued to be mainstream. I'll continue to leverage this power-tool (and get paid handsomely by by clients for wielding such power) for the rest of my career.
- sbennettmcleish 12y agoAgree 100%. I've been using XSLT very extensively in my current role for at least two years now and I love it! However, I can say with certainty that I'm the only one within my team that shares this view. Using it for transformation of XML to HTML, semantic validation (does my XML contain A and B when C='Individual'?) and mapping from one XML flavour to something JAXB friendly. Implementing much of this stuff in Java or other would be a nightmare and/or unproductive process with a LOT more code required.
- jimfuller2014 12y agoyes, its a typical scenario ... XSLT is one of those languages where people either love it or hate it ... I think its to do with the fact that it takes a bit longer to become productive or useful in XSLT, most developers are expected to just immediately start writing code that 'does stuff' these days. Anyhow, I see most of developers pain with XSLT is because they never learned the basics of the language and thrash about ... perhaps thats the biggest fault of the language, in that its difficult to tell its story with an analogy to where people start from. Anyhow, learning any fp language tends to improve a developer as a programmer
- crdoconnor 12y ago>First, I don't understand what's with the complaints about the syntax. It's just a flavor of XML. People who hate XSLT are not usually fans of XML either. >But even if you don't like the syntax, so what - there aren't any alternatives to XSLT. There are hundreds. Simply use any normal programming language (javascript, python, perl, ruby, etc.) with a templating language (handlebars, jinja2, etc.). The results will be much cleaner, easier to debug, and will remain cleaner as your transformations get ever more complex (code has a habit of doing that).
- aboodman 12y agoThe opposing viewpoint: http://fishbowl.pastiche.org/2002/02/12/xslt_is_the_spawn_of_satan/ http://fishbowl.pastiche.org/2002/02/12/xslt_is_the_spawn_of...
- qaribou 12y agoI'd recently experimented with XSLT for a PHP site's templating engine, after having ignored it since learning it about a decade ago. I swapped out all PHP code to load a bunch of variables for building a big DOMDocument tree in-memory, then applying my XSLT to that to build the rendered HTML. Once I stopped trying to stick a bunch of <xsl:if> and <xsl:for-each>es all over the place, and took a compositional approach to templating where I simply defined the <xsl:template>s I needed and then put <xsl:apply-templates> where I wanted them to go, it actually worked extremely well. All my templates were nicely organized, and I'm far happier working in XML than HTML, with all it's wacky requirements with some tags needing to be closed and others not. It's just the little things that really made me happy -- sticking my fontawesome icons in with an <i class="fa fa-pied-piper" /> instead of <i class="fa fa-pied-piper"></i> always brought a smile to my face. It also made testing nice and fast since it's pretty easy to save the XML I want to transform as a file outside of my PHP app, and just xsltproc it from a terminal until I get it working how I want it. The big pain points were all around the library support. The XSLT lib was generally fine, but the XML processing PHP provides was rubbish. The main complaint I heard was that even PHP5.5 wouldn't support outputting as html5, and you need to manually just echo out a '<!DOCTYPE html>' to work around that. I couldn't just import HTML directly from elsewhere as rendered by my CMS into my document tree either - it needed to have string literals of tags concatenated to it just to work. The sheer number of piddly little flags that are barely documented but you need to get exactly right in order for it to work at all was a big nuisance too. I also needed to turn off all parser warnings so it wouldn't barf on reading html5 docs, and when a big advantage of the language is that you can be strict about your markup, it seems quite silly to have to turn off warnings. Looking back on it, none of my problems were really with XSLT as a language, just with how it's implemented in various languages. I feel like a lot of people got turned off when they learned about it in school, and consequently nobody was interested in doing a good job of implementing its supporting libraries in any language.
- yegor256a 12y agoTotally agree, XSLT is a great instrument and it can/should be used in a browser too, see http://www.yegor256.com/2014/06/25/xml-and-xslt-in-browser.html http://www.yegor256.com/2014/06/25/xml-and-xslt-in-browser.h...
- thangalin 12y agoThe following 45 lines of XSL code transforms a simple XML document into XHTML DIV tags that mirror the input structure: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output indent="no" method="html" doctype-system="about:legacy-compat" encoding="utf-8"/> <!-- Action parser that responds to HTTP requests. --> <xsl:param name="action" select="'do.dhtml'"/> <xsl:template match="/"> <html> <head> <meta charset='utf-8'/> <title>Title</title> <link rel='stylesheet' type='text/css' href='css/common.css'/> </head> <body> <xsl:apply-templates/> <script type='text/javascript' src='js/common.js'></script> </body> </html> </xsl:template> <!-- Make the document complete with div elements and classes. --> <xsl:template match="*"> <div class="{local-name()}"><xsl:apply-templates select="node()|@*"/></div> </xsl:template> <!-- The 'id' attribute indicates a link. --> <xsl:template match="*[@id]"> <div class="{local-name()}"><a href="{$action}?action={local-name()}&id={@id}"><xsl:apply-templates select="node()|*"/></a></div> </xsl:template> <!-- Retain the attributes (except if named "class"). --> <xsl:template match="@*"> <xsl:if test="name() != 'class'"><xsl:copy-of select="."/></xsl:if> </xsl:template> </xsl:stylesheet> CSS is then used to customize the presentation layer. For example, all the pages in the following website (click only "Next" and Safari is broken) are completely client-side XSLT, but use an XML document as a starting point: http://djarvis.bitbucket.org/xml/ http://djarvis.bitbucket.org/xml/ The pie chart on the following page was also generated using client-side XSLT: http://djarvis.bitbucket.org/xml/resources.xml http://djarvis.bitbucket.org/xml/resources.xml The only "trick" that was required was to load CSS (and some JavaScript) based on the current filename in the URL: http://djarvis.bitbucket.org/xml/js/common.js http://djarvis.bitbucket.org/xml/js/common.js Most people write one XSLT page per web page to transform, but by using structured DIVs with classes that correspond to the XML element names, only one general-purpose XSL page is required. This demonstrates the separation of content (XML) from presentation (XHTML/CSS).
- crdoconnor 12y ago>Benefit #1: Portability You know that a technology has got serious problems when the best thing you can say about it is "well, it is at least preinstalled everywhere"! >Benefit #2: Separation of concerns If you want to put business logic in your XSLT, you can. It is a turing complete language (unlike, say, jinja2). True, the language is so terrible that you will probably do everything possible to avoid putting business logic in your XSLT and that would help with your separation of concerns. I do not consider this a benefit, however. >Benefit #3: Transformations This is not a benefit of the language. This simply what it was originally intended to do. >Benefit #4: Focus on API: The focus on XML as the result of a controller naturally focuses development on the application API As opposed to other languages where you don't focus development on the application API? >Benefit #5: Sand-boxed Definitely a benefit but far from a unique one. It is also doesn't have particularly good sandbox implementations: https://www.mozilla.org/en-US/security/known-vulnerabilities/firefox/ https://www.mozilla.org/en-US/security/known-vulnerabilities... >Benefit #5: Ease of testing Compared to what?
- harryf 12y ago>>Benefit #1: Portability > You know that a technology has got serious problems when the best thing you can say about it is "well, it is at least preinstalled everywhere"! ... plus it's not even true. For any collection of XSL stylesheets, representing any kind of real world complexity, the chances you can port them as-is from A to B is highly dependent on which XSLT features were used (1.0, 2.0 and now 3.0 ), what the XSL processor supports plus "host" framework idiosyncrasies ( Java, .NET, PHP ( http://php.net/manual/en/xsltprocessor.registerphpfunctions.php http://php.net/manual/en/xsltprocessor.registerphpfunctions.... ! , etc. ).
- Nitramp 12y ago> > Benefit #3: Transformations > This is not a benefit of the language. This simply what it was originally intended to do. If a technology intends to be good at something, and then is, that's not a benefit of the technology? Huh? In any case, transformation, i.e. push vs pull templates, is indeed an interesting distinction and feature of XSLT. Using XSLT for push-style problems (like templating a book, a manual, etc.) is where it is actually useful.
- Theyeard 12y agoOne interesting use-case is having an Atom feed and HTML page at the same time. See source code here: https://curiosity-driven.org/ https://curiosity-driven.org/
- jimfuller2014 12y agoI can say with high confidence that I've probably written more XSLT then any of the commentators here ... respect completely their right to hate/love/ignore any tech, but many of the comments here are based on very little fact. * first bugbear - XSLT is not an implementation of lisp (or insert lang here), we shouldn't care if it implements said language well or not * XSLT is extremely well (saxon) implemented in some processors and poorly in others ... unsure what that has to do with the language itself. * XSLT has more implementations then a lot of other popular languages (Ruby, Perl, python come to mind), that doesn't make it better (or worst) then those languages ... its just a fact. * there are no security vulnerabilities in the design of XSLT that I am aware about (well, there are, and most certainly are in implementations, but at that level I can call out issues with every language and its processor(s)) ... stating it is insecure by design is rubbish * anyone using XSLT v1.0 should know that there has been, for years, an XSLT v2.0 production ready version in the form of saxon-ce (w/ appropriate open source license) ... I can't explain why folks insist on using XLST v1.0, some are doing so cause they can't migrate to XSLT v2.0, many of them are unaware of new options. I can understand why folks find XSLT hard, or hurts their brain or makes them want to cringe at the mention of an angle bracket ... teaching basic programming idioms is hard for most people, learning fp principles is even more of a bender (esp if you come from OO world) ... XSLT template matching is a powerful mechanism which allows one to perform transformations with a succinct set of matching rules ... but yes it is verbose and like many languages, XSLT has its ugly points to the language. If you are allergic to angle brackets, then any language that uses them is going to be anathema to them, but every person I've known who have learned XSLT became a better programmer for it. Thats my own personally biased single data point observed 'fact'.
- Tloewald 12y agoI don't think learning XSLT made me a better programmer, but it is an amazingly useful tool for dealing with large amounts of XML (which is what it was designed for). That said, the way XPATH works is -- to put it mildly -- counter-intuitive. My biggest gripe with XSLT is debugging. It's just not very much fun. Insofar as it forces you to reason very carefully about your code, that's probably good practice if you haven't already had plenty of practice -- e.g. developing websites before we had decent browser consoles, or writing INITs and TSRs in the 80s.
- deleted 12y ago[deleted]
- fubuu 12y agoFor me XSLT (I mean 2.0 and above) is awesome technology for current web environment. There are a lot of markup languages with which I can for example create graphics (SVG) and use it inside PDF (thru formatting objects (XSL-FO)). I can store complex application configurations inside Excel (OOXML) and transform it to xml and validate it (and then generate data file for db). Or I can manage language translations in excel file and merge them to individual properties files. I can also use powerful XSLT 2.0 inside browser with Saxon-CE. There are really lots of use cases for XSLT. I must mention that I use this technology with Apache ANT and this combination saved a lot of my time! In this way it is possible to do sophisticated automations. I don't know how many people use it but it is possible to have a build file and some xml configuration. In main ant target you specify xslt task (input=xml configuration, output=build file) and subsequent ant task which triggers this generated build file. This technique gives great flexibility! Of course when working with XMLs you need a good IDE!