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

17 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)

  2. No, no, a thousand times no by BusterB · · Score: 2, Informative

    XSLT is not a procedural programming language. To quote the w3c, XSLT is a language for transforming XML documents into other XML documents. Sure, you might be able to use XSLT to generate HTML reports from the data generated by a query to a database storing XML, but you can't use XSLT alone to perform much other logic.

    Daniel Robins' articles were very helpful for me to understand how XSLT ties in with other systems.

    http://www-106.ibm.com/developerworks/usability/ li brary/us-gentoo/
    http://www-106.ibm.com/developer works/usability/li brary/us-gent/
    http://www-106.ibm.com/developerwo rks/usability/li brary/us-gentoo3/
    http://www-106.ibm.com/develope rworks/usability/li brary/us-gentoo4/

  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.

  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. Parsing XML by klui · · Score: 2, Informative
    I've had good luck with using XMLsoft's XML parser using the traditional C interface. The website provides both DOM and SAX paradigms. You'd use DOM if you want to read the entire XML file into memory into a tree structure and grab various XPATHs; use SAX if you're only interested in a small subset of the XML file. A DOM/XSLT processor is provided so all you need is to write your XSLT templates.

    SAX just provides the hooks that tells you whether you're at the beginning, end, of a tag, document, characters, and so forth but doesn't allow you to reference items you've already looked at--that's your program's job. It's more efficient and faster than DOM in this sense; however as your needs of the total XPATHs of your document approaches its entirety, the time saved using SAX will diminish towards the performance of DOM so you're better off using DOM/XSLT since the maintenance is easier.

    Performance wise, I found the XMLsoft version to be 2X, more or less, than that of a parser written in Java using Oracle's XML library depending on the size of the XML document. There are some features missing in XMLsoft's version but it is constantly being updated and feature gaps eliminated.

    Do a google search for XSLT FAQ for more info on DOM/XSLT. XSLT is quite a beast.

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

  9. Re:Don't do it. by Anonymous Coward · · Score: 2, Informative
    Just to be clear, that article goes on one assumption that once disproven would remove the teeth and conclusions leaving a description of what the language can do and what it can't.

    It goes on the assumption that you are supposed to be able to do everything from within XSLT. This is incorrect. XSLT is for transforming one XML document into another. To do anything more complex is supposed to require another language. Maybe they'll change this in future versions, but for now there's a defined scope of what XSLT should and shouldn't do.

    So XSLT outputs XML, but saying that it outputs XML is far too vague a term. XSLT can output XML styled XML - not everything that happens to be well-formed XML. By XML styled XML I mean syntax that doesn't invent it's own way of escaping characters (ie, Javascript's backslash) rather than using the XML way. This also means outputing XML that doesn't heavily rely on CDATA. This means that the input/output files must not only be well-formed XML - but the input/output must be useful when they provide XML that takes full advantage of the XML syntax.

    Knowing this it's easy to see where XSLT suits and doesn't. It doesn't suit outputting XHTML with javascript that requires a backslash before certain reserved characters. Nor does it suit outputing in LaTeX. Nor does it suit outputing CSS.

    You may well be able to bend XSLT to fit, but you'd be better to save yourself the trouble and just push the output through something more suited to the job. XSLT isn't a general purpose text mangler - that's what Perl is for.

    XSLT is however the best way of mangling XML documents into different formats. It's syntax - although verbose - is surprisingly small and efficient when you know how to craft an XPATH.

    I've been writing XSLT for a year now, and at the ripe old age of 23, I get all the chicks - believe me.

  10. zvon.org by Anonymous Coward · · Score: 2, Informative

    I find zvon.org to be a great site for understanding w3 standards (including XSLT). The tutorials are just sequences of examples; they get straight to the point.

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

  12. 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.
  13. Use extensions by havardw · · Score: 2, Informative

    I've used extensions for some of the things that I couldn't easily do in XSL.
    Takle a look at the Xalan extensions, or find something for your implementation.

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

  15. Re:Nobody said XML *was* a PL -- we're talking XSL by Anonymous Coward · · Score: 1, Informative
    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!
    more of a moot question, really.

    http://www.xml.com/pub/a/2000/07/05/xpathscript/
    http://www.suranyami.com/XSPtutorial/

  16. Re: XML folks believing their own hype by mattcoarr · · Score: 2, Informative

    Granted, defining a language or technology to be syntactically based on xml makes it very verbose, but there are other consequences of this too.

    For one, this allows for facilitated tool integration and automated manipulation and handling. This could be for a graphical or more concise textual represenation of the xslt "program" or for automated generation. And tool integration should not be underestimated.

    Think of the refactoring support that editors/IDEs are starting to provide. AFAIK, Right now this is most prevelent in java environments (have a look at IntelliJ's iDEA), having its origins in SmallTalk. This will probably be extened to other languages and technologies over the next one to three years.

    I highly recommend the book Refactoring: Improving the Design of Existing Code by Martin Fowler. Also checkout the site refactoring.com.

  17. 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).