views:

493

answers:

3

After a long learning loop via XAML, I've returned to HTML and javascript, and realised that the concept of declarative code - in terms of rules for transformation - is an incredibly powerful concept.

Despite its surfeit of syntax, XSLT processing of XML is the keystone of declarative transformation programming. I have, however, always found it hard to understand how XSLT would be used for everyday tasks (with XML).

What are some good examples of XSLT elegantly solving a programming problem, outside of generating HTML?

I'm guessing it's good at graph transformation and data reprocessing...

Edit: I'm hoping for some actual examples - one of the things that puts me off full-on xslt is the visual complexity of the code.

+2  A: 

How about code generation? I use this in protobuf-net to create the .cs files from a .proto model (exported as xml) - like so. In .NET 4.0, T4 templates would be a more obvious choice, but the T4 engine isn't standalone (AFAIK) before then.

Xslt is also used extensively to transform between different xml layouts - for example, in middleware tools like biztalk.

Marc Gravell
A: 

I used XSLT the other day. I needed to provide a way to let the user save the contents of a tree control to a file so they could email it to their boss, show it to their grandchildren, etc.

It was requested that they be able to save it as an HTML file with a nice tidy look to it, or as XML so they could process it further however they wished.

So I suggested that we output XML with an XSLT linked to it, so that way they have the raw XML but it looks nice when they open it in the browser.

It's not ideal (if they move the HTML somewhere else without the XSL, IE refuses to display it at all!) but if we change plans, I can just modify the product so it will let the user choose the format, and if they choose HTML, all I have to do is run the XSLT on the XML to get the HTML before I save it.

Ideally there would be a way to make a self-contained HTML file that contained XML and XSLT, but I'm not aware of one... which is a question in itself.

Daniel Earwicker
+4  A: 
Dimitre Novatchev