Querying XML
From Suhrid.net Wiki
Jump to navigationJump to searchIntro
- 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