Examples

The placement of the where clause in a query affects processing. For example, this query selects all the books whose author’s first name is Mary:

select com.sybase.xml.xql.Xql.query
	("/bookstore/book[author/first-name ='Mary']”, xmlcol)
from XMLDAT
where
		com.sybase.xml.xql.Xql.query(“/bookstore/book
			[author/first-name= ‘Mary’]”, xmlcol)!=
		convert(com.sybase.xml.xql.Xql, null)>>EmptyResult
<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>

query() is invoked twice, once in the where clause and once in the select clause, which means the query executes twice and may be slow for large documents.

Thus, you can save the result set in an object while executing the query in the where clause and then restore the result in the select clause.

Or, you can write a class like HoldString, thats concatenates the results obtained from every invocation of com.sybase.xml.xql.Xql.query(), for each XML document in each row:

declare @result HoldString
select @result = new HoldString()
select @result>>get()
from XMLDAT
where
	@result>>put(com.sybase.xml.xql.Xql.query
	(“/bookstore/book[author/first-name= ‘Mary’]”, xmlcol))!=
convert(com.sybase.xml.xql.Xql,null)>>EmptyResult

Sybase advises that you do not store the result set in the where clause. The query does not always execute the where clause, so trying to retrieve its result in the select clause may generate an erroneous result set. HoldString is an example class.

Because Adaptive Server stores each document in a column of a given row, when the query scans a set of rows in the where clause, more than one row may satisfy the search criteria. If this occurs, the query returns a separate XML result document for each qualified row. For example, if you create the following table:

create table XMLTAB ( xmlcol image)
insert XMLTAB values 
	( com.sybase.xml.xql.Xql.parse(<xml><A><B><C>c</C></B></A></xml>));
insert XMLTAB values 
	( com.sybase.xml.xql.Xql.parse(<xml><D><E><C>c</C></E></D></xml>));

Then execute this query:

select com.sybase.xml.xql.Xql.query("//C", xmlcol)
from XMLTAB

You would expect to get the following result set:

<xql_result>
<C>c</C>
<C>c</C>
</xql_result>

Instead, the result set returns the same row twice, once from the select clause and once from the where clause:

<xql_result>
<C>c</C>
</xql_result>

<xql_result>
<C>c</C>
</xql_result>