XML Query Language (XQL) has been designed as a general-purpose query language for XML. XQL is a path-based query language for addressing and filtering the elements and text of XML documents, and is a natural extension to XSL syntax. XQL provides a concise, understandable notation for pointing to specific elements and for searching for nodes with particular characteristics. XQL navigation is through elements in the XML tree.
SQL and XQL are independent languages. The examples presented here apply to XQL only.
The most common XQL operators include:
Child operator, / – indicates hierarchy. The following example returns <book> elements that are children of <bookstore> elements from the xmlcol column of the xmlimage table:
select com.sybase.xml.xql.Xql.query("/bookstore/book", xmlcol) from xmlimage <xql_result> <book style=autobiography> <title>S
Descendant operator, // – indicates that the query searches through any number of intervening levels. That is, a search using the descendant operator finds an occurrence of an element at any level of the XML structure. The following query finds all the instances of <emph> elements that occur in an <excerpt> element:
select com.sybase.xml.xql.Xql.query
("/bookstore/book/excerpt//emph",xmlcol) from xmlimage <xql_result> <emph>I</emph> </xql_result>
Equals operator, = – specifies the content of an element or the value of an attribute. The following query finds all examples where “last-name = Bob”:
select com.sybase.xml.xql.Xql.query
("/bookstore/book/author[last-name='Bob']", xmlcol) from xmlimage <xql_result> <author> <first-name>Joe</first-name> <last-name>Bob</last-name> <award>Trenton Literary Review Honorable Mention</award></author> <author> <first-name>Mary</first-name> <last-name>Bob</last-name> <publication>Selected Short Stories of <first-name>Mary</first-name> <last-name>Bob</last-name></publication></author> <author> <first-name>Toni</first-name> <last-name>Bob</last-name> <degree from=Trenton U>B.A.</degree> <degree from=Harvard>Ph.D.</degree> <award>Pulizer</award> <publication>Still in Trenton</publication> <publication>Trenton Forever</publication></author> “</xql_result>
Filter operator, [ ] – filters the set of nodes to its left, based on the conditions inside the brackets. This example finds any occurrences of authors whose first name is Mary that are listed in a book element:
select com.sybase.xml.xql.Xql.query
("/bookstore/book[author/first-name = 'Mary']", xmlcol) from xmlimage <xql_result> <book style=textbook> <title>History of Trenton</title> <author> <first-name>Mary</first-name> <last-name>Bob</last-name> <publication>Selected Short Stories of <first-name>Mary</first-name> <last-name>Bob</last-name></publication></author> <price>55</price></book>
Subscript operator, [index_ordinal] – finds a specific instance of an element. This example finds the second book listed in the XML document. Remember that XQL is zero-based, so it begins numbering at 0:
select com.sybase.xml.xql.Xql.query("/bookstore/book[1]", xmlcol) from xmlimage Query returned true and the result is <xql_result> <book style=textbook> <title>History of Trenton</title> <author> <first-name>Mary</first-name> <last-name>Bob</last-name> <publication>Selected Short Stories of <first-name>Mary</first-name> <last-name>Bob</last-name></publication></author> <price>55</price></book> </xql_result>
Boolean expressions – you can use Boolean expressions within filter operators. For example, this query returns all <author> elements that contain at least one <degree> and one <award>:
select com.sybase.xml.xql.Xql.query
("/bookstore/book/author[degree and award]", xmlcol) from xmlimage <xql_result> <author> <first-name>Toni</first-name> <last-name>Bob</last-name> <degree from=Trenton U>B.A.</degree> <degree from=Harvard>Ph.D.</degree> <award>Pulizer</award> <publication>Still in Trenton</publication> <publication>Trenton Forever</publication></author> </xql_result>