Sybase Adaptive Server Enterprise (ASE) uses Java classes for powerful XML SQL integration. However, equally important to accessing XML data in ASE is XML Query Language (XQL), a standard query language designed to search an XML document for nodes with particular characteristics.  By learning some of the basics of XQL, such as parsers and XQL operators, you can begin to understand the flexibility afforded in designing client/server, n-tier, and Web applications in Java using Sybase ASE as the database repository.

While it would be possible, since XML documents are stored as standard SQL image and text columns, to use string functions, such as substring() and charindex(), to navigate an XML document, this is an inefficient method of parsing XML. Instead, the developer should use a specialized tool designed for navigation, the XML parser.

You use the parser to navigate the paths to the nodes of the hierarchical tree that comprises the XML document. This path was designed to mimic a file path within a directory structure. This similarity allows HTML and the other markup languages used throughout Internet development to be compatible with XQL in the same way they were designed to handle both URL and file directory paths.

Parsers can be used as classes in languages like C++ and Java or as stand-alone programs that take arguments on the command line to determine exactly what you want to extract and where you want the results to go.

In Sybase ASE, you can use any Simple API for XML (SAX) 2.0-compatible parser. In my examples, I will use the Xerces parser (1.3.1) that ships with Sybase ASE.

Converting an existing raw XML column to a parsed XML column
The example in Listing A uses the parse() method to convert and parse a raw text or image XML document.

This example converts the xmlcol column of the XMLTEXT table to parsed data and stores it in the xmldoc column.

Inserting, updating, and deleting XML documents
The example in Listing B uses the parse() method to insert an XML document, which takes the XML document as the argument and returns sybase.aseutils.SybXmlStream.

Note that although Adaptive Server can implicitly map both text and image data to InputStream, it will write sybase.ase.SybXmlStream data only to image columns. Attempts to write this to text columns will result in an error.

Basic operators
Most of the operators available in the XQL specification facilitate the path you take to extract a node, a set of nodes, or many other types of derived information about the XML document. To give you an idea of how XQL works, I will discuss the most common of its operators.

Child operator/—indicates hierarchy. The example in Listing C returns <book> elements that are children of <bookstore> elements from the xmlcol column of the xmlimage table.

Descendant operator//—indicates that a search using the descendant operator will find an occurrence of an element at any level of the XML structure. The query in Listing D finds all the instances of <emph> elements that occur in an <excerpt> element.

Equals operator= —specifies the content of an element or the value of an attribute. The query in Listing E finds all examples where the last name equals Bob.

Filter operator[ ]—filters the set of nodes to its left, based on the conditions inside the brackets. The example in Listing F finds any occurrences of authors listed in a book element whose first name is Mary.

Subscript operator—[index_ordinal]—finds a specific instance of an element. The example in Listing G finds the second book listed in the XML document. Remember that XQL is zero-based, so it begins numbering at 0.

Boolean expressions—You can use Boolean expressions within filter operators. For example, the query shown in Listing H returns all <author> elements that contain at least one <degree> and one <award>.

As would be expected from a modern query language, the basic XQL operators alone provide a powerful syntax to query data. In future articles, I’ll explore setting up ASE servers to use XQL and drill down on some performance considerations.