• « XSLT and ASP.NET tutorial
    • |
    • Main
    • |
    • XSLT Quote of the Day »
            • December 03, 2004

              Michael Kay compares XSLT and XQuery

            • In a recent post to xml-dev Michael Kay writes in response to a post questioning the lack of comparisons between XSLT and XQuery:

              It seems odd to me that I’ve never seen an article comparing actual XSLT and XQuery code for say moderately complex operations.

              Apart from my Knight’s Tour, which Jonathan cited (and which is a very untypical application), I thought I’d try a very simple transformation that I had to do recently. The task is to remove those parts of a document rooted at an element having the attribute secret=”yes”. This is bog-standard XSLT and doesn’t need any 2.0 features:

              <xsl:template match="*">
              <xsl:copy>
                <xsl:copy-of select="@*"/>
                <xsl:apply-templates/>
              </xsl:copy>
              </xsl:template>
              
              <xsl:template match="*[@secret='yes']"/>
              

              7 lines, of which 6 are copy-and-paste from many other stylesheets that you’ve written before. (Add two lines if you will for the header and footer).

              Here’s an attempt at an XQuery solution:

              declare function m:copy($e as element()) as element()? {
                if ($e/@secret = 'yes')
                  then ()
                  else element {node-name($e)} { 
                         $e/@*, 
                         for $n in $e/node() return 
                             if $n instance of element()
                             then m:copy($n)
                             else $n
                  }
              }
              
              document{
                m:copy(doc('input.xml')/*)
              }
              

              14 lines of code (twice as much), and less scope for reuse. No great difference in the conceptual difficulty - both require an understanding of recursion - but I think XSLT emerges here as the winner. (And remember this is only XSLT 1.0).

              But in fact there’s a bigger issue - the XQuery code is wrong. It loses the namespaces from the source document. Perhaps I’m missing something, but I can’t see any way to solve this. XQuery may be computationally complete, but it’s not actually closed over the data model - there is no way of generating a namespace dynamically. As far as I can see at the moment, that’s a stopper, and this exercise has to be coded in XSLT.

              Of course, I’m sure there are other problems where XQuery has the edge.

              Michael Kay http://www.saxonica.com/


              I think we have a new quote of the day!!! (an area in which sadly has not had much attention since Dimitre submitted the first entry about a week ago)…

              Something like…

              “XQuery may be computationally complete, but it’s not actually closed over the data model - there is no way of generating a namespace dynamically. As far as I can see at the moment, that’s a stopper, and this exercise has to be coded in XSLT.”

              I think it’s time to update the “XSLT Quote of the Day” :)

              I’ll do that now…

            • Posted by m.david : December 3, 2004 01:54 AM GMT

            Trackback Pings

            TrackBack URL for this entry:
            http://www.xsltblog.com/xslt-blog-mt/mt-tb.cgi/88

            Comments

              • I’m sure the XQuery folk could come up with plenty of examples of tasks that, while possible in XSLT, are shorter and simpler in XQuery. (I came up with an 11-line query across recipes stored in multiple files that outputs a single sorted list of ingredients, each with links to all recipes that use it.)

                Regardless of whether XSLT or XQuery can do more than the other, plenty of tasks are clearly easier in one than the other, and both have a fine future.

                Bob

              • Posted by: Bob DuCharme at December 3, 2004 07:07 AM
              • You’re right… No doubt they could and probably should as I don’t want to come across as someone who is totally anti-XQuery. I’m not at all!!! In fact if XQuery would have come along first I would have had a hard time justifying learning XSLT simply because XQuery, at that point, would have been so much easier to learn coming from a syntax standpoint. But with that in mind I am glad it didn’t come along first as it forced me to re-learn a functional way of thinking much more than I think XQuery would have. While obviously both Functional-like languages with XQuery’s syntax I think I would have kept on in thinking more procedurally than functionally. But I really can’t say that as a surety as there would be know way for me to “go back in time” within my mind and rethink things through without considering what I have already learned since using XSLT.

                None-the-less, I am probably a little to harsh on XQuery and will try to be a little less as time goes forward. A good way to do this would be to showcase good examples of proper use of XQuery as well as news items, like the previous post regarding Business Intelligence, that showcase XQuery being used in real world applications. Given that both languages can compliment each other and act almost as a “buddy system” allowing each to do what they were designed to do best and passing streams back and forth as needed to get the final result in an efficient and “code-friendly” manner I can foresee plenty of opportunities to showcase both languages in the future. Luckily for us Dr. Kay has built-in support for both languages in Saxon so it makes this task a lot easier to implement now and showcase now.

                Thanks again for your comments Bob!

                Cheers!

              • Posted by: M. David Peterson at December 3, 2004 09:53 AM

            Post a comment




            Remember Me?

            (you may use HTML tags for style)

          • © 2005 :: <XSLT:Blog/> (xsltblog.com) is a product of M. David Peterson and FunctionalX Consulting. See Licensing Info Below.
          • Except where otherwise noted, this sites content and source code is licensed under the Attribution License from Creative Commons.