Comparing XSLT and XQuery Michael Kay XTech 2005
2 About me Database background Started using XML in 1998 for content management applications Author of XSLT Programmer’s Reference Developer of Saxon XSLT processor Member of W3C XSL and XQuery Working Groups Founded SAXONICA March 2004
3 Comparison Points 1: Functionality –features in the language 2: Aesthetics –likes and dislikes –learning barriers –usability, actual and perceived 3: Implementations –maturity, conformance –performance –market focus
4 Functional Overlap Same data model and type system XPath as common subset Same function library Similar mechanisms for defining functions Similar mechanisms for constructing elements and attributes Both declarative languages
5 XSLT 2.0 also has... Template rules Formatting (dates, numbers) More regular expression handling Multiple output documents Plain text input and output Refinement (import precedence) Keys
6 XQuery FLWOR expressions Relational style of query Convenient for data manipulation Optimization theory well-understood Appeal to SQL users No functionality that can’t be expressed in XSLT
7 Consequences... XSLT is stronger on –rendition –up-conversion –documents XQuery is stronger on –optimization –structured data
8 Use of XML Syntax XSLT uses XML syntax, XQuery doesn’t Advantages: –stylesheets are documents, they can be edited, stored, transformed, validated, embedded, transcoded –XML syntax is extensible and robust Disadvantages: –XML is ugly and verbose –Easier to embed XQuery in Java or C#
9 XSLT Example 1 remove attributes <xsl:stylesheet version=“2.0" xmlns:xsl=" ">
10 XQuery Example 1 remove attributes xquery version 1.0; declare function local:copy($node as element()) { element {node-name($node)} for $c in child::node return typeswitch($c) case $e as element() return local:copy($a) case $t as text() return $t case $c as comment() return $c case $p as processing-instruction return $p } }; local:copy(/*)
11 XQuery Example 2 join two tables xquery version 1.0; { for $p in doc ("auction.xml")/site/people/person let $a := for $t in doc("auction.xml") /site/closed_auctions/closed_auction where = return $t return {count ($a)} } XMark Q8
12 XSLT Example 2 join two tables <xsl:variable name="a" select="/site/closed_auctions/closed_auction = XMark Q8
13 XSLT Learning Barriers XML: –encoding, entities, whitespace, namespaces Declarative programming: –variables, recursion, paths, grouping Data model –nodes not angle brackets Rule-based programming –templates
14 XQuery Learning Barriers XML: –encoding, entities, whitespace, namespaces Declarative programming: –variables, recursion, paths, grouping Data model –nodes not angle brackets Rule-based programming –templates
15 Learning Barriers? It depends where you are coming from! Many XQuery users come from SQL –less trouble with set algebra –tend to write everything as a FLWOR expression let $x := //item where $x/code = 3 return $x spot the error!
16 Performance and Optimization XMark Q8 (shown earlier) –XQuery version –XSLT version Database sizes –1Mb –4Mb –10Mb
17 XMark Q8 results (msecs) 1Mb Xalan xt MSXML Saxon 8.4 XSLT XQuery Saxon 8.4 Qizx Galax Mb Mb O(n 2 ) O(n)
18 Two can play at that game! Xalan xt MSXML Saxon 8.5 1Mb XSLT XQuery Saxon 8.5 Qizx Galax Mb Mb O(n 2 ) O(n) caveat: this is one query only!
19 Conclusions Two languages with a high degree of functional overlap Different aesthetics, but similar learning challenges XSLT has richer functionality especially for document rendering XQuery products are more likely to be optimized for large amounts of structured data