Difference between revisions of "Querying XML"

From Suhrid.net Wiki
Jump to navigationJump to search
Line 31: Line 31:
 
* doc("Bookstore.xml")/Bookstore/Book[@Price < 90]/Title - Above, but return only title.
 
* doc("Bookstore.xml")/Bookstore/Book[@Price < 90]/Title - Above, but return only title.
 
* doc("Bookstore.xml")/Bookstore/Book[Remark]/Title - Existence condition, Book must have a remark element
 
* doc("Bookstore.xml")/Bookstore/Book[Remark]/Title - Existence condition, Book must have a remark element
* doc("Bookstore.xml")/Bookstore/Book[@Price < 90 and Authors/Author/Last_Name = "Ullman"]/Title
+
* doc("Bookstore.xml")/Bookstore/Book[@Price < 90 and Authors/Author/Last_Name = "Ullman" and Authors/Author/First_Name = "Jennifer" ]/Title  : Bigger condition. The second part is actually  a "there exists". So actually not doing an AND.
 +
* doc("Bookstore.xml")/Bookstore/Book[@Price < 90 and Authors/Author[Last_Name = "Ullman" and First_Name = "Jennifer" ]/Title : This is the correct one.

Revision as of 07:24, 5 February 2014

Intro

  • Not as mature as querying relational databases
  • No underlying algebra
  • XPath : Path expressions and conditions
  • XSLT : XPath + Transformations, output processing
  • XQuery : XPath + full featured query language
  • XLink, XPointer : Use XPath as a component

XPath

  • Think of XML as a tree
  • Expressions in XPath as navigations down/across the tree with conditions
  • / - root element + separator, Element name, * is wildcard, @ for attribute
  • // - any descendant of the current element including self
  • condition in square bracket. [price < 50]. Also [] used as array access.
  • Many built-in functions : e.g. contains(s1, s2) : true/false. name() : returns element tag name
  • Navigation axes : e.g. parent, following-sibling, descendants
  • XPath queries operate on and return sequence of elements for XML document & XML stream
  • Sometimes result of XPath query can be expressed in XML, but not always

Sample queries

  • doc("Bookstore.xml")/Bookstore/Book/Title - returns titles of all books
  • doc("Bookstore.xml")/Bookstore/(Book | Magazine)/Title - titles of all books or magazines
  • doc("Bookstore.xml")/Bookstore/*/Title - wildcard
  • doc("Bookstore.xml")//Title - any Title element anywhere in the tree - Double slash
  • doc("Bookstore.xml")//* - Will print the whole tree for the root, then subtree for the child etc
  • doc("Bookstore.xml")/Bookstore/Book/data(@ISBN) - Data operator needs to be specified
  • doc("Bookstore.xml")/Bookstore/Book[@Price < 90] - Condition, price < 90 : Will print the whole book
  • doc("Bookstore.xml")/Bookstore/Book[@Price < 90]/Title - Above, but return only title.
  • doc("Bookstore.xml")/Bookstore/Book[Remark]/Title - Existence condition, Book must have a remark element
  • doc("Bookstore.xml")/Bookstore/Book[@Price < 90 and Authors/Author/Last_Name = "Ullman" and Authors/Author/First_Name = "Jennifer" ]/Title : Bigger condition. The second part is actually a "there exists". So actually not doing an AND.
  • doc("Bookstore.xml")/Bookstore/Book[@Price < 90 and Authors/Author[Last_Name = "Ullman" and First_Name = "Jennifer" ]/Title : This is the correct one.