Tips & Traps in Learning XSLT and "Real" Functional Programming?
Asked Answered
D

5

27

Until recently my knowledge of XSLT was enough for my simple, straightforward needs. But a more advanced requirement sent me to reference material when "just trying stuff" didn't work. It was actually fun starting over at the beginning. I initially learned XSLT in the middle of deadline pressure and in 20/20 hindsight "working" was clearly more important than "understanding."

I was surprised to read on page 33 of Michael Kay's book that XSLT has its roots in Scheme. Wikipedia says,

Scheme is one of the two main dialects of the programming language Lisp.

Maybe I know (or will soon know) more about functional programming than I thought I did. Searching SO for posts like Is XSLT a functional programming language? and following its working links show there are several opinions whether or not XSTL is a functional language. From the post Which functional programming language should I choose as first functional programming language? I got the advice to learn F# first because I come from .NET and I like to get to useful programs quickly.

My first question is, if someone knows XSLT 2.0 very well, how much of a "real" functional programming language such as F#, Lisp or Haskell does the person already know before learning F#, Lisp or Haskell? For purposes of discussion assume a person who "Knows XSLT 2.0 very well" learns very few new concepts the first time she reads the XSLT Cookbook. Instead her reaction to unfamiliar information is, "Of course that's a good way to do that. I should have thought of that!"

The second question is, are there parts of XSLT 2.0 / XPATH 2.0 I should make sure I understand very well to make learning F# / Lisp / Haskell easier?

Third, are there parts of XSLT that are best ignored if one plans to eventually learn F# / Lisp / Haskell / etc.? That is, does XSLT have concepts analogous to GOTO, ALTER and ON ERROR RESUME NEXT that are not appropriate in "real" functional programming languages?

Brief answers are fine because learning a functional programming language is an unscheduled future project for me.

P.S. Several hyperlinks removed for lack of reputation. Drat! ;-)

Dither answered 10/2, 2010 at 16:23 Comment(5)
Welcome! I must say your first question is pretty good :)Guimar
By now you should also be able to put links into your question.Lola
I was expecting very good response for this .. Not even a single answer .. !!?? I am certainly upset ..Pastorale
I think few people are awaiting because of the last words of the questioner ..Pastorale
Links added, & thanks, Jonas ;-)Dither
P
4

My advice would be not to lean too heavily on XSLT when you are learning your first second functional language. Here's what I'm thinking:

  • There are now a great many good "on-ramps" to various functional languages that don't presume previous knowledge and experience. If your XSLT experience enables you to travel faster up the on-ramp, we're all surprised and pleased, but it's not necessary.

  • Functional programmers care a lot about syntax. XSLT programmers are forced to use XML syntax. There is therefore a big cap in community norms and values between the two communities.

  • The typical functional language is not only functional, but has something special it cares about, which gives the language its own unique flavor. Some examples (in alphabetical order):

    • Erlang cares a lot about reliability and parallel/distributed computation.
    • F# cares about interoperating nicely with the .NET framework and type system.
    • Haskell cares about purity, about sexy types, and about one conception of beautiful syntax
    • Scheme cares about extensibility and about an entirely different conception of beautiful syntax.
    • XSLT cares about pattern matching and processing XML.

There's plenty of material on SO and elsewhere about picking a functional language to learn; if you like, you can see what one polyglot functional programmer thinks. Pick a language—any language—and sit back, get to work, and enjoy the flight!

Pekan answered 10/2, 2010 at 23:21 Comment(0)
Z
5

I have played around a little with XSLT, and have delved deeper into the functional languages Lisp, Scheme, and Haskell. From what little I've seen, I'm not sure that XSLT as it's commonly used will teach you that much about functional languages. You may learn a few tricks, but in my opinion, JavaScript is a better analogue to what you'll see in untyped functional languages.

Wikipedia backs me up on this one:

As a language, XSLT is influenced by functional languages, and by text-based pattern matching languages like SNOBOL and awk. Its most direct predecessor was ISO DSSSL, a language that performed the same function for full SGML that XSLT performs for XML. Many of the standards committee that developed XSLT had previously worked on DSSSL, including James Clark. XSLT can also be considered Turing-complete template processor.

In other words, it borrows a lot from functional languages, but also borrows from templating languages like awk and SGML. So yes, it's a functional language, in the same not-entirely-obvious way that C++ templates are a functional language. But there's no easily-mapped line from learning XSLT to Scheme or Haskell.

Also, don't underestimate how different functional languages can be from each other. Lisp and Scheme are both so loosely typed as to feel essentially typeless. I think of these languages as being like the Mandarin Chinese of programming languages, with an essentially fluid, context-based syntax. Haskell, on the other hand, is so strongly typed that it confuses even experienced programmers. It also reaches beyond its lambda-calculus roots to borrow concepts from abstract algebra and Category theory, making it a bit mind-boggling to learn for non-math types.

But none of this is meant to discourage you. By all means, learn a functional language. They will change your approach to programming in ways you'd never suspect.

Zeitgeist answered 10/2, 2010 at 19:26 Comment(0)
P
4

My advice would be not to lean too heavily on XSLT when you are learning your first second functional language. Here's what I'm thinking:

  • There are now a great many good "on-ramps" to various functional languages that don't presume previous knowledge and experience. If your XSLT experience enables you to travel faster up the on-ramp, we're all surprised and pleased, but it's not necessary.

  • Functional programmers care a lot about syntax. XSLT programmers are forced to use XML syntax. There is therefore a big cap in community norms and values between the two communities.

  • The typical functional language is not only functional, but has something special it cares about, which gives the language its own unique flavor. Some examples (in alphabetical order):

    • Erlang cares a lot about reliability and parallel/distributed computation.
    • F# cares about interoperating nicely with the .NET framework and type system.
    • Haskell cares about purity, about sexy types, and about one conception of beautiful syntax
    • Scheme cares about extensibility and about an entirely different conception of beautiful syntax.
    • XSLT cares about pattern matching and processing XML.

There's plenty of material on SO and elsewhere about picking a functional language to learn; if you like, you can see what one polyglot functional programmer thinks. Pick a language—any language—and sit back, get to work, and enjoy the flight!

Pekan answered 10/2, 2010 at 23:21 Comment(0)
G
2

I'm not quite as qualified as I'm sure some answerers will be. I have used Haskell, Scheme, and XSLT, though I learned them in the opposite order -- Scheme, then Haskell, and then using XSLT for basic stuff. I found that the thinking processes I learned using Scheme and later Haskell definitely helped me understand the high level concepts of XSLT programming. All the details, of course, still had to be dredged from reference guides and such.

I imagine that you'll find the same effect in reverse.

(I also found that XSLT, like other programming-languages-in-XML, is a painful way to process an XML file, and that just writing a script in Python was easier, faster, and more flexible).

Gherlein answered 10/2, 2010 at 19:25 Comment(0)
K
2

I believe XSLT is in fact a good first pure functional language: http://blogs.msdn.com/ashleyf/archive/2007/04/11/xslt-a-pure-functional-language.aspx

Kilderkin answered 11/2, 2010 at 19:15 Comment(1)
The linked article explains it well in few words. Thanks.Dither
C
2

Yes, XSLT has more of a functional programming language. For XSLT that has quite drastic consequences. For example variables are immutable.

In my experience, that makes it extremely hard to learn XSLT like a programming language. If you have no experience with functional programming you will not understand why XSLT is so. You must be very motivated or your boss must motivate you. Otherwise you will endlessly shake your head and start hating that language. For example it is impossible to work with XSLT without using recursion. If you are not familiar with recursion enough, you are unable to imagine how anybody could work like this. In Internet you can read phrases like when you learn Functional Programming you must "re-wire" your brain.

XSLT is no good language to "re-wire" your brain, because in XSLT there are too many things that distract you from this goal. There are many textbook that start to tell you the whole story of XML, for example XML namespaces, schemas, the XML syntax rules, XPath. This is already complicated. Most people learn these things automatically when they need them. If you just want to learn XSLT you don't need all this and you don't want all this. You just want to start with simple examples. But if you find simple examples these are then already complicated to understand, because as already said, without recursion, you cannot work with XSLT.

I can tell you from my personal experince, that when I came from programming languages like Python or Java I was completely shocked about XSLT. It is nothing what you expect or know.

If you are really ambitious to learn functional programming (which personally I would strongly recommend you), you should definitively not start with XSLT. The concepts of functional programming are not complicated. But you need more practice to really do practical things with functional programming. And therefore XLST does not work, because you have the whole XML eco system and the impracticable XML syntax always around you. That makes the learning experience bulky even if you manage to familiarise with all this.

XSLT is also not really appropriate as a first programming language. All you typically do with XSLT is transforming XML files. You cannot simply start with a Hello Wold example. In order to demonstrate you, what I mean, you can have a look at this example. It computes the average. In a programming language like Lisp or Haskell that is a simple exercise. But see how much stuff you need in order to have a good example with XSLT. And that example is not even complicated. It does not use recursion.

If you are not so much interested in functional programming, you can still learn and use XSLT for simple things. Indeed the very very most use cases for XSLT are simple. For example you only want to move a node from one location to another location or rename a node. You can use XSLT effectively for such things. For example if you have a tool update in your project and you want to automatically convert the file format from one tool version to a file format of a newer tool version. Such things work perfectly in XSLT. But this is just an XML tranformation. XSLT can do much more than XML transformations. If you want to use it like a real programming language, you also need to learn more than what you need for these simple XML transformations.

Since XSLT version 2.0 the languages provides a few features that in many situations help you to avoid complicated language features. You will quite often on Stack Overflow for a problem find different solutions for XSLT 1.0 and XSLT 2.0. Knowing this is also part of the challenge. XSLT 1.0 always works. But the XSLT 2.0 is often much easier (see a typical example for this). As a beginner you don't know if the one solution for a problem you find on Stack Overflow is a good solution or not.

In order to make clear, what I mean with "complicated" things, I have an example. There is a XSLT based tool that converts data from Open Street Map into SVG. It can be found here. That is definitively not a simple transformation. If you want to develop such things in XSLT, you need a good understanding of functional programming. It is no surprise that this project is not maintained since 2012. But I can confirm that this tool indeed worked well at that time.

Another point is that for debugging XSLT you need quite specific tools. That also complicates daily work.

As a final word, I would like to clarify why XSLT is this way. XML is two things. It is a data structure. And it is formatted text (e.g. (X)HTML). That has complicated implications. For example in XML there are rules that specify when line breaks are automatically removed (the normalization process). If you want to format something on your own way, this makes XML a complicated file format.

XSLT is also two things. It is XML (which again is a data structure and formatted text) and a programming language. That means, you can freely mix programming constructs and elements of your target language. That is very nice and comfortable. But for learning a programming language this is probably not what you want. You should pay the price of this clumsy XML syntax only if your application really is about XML data processing.

If you like the idea that your programming language itself is a data structure, you should consider Lisp. What for XSLT is XML is for Lisp S-Expressions. Lisp is Data Structure and programming language. XML/XSLT and Lisp are nearly the only programming languages with this duality (Homoiconicity). That is enabler for many dynamic programming solutions. But Lisp is a general purpose language that goes much beyond XML/XSLT while for XML processing XSLT is the answer.

Conchiolin answered 30/6, 2023 at 19:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.