- 3 languages: - XPath: describe sets of paths - XQuery: extension to XPath with an SQL-style - XSLT: language for XML transformations - XPath - Assumes that all operations produce a sequence of itemes (as opposed to bag of tuples) - An item is: - a primitive value (int, real, bool, string, etc.) - A node (many kinds, we talk about 3) - documents: files containing xml - elements: xml elements - attributes: attributes inside the elements - Document nodes: - every xpath query refers to a document Ex. doc("/usr/soule/movie.xml") - Path expression: - Sequence of tags and slashes Ex. /a/b/c - Start at the document, and evaluate teh expression - The "root" is considered a sub-element of the document node - Note that evaluating a path expression results in a sequence of elements. Ex. /MovieData/Movie - Can also use relative paths, don't start with / - /bookstore : select the root element bookstore - //book : select book element no matter where they are in the doc - Attributes use @ syntax Ex. /a/b/c/@A - find the sequence of elements by applying /a/b/c, then find attribute A - The result is a sequence of attributes - //@lang : select all lang attributed - Predicates: - bookstore/book[1] : select first book element that is a child of bookstore - /bookstore/book[last()] : select the last - //title[@lang='eng'] : Selects all the title elements that have an attribute named lang with a value of 'eng' - Wildcards: - * matches any element - @* any attribute - node() any node Ex: /bookstore/* : matches children of bookstore - | operator - //book | //price select all the title AND the price elements - Be careful, AND not OR - XPath Axes - Axes define node sets relative to current node - Ex: ancestor, child, descendant, following-sibling - Path syntax is: /step/step/... each step is: axisname::nodetest[predicate] Ex: child::*/child::price - Selects all price grandchildren of the current node - XPath operators: - Additive, multiplicative, relational, boolean (and, or) - Example: /MoviData/Star[//City = "Malibu"]/Name - Returns movie stars who have a house in Malibu - XQuery - Example: for $x in doc("books.xml")/bookstore/book where $x/price>30 order by $x/title return $x/title - Same model as XPath - A functional language => any XQuery expression can be used where expressions are expected - FLWR (flower) or FLWOR expressions: - for- let- where- return- clauses - for- let- where- order-by- return- clauses - Syntax: 0 or more for or let, 0 or 1 where, 1 return - example:
Title | Artist |
---|---|
. | . |