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?"

20 of 52 comments (clear)

  1. XML is not a programming language by Gaijin42 · · Score: 5, Informative

    It is important to remember that XML is not a programming language, it is a data format. XSL is also not a procedural language, it is a language for formatting data into a stylesheet. I refer you to the XSL page of the w3 :

    XSL is a language for expressing stylesheets. It consists of three parts: XSL Transformations (XSLT): a language for transforming XML documents, the XML Path Language (XPath), an expression language used by XSLT to access or refer to parts of an XML document. (XPath is also used by the XML Linking specification). The third part is XSL Formatting Objects: an XML vocabulary for specifying formatting semantics. An XSL stylesheet specifies the presentation of a class of XML documents by describing how an instance of the class is transformed into an XML document that uses the formatting vocabulary. For a more detailed explanation of how XSL works, see the What Is XSL page.

    So the short answer to your question is : no, XSL is not the right choice to do procedural logic. If you are doing something with your data other than displaying/formatting it, use something other than XSL. (Perl, Python, JSP, ASP, etc)

    1. Re:XML is not a programming language by tenman · · Score: 5, Interesting

      Right On!!! I'm so glad to see this right off the bat. Just because you don't know how to use a language doesn't mean it sucks. I have spent the last 2 years working on XML 'based' applications. The parser and the standards are what is so cool about it. The /. group should absolutely fall in love with this. It is open, and powerful, it's open, and open. What more could you want from a data storage standard?

      Like the parent poster said, you don't program loops with XML or XSL, you use something like XPATH in conjunction with PERL to do that. I have to agree that Oracle's XML stuff is not the coolest in the world (or anywhere else for that matter), but don't knock XML. XML is your friend, and you need to learn how to use it before you bash it.

      </soapbox> ha! "soap" </pun>

    2. Re:XML is not a programming language by dimator · · Score: 4, Funny

      It is open, and powerful, it's open, and open.

      That's all well and good, but is it open ?

      --
      python -c "x='python -c %sx=%s; print x%%(chr(34),repr(x),chr(34))%s'; print x%(chr(34),repr(x),chr(34))"
  2. 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...)

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

      Let me make the above clearer: Say you have a document, with a root tag "collection", containing several "book" tags (I am too lazy to insert angle brackets here). XML Objectify will allow you for example to create an XO_Book class that will be automatically instantiated for each "book" tag in your document. It will also instantiate an XO_Collection class that you may also overload (or not).

      For example, say that you want to have a method, file(), attached to each book. You can create that method (in Python), attach it to the XO_Book class and then run your XML file through XML Objectify. You will get back an object containing the root of the XML file and objects for each tag encountered within. If there are multiple tags, you get a magical iterator. So, my example above will look like this:

      collection = xml_objectify.xml_objectify(myxmlfile)
      for book in collection.book:
      book.file()

      Procedural and OOP power, while still talking to XML. Combine that with a good template engine (Cheetah for Python is very nice) and you will hit the ground running really fast w/o XSLT.

    2. Re:What about python? by Matts · · Score: 3, Informative

      XML::Parser's Object style does exactly this, and has done so for I think a couple of years now.

      But it doesn't really answer this guy's question ;-)

      --

      Matt. Want XML + Apache + Stylesheets? Get AxKit.
  3. Yes. by pb · · Score: 3, Informative

    http://www.dpawson.co.uk/xsl/sect2/N5019.html

    It doesn't want to be a programming language. :)

    --
    pb Reply or e-mail; don't vaguely moderate.
  4. Going from Perl to XSL... by sab39 · · Score: 5, Informative

    Going from Perl to XSL is like going from Water to Potatoes.

    They solve different problems.

    As a programmer, your toolset should include more than one tool. Perl simply cannot be beaten for text processing. XSL is probably the best out there for processing XML into other XML, but this space is immature so be on the lookout for better solutions. Java (and C#) is great for building large enterprise applications or applications that benefit from a lot of object-orientation. C is good for programming close to the metal. Python is excellent for development speed, prototyping, and general-purpose code. Fortran is still among the best out there for heavy-duty numerical programming. Prolog rules for AI. Functional languages are great for solving complex algorithmic problems. Basic is good for, well, nothing ;)

    Learn as many different languages as you can find. Then use the right tool for the job. If you find yourself worrying that a particular thing you need to do is hard in the language you're using, you're either taking the wrong approach to the problem (I believe XSLT allows some kinds of problems to be solved *without* looping even where they'd require looping in Perl) or you're using the wrong tool.

    Note: I can't say I've met all these requirements myself. My own programming experience is conspicuously missing Lisp, Scheme, Fortran and XSLT, and I only learnt one semester's worth of Prolog and never used it since; ditto ML which is the only functional language I know. And I haven't gotten as far into Python as I'd like.

    But I do know enough of each to know when I'm faced with a problem that one of them might be best for.

    1. Re:Going from Perl to XSL... by omarius · · Score: 3, Funny
      Bullshit. Basic is obviously completely optimised for writing AD&D Character Generators on the Apple ][e platform. I mean, duh, you didn't know that? ;)

      -Omar@poke

  5. Don't think procedural by Anonymous Coward · · Score: 4, Informative

    XSL is a transformation language. Don't think in loops, think in templates.
    I see horrible XSLT work where people try to use evil for-each constructions...

    And the fact that XSL is an XML application ('tag-based' as you call it) has nothing to do with it being procedural or nonprocedural, it's just a syntax issue (which can annoye me when writing more complex transformations, I think we will see more powerfull transformation languages like stratego in the future).

  6. Using Perl With XSLT by Enoch · · Score: 3, Informative
    Check out this article on perl.com describing how to use Apache/mod_perl to drive a "data driven processing pipeline" through XSLT. (It is a follow-up to this article introducing AxKit, which is a document server.

    Jeremy

  7. XSLT is Turing Complete by kgp · · Score: 3, Informative

    Shocking as it may seem XSLT is Turing complete and you can code in it (not that that is a good idea of course)

    http://www.unidex.com/turing/utm.htm

    and

    http://xml.coverpages.org/ni2001-03-19-b.html

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

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

  9. 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.

  10. Nobody said XML *was* a PL -- we're talking XSLT. by fm6 · · Score: 4, Informative
    So the short answer to your question is : no, XSL is not the right choice to do procedural logic.
    True, but kind of beside the point. Procedural programming is not the only way to write software, it's just the model that most programmers happen to know. Functional programming is probably the most important rival of procedural programming. And the functional model is rather an obvious choice for an XML transformation language like XSLT, since XML models tend to be rather recursive! Maybe serious Perl hackers are unfazed, but I find the idea of unravelling deeply-nested data-structures in Perl pretty scary.

    Functional programming requires a completely different mode of thinking than procedural programming. Anybody who understands the definition of "procedure" in ordinary English understands the procedural programming paradigm, even if they don't know the technical details. But the functional programming paradigm is a lot less commonplace, and takes some getting used to. Before tackling XSLT, it might make sense to try to pick up some fundamental FP concepts. One way to do this would be to work through the leading functional programming textbook. (It says a lot about MIT that they've gone and made the entire book available online!)

    It is kind of irritating that XSLT's creators decreed that a syntactically correct XSLT program had to be a well-formed XML document -- and thus rather verbose. But that sort of thing rather goes with the FP mindset -- FP source is supposed to be easily transformable using the same language it's written in. That's why there are Lots of Insipid Silly Parentheses -- and so few syntax rules -- in languages like Scheme.

    It's worth mentioning that Perl, though basically a procedural language, has some of the same design philosophy as the FP languages. It's easier to code a simple loop in Perl than in an FP language -- but Perl has as many ways to avoid coding a loop as any FP language!

    Also worth mentioning that XSLT and XSL are not quite the same thing. XSLT is the transformation language for XSL. It's a relatively recent invention. As originally conceived by Microsoft (yep, the evil empire was the biggest partner in drawing up the original XSL spec), transformation languages were a mix-and-match affair. A lot of early examples had XSL stylesheets with embedded Visual Basic code!

    Come to think of it, it wouldn't be hard to do a stylesheet language based on Perl, or to evolve a convention for embedding Perl instead of XSLT in XSL stylesheets. Now, why do you suppose nobody is working on that. Uh, that's a rhetorical/socratic question!

  11. 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.

  12. You must learn about functional languages by aburnsio.com · · Score: 3, Informative
    I wrote this big long post about language paradigms but my browser erased it. Thank you, IE.

    Anyhow, what you've got to get through your mind is that XSLT is a functional language; it's not procedural. We're not in Kansas anymore, Toto. Functional languages are a different beast than what you're used to, i.e. procedural languages.

    If you've taken a CS course on programming languages and they covered functional languages well than you don't have to learn anything, just realize that XSLT is functional and program accordingly.

    If you haven't taken a CS programming languages course or didn't cover functional languages then you need to learn the functional paradigm. Think transformation, not variable assignment. Think functional composition, not a row of statements. Think recursion, not looping.

    To learn it yourself, you're going to need a good book on functional programming, such as Haskell or Erlang. Both have open-source implementations available. This is not something you're going to learn in a day, it will take you a month of diligent study before you understand what you're doing. If you know rigorous logic well and are adept in discrete math and set theory you'll advance more readily.

    In addition to learning functional programming, you'll have to learn the quirks of XSLT. For one, you need to have mastery of the specs for XML, XPath, and of course XSLT. Al these specs are quite readable: read them through, try out examples. You also need to be aware of the limitation of XSLT, especially in these early days of the 1.0 version (2.0 should be more useful). Often you'll need to either do processing before/after XSLT with Perl or something else, or directly incorporate JavaScript into your XSLT document. Be sure to understand inclusions and use XSLT library documents with useful functions.

    Once you understand functional languages in general and XSLT in particular you'll be on your way to mastery of XSLT. You'll also learn alot along the way that will be useful even if you never touch XSLT again. In addition, you'll have experience in the maxim uttered by other posters that you must use the right tool for the right job. XSLT is superb for most common XML transformations (such as XML to HTML) and is far better than any other languages for doing this. But sometimes you need something else like Perl or Java for direct manipulation. Use the right tool for the right job.

  13. 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.

  14. 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.

  15. Keep it small! by hoquaim · · Score: 3, Informative

    I've written many thousands of lines of XSLT code. I've grown to really despise XSLT, and I'll offer these cautionary notes... The more logic you have to write, the more you need to think about your use of XSLT. I've found that it's very easy to write hard-to-maintain code in XSLT.

    I've also have had to resort to extensions (written in Java) in a number of cases. Not only was this a pain, but it made my XSLT non-portable between Apache's Xalan and Saxon (let alone non-Java processors).

    XSLT is great for small XML transformations (and perhaps especially good for HTML generation -- that's NOT what I was doing) -- where small is referring to the number of XSL templates, and the line count of those templates.

    Also: a number of people have mentioned the functional nature of XSLT. It also has many declarative aspects: in particular many of your templates are not explicitly called by name, they are implicitly called by rule matching in the template definition. This is one of the aspects of the language that makes building large applications in XSLT difficult (and yet for small scripts this same feature is powerful!).

    The greater your ratio between logic : emitted-fragments (ie. HTML), the more caution you need to use with XSLT (and the greater the likelihood your app is better implemented in a language good XML support, Java, C#, Perl, Python).

    Finally, I wrote a ~500 line XSLT script and the equivalent transformation in Tcl (+TclDOM). XSLT with Saxon or Xerces took about 3 or 4 seconds to run, Tcl took about 200 milliseconds! So: be aware of the performance aspects of your XSLT engine.

    Now that JDOM (an XML API for Java) has XPath support, I'll be far more likely to use Java for many of my XML transformation needs. The coolest thing about XSLT was the fact that it had XPath... now many traditional scripting and OO languages have XPath support which greatly eases XML processing in those languages (much the same way that adding regexp support to a language makes string processing much easier).