Title: Comparing XSLT and XQuery
1Comparing XSLT and XQuery
2About me
- Database background
- Started using XML in 1998 for content management
applications - Author of XSLT Programmers Reference
- Developer of Saxon XSLT processor
- Member of W3C XSL and XQuery Working Groups
- Founded SAXONICA March 2004
3Comparison 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
4Functional 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
5XSLT 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
6XQuery FLWOR expressions
- Relational style of query
- Convenient for data manipulation
- Optimization theory well-understood
- Appeal to SQL users
- No functionality that cant be expressed in XSLT
7Consequences...
- XSLT is stronger on
- rendition
- up-conversion
- documents
- XQuery is stronger on
- optimization
- structured data
8Use of XML Syntax
- XSLT uses XML syntax, XQuery doesnt
- 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
9XSLT Example 1remove all _at_NOTE attributes
ltxslstylesheet version2.0"
xmlnsxsl"http//www.w3.org/1999/XSL/Transform"gt
ltxsltemplate match""gt ltxslcopygt
ltxslcopy-of select"_at_ except _at_NOTE"/gt
ltxslapply-templates/gt lt/xslcopygt lt/xsltemplat
egt lt/xslstylesheetgt
10XQuery Example 1remove all _at_NOTE attributes
xquery version 1.0 declare function
localcopy(node as element()) element
node-name(node) _at_ except _at_NOTE, for
c in childnode return typeswitch(c)
case e as element() return localcopy(a)
case t as text() return t case c as
comment() return c case p as
processing-instruction return p
localcopy(/)
11XQuery Example 2join two tables
xquery version 1.0 ltresultsgt for p in doc
("auction.xml")/site/people/person let a
for t in doc("auction.xml")
/site/closed_auctions/closed_auction
where t/buyer/_at_person p/_at_id
return t return ltitem person"p/name"gt
count (a) lt/itemgt lt/resultsgt
XMark Q8
12XSLT Example 2join two tables
ltresult xslversion"1.0" xmlnsxsl"http//www.w3
.org/1999/XSL/Transform"gt ltxslfor-each
select"/site/people/person"gt ltxslvariable
name"a" select"/site/closed_auctions/clo
sed_auction
buyer/_at_person current()/_at_id"/gt ltitem
person"name"gt ltxslvalue-of
select"count(a)"/gt lt/itemgt
lt/xslfor-eachgt lt/resultgt
XMark Q8
13XSLT Learning Barriers
- XML
- encoding, entities, whitespace, namespaces
- Declarative programming
- variables, recursion, paths, grouping
- Data model
- nodes not angle brackets
- Rule-based programming
- templates
14XQuery Learning Barriers
- XML
- encoding, entities, whitespace, namespaces
- Declarative programming
- variables, recursion, paths, grouping
- Data model
- nodes not angle brackets
- Rule-based programming
- templates
15Learning 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 3return x
spot the error!
16Performance and Optimization
- XMark Q8 (shown earlier)
- XQuery version
- XSLT version
- Database sizes
- 1Mb
- 4Mb
- 10Mb
17XMark Q8 results (msecs)
18Two can play at that game!
1Mb
XSLT
4Mb
10Mb
Xalan
1503
11006
65855
O(n2)
xt
160
2253
16414
MSXML
33
519
4248
Saxon 8.5
27
26
45
O(n)
XQuery
Saxon 8.5
16
16
31
Qizx
351
711
1813
Galax
1870
6672
16625
caveat this is one query only!
19Conclusions
- 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