XPath and XQuery - Search - University of Malta
Transcript of XPath and XQuery - Search - University of Malta
XPath and XQuery
Charlie AbelaDepartment of Artificial Intelligence
CSA 3210 XPath & XQuery © 2
Last Lecture
Introduction to XMLUse of DTD and XML schema to validate XML
Problems?
CSA 3210 XPath & XQuery © 3
Lecture Outline
XPath: a practical language to select nodes from an XML documentXQuery: compatible with XPath and is used for finding and extracting elements and attributes from XML documents. Exercise
CSA 3210 XPath & XQuery © 4
Querying XML
In relational databases, parts of a database can be selected and retrieved using SQL
Same necessary for XML documentsEssentially moving from relations to treesQuery languages: XQuery, XQL, XML-QL
The central concept of XML query languages is a path expression
Specifies how a node or a set of nodes, in the tree representation of the XML document can be reached
CSA 3210 XPath & XQuery © 5
XPath
XPath is a language for addressing parts of an XML document.
It operates on the tree data model of XMLIt has a non-XML syntax
Two types of Path ExpressionsAbsolute (starting at the root of the tree)
Syntactically they begin with the symbol /It refers to the root of the document (situated one level above the root element of the document)
Relative to a context node
CSA 3210 XPath & XQuery © 6
Path through Tree Model
Finding all the books with a title “Artificial Intelligence”
CSA 3210 XPath & XQuery © 7
Library Example<library location="Bremen">
<author name="Henry Wise"><book title="Artificial Intelligence" ISBN="12345678"/><book title="Modern Web Services" ISBN="87654"/><book title="Theory of Computation" ISBN="2468"/>
</author><author name="William Smart">
<book title="Artificial Intelligence" ISBN="1357"/></author><author name="Cynthia Singleton">
<book title="The Semantic Web" ISBN="5678"/><book title="Browser Technology Revised" ISBN="12345"/>
</author></library>
CSA 3210 XPath & XQuery © 8
General Form of Path Expressions
A path expression consists of a series of steps, separated by slashes
/library/author/book
A step consists of An axis specifier, A node test, and An optional predicate
//author[1]/book[1]axis specifier node test
predicates
CSA 3210 XPath & XQuery © 9
General Form of Path Expressions (2)
An axis specifier determines the tree relationship between the nodes to be addressed and the context node
E.g. parent, ancestor, child (the default), sibling, attribute node// is such an axis specifier: descendant or self
//child::author/attribute::name//author/@name
CSA 3210 XPath & XQuery © 10
General Form of Path Expressions (3)
A node test specifies which nodes to address
The most common node tests are element names E.g., * addresses all element nodescomment() addresses all comment nodes
//author/*
CSA 3210 XPath & XQuery © 11
General Form of Path Expressions (4)
Predicates (or filter expressions) are optional and are used to refine the set of addressed nodes
E.g., the expression [1] selects the first node[position()=last()] selects the last node[position() mod 2 =0] selects the even nodes
CSA 3210 XPath & XQuery © 12
Path Expressions in XPath (1)
Address all author elements/library/author
Addresses all author elements that are children of the library element node, which resides immediately below the root
CSA 3210 XPath & XQuery © 13
Result of Query (1)
CSA 3210 XPath & XQuery © 14
Path Expressions in XPath (2)
Address all author elements//author
Here // says that we should consider all elements in the document and check whether they are of type authorThis path expression addresses all authorelements anywhere in the document
CSA 3210 XPath & XQuery © 15
Result for Query (2)
CSA 3210 XPath & XQuery © 16
Path Expressions in XPath (3)
Address the location attribute nodes within library element nodes
/library/@location
The symbol @ is used to denote attribute nodes
CSA 3210 XPath & XQuery © 17
Result of Query 3
CSA 3210 XPath & XQuery © 18
Path Expressions in XPath (4)
Select all title attribute nodes within bookelements anywhere in the document, which have the value “Artificial Intelligence”
//book/@title="Artificial Intelligence"
CSA 3210 XPath & XQuery © 19
Path Expressions in XPath (5)
Select all books with title “Artificial Intelligence”
//book[@title="Artificial Intelligence"]
Test within square brackets: a filter expressionIt restricts the set of addressed nodes.
Difference with query 4. Query 5 filters all book elements, the title of which satisfies a certain condition.Query 4 checks for certain title attribute nodes of bookelements
CSA 3210 XPath & XQuery © 20
Result of Query (4)
CSA 3210 XPath & XQuery © 21
Result of Query (5)
CSA 3210 XPath & XQuery © 22
Path Expressions in XPath (6)
Address the first author element node in the XML document//author[1]
Address the last book element within the first author element node in the document//author[1]/book[last()]
Select all book element nodes whose title is not “Artificial Intelligence”//book[@title!=“Artificial Intelligence”]
CSA 3210 XPath & XQuery © 23
Results of Query set (6)
CSA 3210 XPath & XQuery © 24
Path Expressions in XPath (7)
Count the number of authorscount(//author)
List the books written by “Henry Wise”//author[@name="Henry Wise"]/book
Count the number of books written by “William Smart”count(//author[@name="William Smart"]/book)
CSA 3210 XPath & XQuery © 25
Results of Query set (7)
CSA 3210 XPath & XQuery © 26
XPath embedded in JSPs<%@ taglib uri="/src/tags/taglib.tld" prefix="mytag" %><html> <head>
<title>View Orders</title> </head> <body bgcolor="white" link="#666699" vlink="#666699"> <B> OrderDate: </B> <mytag:getvalue select=“/ORDER/DATE" /> <BR> <HR> <!-- ifdef tag checks whether the Xpath expression is valid --> <mytag:ifdef select=“/ORDER/SHIPTO" > <B> ShipTo:</B><BR/> <mytag:getvalue select=“ /ORDER/SHIPTO/NAME " /> <BR> <mytag:getvalue select=“ /ORDER/SHIPTO/STREET " /> <BR> <mytag:getvalue select=“ /ORDER/SHIPTO/CITY " /> <BR> <mytag:getvalue select=“ /ORDER/SHIPTO/STATE " /> <BR> <mytag:getvalue select=“ /ORDER/SHIPTO/ZIP " /> <BR>
</mytag:ifdef>
CSA 3210 XPath & XQuery © 27
What is XQuery?
A query language that allows you to:select elements/attributes from input documentsjoin data from multiple input documentsmake modifications to the datacalculate new dataadd new elements/attributes to the resultssort your results
Slides adapted from presentation by Priscilla Walmsleyhttp://www.datypic.com/services/xquery/intro.html
CSA 3210 XPath & XQuery © 28
Example
CSA 3210 XPath & XQuery © 29
Uses of XQuery
As varied as the uses for XML. Examples:Extracting information from a database for use in a Web serviceGenerating summary reports on data stored in an XML databaseSearching textual documents on the Web for relevant information and compiling the resultsSelecting and transforming XML data to XHTML to be published on the WebPulling data from databases to be used for application integrationSplitting up an XML document that represents multiple transactions into multiple XML documents
CSA 3210 XPath & XQuery © 30
XQuery Processing
CSA 3210 XPath & XQuery © 31
XML Input
Could be:text files that are XML documentsfragments of XML documents that are retrieved from the web using a URIa collection of XML documents that are associated with a particular URIdata stored in native XML databasesdata stored in relational databases that have an XML front-endin-memory XML documents
CSA 3210 XPath & XQuery © 32
Example: Catalogue.xml
CSA 3210 XPath & XQuery © 33
Selecting nodes
CSA 3210 XPath & XQuery © 34
Results
CSA 3210 XPath & XQuery © 35
Path Expressions and FLWOR
•FLWOR expressions are analogous to SQL Select statement•FLWOR is an acronym for "For, Let, Where, Order by, Return".
CSA 3210 XPath & XQuery © 36
Sort results
CSA 3210 XPath & XQuery © 37
Wrap results in a <ul> element
CSA 3210 XPath & XQuery © 38
Wrap each name in a <li> element
CSA 3210 XPath & XQuery © 39
Eliminate the name
CSA 3210 XPath & XQuery © 40
Tools
XPath Evaluator (free online):http://b-cage.net/code/web/xpath-evaluator.html
XQueryXMLSpy (trial): http://www.altova.comStylus Studio (trial): http://www.stylusstudio.com/
CSA 3210 XPath & XQuery © 41
XPath ExerciseDownload the file bibliography.xml (from course material page)Write XPath queries for the following:
the titles of articles the second article the second author of the second article the authors of the article whose title is 'Safe Constraint Queries' the titles of articles where 'Frank Neven' is one of the authors the titles of articles where both 'Frank Neven' and 'Dirk Van Gucht' are authors the titles of articles where either 'Frank Neven' or 'Dirk Van Gucht' is an author the number of articles the titles of articles with only one author the number of articles with more than three authors
Check your queries against XPath Evaluator or some other tool
CSA 3210 XPath & XQuery © 42
Suggested Reading
Semantic Web Primer, Chapter 2XML Path Language, J.Clark, http://www.w3.org/TR/xpathXPath tutorial: http://www.w3schools.com/xpath/default.aspXQuery tutorial: http://www.w3schools.com/xquery/default.aspDetailed XQuery tutorial: http://www.datypic.com/services/xquery/IntroductionToXQuery.pdf
CSA 3210 XPath & XQuery © 43
Next Lecture
Introduction to RDF and RDFSCore concepts of the languagesExamples
CSA 3210 XPath & XQuery © 44
<weather time="2002-06-06T15:35:00-05:00"> <report latitude="41.2° N" longitude="71.6° W"> <locality>Block Island</locality> <temperature units="°C">16</temperature> <humidity>88%</humidity> <dewpoint units="°C">14</dewpoint> <wind>
<direction>NE</direction> <speed units="km/h">16.1</speed> <gust units="km/h">31</gust>
</wind> <pressure units="hPa">1014</pressure><condition>overcast</condition> <visibility>13 km</visibility>
</report> <report latitude="34.1° N" longitude="118.4° W"> <locality>Santa Monica</locality> <temperature units="°C">19</temperature> <humidity>79%</humidity> <dewpoint units="°C">16</dewpoint> <wind>
<direction>WSW</direction> <speed units="km/h">14.5</speed>
</wind> <pressure units="hPa">1010</pressure><condition>hazy</condition> <visibility>5 km</visibility>
</report> </weather>
CSA 3210 XPath & XQuery © 45
QueriesSelect the report elements.
/weather/report
Select the first report element. /weather/report[1]
Select the temperature elements/weather/report/temperature
Select the report element whose locality is “Santa Monica”/weather/report[locality="Santa Monica"]
Select the longitude attribute of the first report element//report[locality="Block Island"]/attribute::longitudeSelect all the direction, speed, and gust elements/child::weather/child::report/child::wind/child::*Return the temperature on “Block Island” in degrees Fahrenheit9 * number(/weather/report[locality="Block Island"]/temperature) div 5 + 32
Select all the elements in the document./descendant::*