- 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: - let: let variable := expression - for: for variable in expression - where: where condition - order: order by list-of-expressions - return: return expression - Syntax: - XQuery is case-sensitive - XQuery elements, attributes, and variables must be valid XML names - An XQuery string value can be in single or double quotes - An XQuery variable is defined with a $ followed by a name, e.g. $bookstore - XQuery comments are delimited by (: and :), e.g. (: XQuery Comment :) - Conditional expressions: for $x in doc("books.xml")/bookstore/book return if ($x/@category="CHILDREN") then {data($x/title)} else {data($x/title)} - Note that the if statement is an expression! - Comparision operators: - General: =, !=, <, <=, >, >= - Value: eq, ne, lt, le, gt, ge Example: $bookstore//book/@q > 10 returns true if any q attributes have a value greater than 10: $bookstore//book/@q gt 10 returns true if there is only one q attribute returned by the expression, and its value is greater than 10. If more than one q is returned, an error occurs: - XSLT: - Used to transform XML to HTML (or similar forms) - Is another query langauge to extract data from XML or turn one form into another - Sylesheets: - XMLT specifications are XML documents - Specifications are called stylesheets - Templates - Each stylesheet will have 1 or more templates. - To apply a stylesheet to an XML doc, we look for matching templates. - The following matches the "root" - Obtaining values from XML: - Value-of: - for-each ... - conditional: ...some output if the expression is true... - "case statement" ... some output ... ... some output .... - Recursion: - The element applies a template to the current element or to the current element's child nodes. - Example:

My CD Collection

Title Artist
. .