Xpath, Xquery and XSLT
Total Page:16
File Type:pdf, Size:1020Kb
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 <bookstore> <book category="COOKING"> <title lang="it">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="CHILDREN"> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>45.00</price> </book> </bookstore> 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 <title lang="en">Everyday Italian</title>, <title lang="en">Harry Potter</title> Path operator is a binary operator which applies the result of the LHS side to the RHS side and produces a result The LHS expression can be arbitrary provided that it is sequence type Trailing / is interpreted as root(self::node()) Double slashes // can be used a sort of wildcard to omit steps: bookstore//title 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 <title lang="it">Everyday Italian</title>, <price>30.00</price>, <title lang="en">Harry Potter</title>, <price>45.00</price> 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 <child>{data($x/title)}</child> else <adult>{data($x/title)}</adult> You can perform looping with for expressions: for $x in (1 to 5) return <test>{$x}</test> 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 <test>{$x}</test> which would produce <test>1 2 3 4 5</test> To invoke functions you can use them inside an element <name>{uppercase($booktitle)}</name> Alternatively you can use the pre-dened functions inside XPath expressions or let clauses doc("books.xml")//book[substring(title,1,5)='Harry'] let $name := (substring($booktitle,1,4)) 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: <minPrice> {local:minPrice($book/price,$book/discount)} </minPrice> 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: <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> Timo Lilja () XPath, XQuery and XSLT February 25, 2010 16 / 24 XSLT Example Let's assume that we have the following XML document: <?xml version="1.0" encoding="ISO-8859-1"?> <catalog> <cd> <title>Empire Burlesque</title> <artist>Bob Dylan</artist> <country>USA</country> <company>Columbia</company> <price>10.90</price> <year>1985</year> </cd> . </catalog> Timo Lilja () XPath, XQuery and XSLT February 25, 2010 17 / 24 XSLT We can apply the following XSL transformation to it ... <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr> <th>Title</th> <th>Artist</th> </tr> <xsl:for-each select="catalog/cd"> <xsl:sort select="artist"/> <xsl:if test="price > 10"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </xsl:if> </tr> </xsl:for-each> </table> </body> </html> ...</xsl:template> Timo Lilja () XPath, XQuery and XSLT February 25, 2010 18 / 24 XSLT The example would produce the result <?xml version="1.0" encoding="ISO-8859-1"?> <?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?> <catalog> <cd> <title>Empire Burlesque</title> <artist>Bob Dylan</artist> <country>USA</country> <company>Columbia</company> <price>10.90</price> <year>1985</year> </cd> . </catalog> Timo Lilja () XPath, XQuery and XSLT February 25, 2010 19 / 24 XSLT Instead of <xsl:if> you can use <xsl:choose> if you need more than one alternative: <xsl:choose> <xsl:when test="price > 10"> ... </xsl:when> <xsl:when test="price > 9"> ... </xsl:when> <xsl:otherwise> .... </xsl:otherwise> </xsl:choose> Timo Lilja () XPath, XQuery and XSLT February 25, 2010 20 / 24 XSLT You can call templates based on the element that matches the attribute ... <xsl:template match="cd"> <xsl:apply-templates select="title"/> <xsl:apply-templates select="artist"/> </xsl:template> <xsl:template match="title"> Title: <span style="color:#ff0000"> <xsl:value-of select="."/></span> </xsl:template> ... 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/.