Slashdot Mirror


Going from Perl to XSL?

linderdm queries: "I am a perl web programmer who has taken a new job that requires me to do web 'programming' using XML, XSL, and Oracle XSQL. My minor experience with XSL so far has been horrible! It is so hard to do things that were very easy in Perl (looping!?!) and just seems incredibly verbose. I am curious to find out what the Slashdot community's experience has been with web programming in procedural languages (Perl, Java, etc) then switching to tag based XML/XSL. Am I wrong to be trying to do procedural programming with XSL?"

6 of 52 comments (clear)

  1. What about python? by costas · · Score: 3, Insightful

    I co-wrote a small XML->Python de-serializer. Go to gnosis.cx and look around for "XML Tools". XML Objectify does exactly what you're looking for: grabs an XML file and gives you a clean object that you can loop through, attach methods to, etc, at SAX speeds. I don't know if it's doable in Perl, but probably if you mix enough $@->$_ in there it could be done :-) (couldn't resist...)

  2. Don't do it. by Apuleius · · Score: 3, Insightful

    And here is why.
    ANd that's just one example.

  3. Think functional, not procedural by colin_zr · · Score: 4, Insightful
    Am I wrong to be trying to do procedural programming with XSL?

    Yes.

    I get the impression that you're not familiar with the functional programming style. Functional programming is a style in which the entire program is defined as a function. There are a number of languages which support functional programming. (Lisp and Haskell are probably the most famous, but XSLT is a functional language as well.) I'm not going to explain the entire theory behind functional programming at this stage. I'll just say that if you wan to be able to use XSLT as anything more than a glorified template system, you'll have to learn about functional programming.

    You ask where looping is in XSLT. Well, functional languages tend not to do looping. You have to do it all using recursion.

    If all you've done is procedural programming then it will seem very weird for a while. It takes some time to get good at it. I experienced a period of frustration when I first started with XSLT, but once you realise that it's functional, it suddenly becomes quite a powerful language.

  4. XML folks believing their own hype by pong · · Score: 3, Insightful

    XML is a useful technology - very useful, and simple too. That does not mean that it is right for every purpose, though. The XML folks, believing their own hype, simply forgot that and decided that the syntax for XSLT should be XML-based. Bad Idea! Use XML for communication between machines, not for machine - human communication! While made to be readable for people, it is verbose and there is waaay to much line noise.

    It's like that everytime something is hyped - people will use technology where it just doesn't make any sense.

  5. XSL Considered Harmful by Voivod · · Score: 3, Insightful


    Check out this excellent article entitled XSL Considered Harmful by Michael Leventhal from 1999. IMHO it's as true now as it was then.

  6. Not just Functional by bevan.arps · · Score: 3, Insightful

    A number of posters have already pointed out that XSLT isn't a procedural language, so procedural thinking won't be the best approach.

    However, XSLT isn't just functional (although functional it is). It can also be characterised as a rule based language.

    A match template can be viewed as a rule that defines what actions should be taken in particular circumstances.

    As an example, loops often don't need to be explicitly coded at all. Instead of writing some code to say "Find all elements and iterate overthem doing Xyz", you just write a template that says "Whenever you find an do Xyz" and let the stylesheet processor drive things for you.

    I'd suggest that the last thing you want to do when writing XSLT code is to think with a Perl mindset (insert procedural language of choice instead of Perl - Java, Delphi, C, C++, whatever).

    Instead, concentrate on thinking about the facilities the language does provide. Buy yourself a good book or two - Micheal Kay's "XSLT Programmers Reference" is very good. I keep my copy within arms reach whenever doing something tricky.

    Every language has its very own mind set - or paradigm, if you will. Even between languages that have substantially similar capabilities (eg Java and Delphi), the "most right" way of solving a problem can be very different, simply because of the design paradigm the language and library designers used.

    XSLT is no different - and because the language is so different to conventional procedural programming languages, you'll tie yourself into great knots if you don't put some effort into learning the "XSLT Way".

    A couple of final tips ...

    ... XPath is your friend - learn about paths and predicates.

    ... Do pay attention to modes (see xsl:template and xsl:apply-templates)

    ... do search the web and pull apart stylesheets written by others

    Hope this helps.