XPath, XQuery and XSLT
Timo Lilja
February 25, 2010
Timo Lilja () XPath, XQuery and XSLT February 25, 2010 1 / 24 Introduction
When processing, XML documents are organized into a tree structure in the memory All XML Query and transformation languages operate on this tree XML Documents consists of tags and attributes which form logical constructs called nodes The basic operation unit of these query/processing languages is the node though access to substructures is provided
Timo Lilja () XPath, XQuery and XSLT February 25, 2010 2 / 24 XPath XPath
XPath 2.0 [4] is a W3C Recomendation released on January 2007 XPath provides a way to query the nodes and their attributes, tags and values XPath type both dynamic and static typing if the XML Scheme denition is present, it is used for static type checking before executing a query otherwise nodes are marked for untyped and they are coerced dynamically during run-time to suitable types
Timo Lilja () XPath, XQuery and XSLT February 25, 2010 3 / 24 XPath
Timo Lilja () XPath, XQuery and XSLT February 25, 2010 4 / 24 XPath Path expressions
Path expressions provide a way to choose all matching branches of the tree representation of the XML Document Path expressions consist of one or more steps separated by the path operater/. A step can be an axis which is a way to reference a node in the path. a node-test which corresponds to a node in the actual XML document zero or more predicates Syntax for the path expressions: /step/step Syntaxc for a step: axisname::nodestest[predicate]
Timo Lilja () XPath, XQuery and XSLT February 25, 2010 5 / 24 XPath
Let's assume that we use the following XML Docuemnt
Timo Lilja () XPath, XQuery and XSLT February 25, 2010 6 / 24 XPath
To query all the titles from the above document one could sa bookstore/book/title The result would be
Timo Lilja () XPath, XQuery and XSLT February 25, 2010 7 / 24 XPath Examples
To select the rst node: /bookstore/book[1] To select the last node: /bookstore/book[last()] To select all elements with attribute lang set to "en" /bookstore/book/title[@lang="en"] To select all elements whose price is more than 35.00 euros /bookstore/book[price>35.00]/title To select all ancestors or self from a book node: bookstore/book/ancestor-or-self::book
Timo Lilja () XPath, XQuery and XSLT February 25, 2010 8 / 24 XPath Operators
XPath provides a set of operators for arithmetic ( +, -,*, div,mod logical tests (=, != <, <=, >, >=) and boolean operators (or, and) and node-set combination |. The pipe operator can be used to select several paths //book/title | //book/price which would produce
Timo Lilja () XPath, XQuery and XSLT February 25, 2010 9 / 24 XPath Functions
XPath has a function library with string conversion, regular expressions, arithmetic, date and time utilities. The library is shared with XQuery 1.0. See the document on function operators [7]. For example to convert the results to lower case: //book/lower-case(title) and the result would be "everyday italian", "harry potter"
Timo Lilja () XPath, XQuery and XSLT February 25, 2010 10 / 24 XQuery XQuery 1.0
XQuery 1.0 [1] a functional query language that can be used to query XML document data XQuery is a superset of XPath and extendes it with FLWOR expressions (FOR, LET, WHERE, ORDER BY, RETURN) XQuery doesn't support mutation and roughly corresponds to SQL's Select statements with no support for update/insert/delete. A draft standard that would extend XQuery to support updating the XML document data through XQuery [3]. Namespaces and functions are supported
Timo Lilja () XPath, XQuery and XSLT February 25, 2010 11 / 24 XQuery Examples
FLOWR expressions allow sorting the expressions for $x in doc("books.xml")/bookstore/book where $x/price>30 order by $x/title return $x/title XQuery supports conditional expressions: for $x in doc("books.xml")/bookstore/book return if ($x/@category="CHILDREN") then
Timo Lilja () XPath, XQuery and XSLT February 25, 2010 12 / 24 XQuery Examples
You can bind variables with let clauses: let $x := (1 to 5) return
Timo Lilja () XPath, XQuery and XSLT February 25, 2010 13 / 24 XQuery Functions
You can dene functions with type annotations declare function local:minPrice($p as xs:decimal?, $d as xs:decimal?) AS xs:decimal? { let $disc := ($p * $d) div 100 return ($p - $disc) } To invoke the function:
Timo Lilja () XPath, XQuery and XSLT February 25, 2010 14 / 24 XQuery Namespaces
Recursion is supported and namespaces are dened as below: declare namespace factorial; (: = "http://example.com/factorial"; :) declare function factorial:fact($i as xs:integer) as xs:integer { if ($i <= 1) then 1 else $i * factorial:fact($i - 1) }; To invoke: factorial:fact(4)
Timo Lilja () XPath, XQuery and XSLT February 25, 2010 15 / 24 XSLT XSLT
XSLT [6] is a transformation language for XML documents Unlike XPath, uses XML syntax XPath expressions are used for path expression querying XSLT is a stylesheet language instead of database query language XQuery supports querying multiple documents at the same time while XSLT handles one document at a time. Most of the modern browsers support XSLT natively if the HTML document refers to XSLT document accordingly:
Timo Lilja () XPath, XQuery and XSLT February 25, 2010 16 / 24 XSLT Example
Let's assume that we have the following XML document:
Timo Lilja () XPath, XQuery and XSLT February 25, 2010 17 / 24 XSLT
We can apply the following XSL transformation to it ... My CD Collection
... Title Artist
Timo Lilja () XPath, XQuery and XSLT February 25, 2010 18 / 24 XSLT
The example would produce the result
Timo Lilja () XPath, XQuery and XSLT February 25, 2010 19 / 24 XSLT
Instead of
Timo Lilja () XPath, XQuery and XSLT February 25, 2010 20 / 24 XSLT
You can call templates based on the element that matches the attribute ...
Timo Lilja () XPath, XQuery and XSLT February 25, 2010 21 / 24 Tools Tools
Gnome libxml [8] Xpath and XSLT support xsltproc for cli XSLT processing Bindings for virtually every programming language XQilla [2] Xpath 2.0 and XQuery 1.0 Written in C++ Galax [5] XPath 2.0 and XQuery 1.0, 99.4% conformance Written in OCaml
Timo Lilja () XPath, XQuery and XSLT February 25, 2010 22 / 24 References ReferencesI
S. Boag, D. Chamberlin, M. F. Fernández, D. Florescu, J. Robie, and J. Siméon. XQuery 1.0: An XML Query Language, 2007. http://www.w3.org/TR/xquery/. Y. Cai. XQilla. http://xqilla.sourceforge.net/HomePage. D. Chamberlin, M. Dyck, D. Florescu, J. Melton, J. Robie, and J. Siméon. XQuery Update Facility 1.0, 2009. http://www.w3.org/TR/xquery-update-10. J. Clark and S. DeRose. XML Path Language (XPath) 2.0, 2007. http://www.w3.org/TR/xpath20/.
Timo Lilja () XPath, XQuery and XSLT February 25, 2010 23 / 24 References ReferencesII
M. Fernández and J. Siméon. Galax. http://galax.sourceforge.net/. M. Kay. XSL Transformations (XSLT) Version 2.0, 2007. http://www.w3.org/TR/xslt20/. A. Malhotra, J. Melton, and N. Walsh. XQuery 1.0 and XPath 2.0 Functions and Operators, 2007. http://www.w3.org/TR/xquery-operators/. D. Veillard. libxml - The XML library for Gnome. http://xmlsoft.org/.
Timo Lilja () XPath, XQuery and XSLT February 25, 2010 24 / 24