Ability to execute an XQuery expression

[Note]

The XQuery specification defines the result of an XQuery expression as a sequence that contains 0, 1, or more items. XQuery expressions can be executed in DB2 either using SQL as the primary language, with the XMLQUERY SQL function, or using XQuery as the primary language. When an XQuery expression is executed using either method, an XML sequence is returned. How the resulting sequence appears in a result set, however, differs depending on whether SQL or XQuery is used as the primary language:

The example that follows illustrates the difference between the result sets from these two querying methods.

To fetch results from queries that use XQuery or XMLQUERY, fetch results from within your application, as you normally would any other result set. Bind your application variable to the result set and fetch until the end of the result set. If the XQuery expression (issued directly or through XMLQUERY) returned an empty sequence, then the row in the result set is also empty.

Difference in result sets from XQuery and XMLQUERY

If the following two XML documents are stored in an XML column, to retrieve all <phone> elements, you can use either XQuery or XMLQUERY. The result sets that are returned by these two methods differ, however, and should be handled accordingly by the application when fetching from the result set.


<customerinfo xmlns="http://posample.org" Cid="1000">
  <name>Kathy Smith</name>
  <addr country="Canada">
    <street>5 Rosewood</street>
    <city>Toronto</city>
    <prov-state>Ontario</prov-state>
    <pcode-zip>M6W 1E6</pcode-zip>
  </addr>
  <phone type="work">416-555-1358</phone>
</customerinfo>

<customerinfo xmlns="http://posample.org" Cid="1003">
  <name>Robert Shoemaker</name>
    <addr country="Canada">
    <street>1596 Baseline</street>
    <city>Aurora</city>
    <prov-state>Ontario</prov-state>
    <pcode-zip>N8X 7F8</pcode-zip>
  </addr>
  <phone type="work">905-555-7258</phone>
  <phone type="home">416-555-2937</phone>
  <phone type="cell">905-555-8743</phone>
  <phone type="cottage">613-555-3278</phone>
</customerinfo>

					

Executing an XQuery expression using XQuery as the primary language yields 5 rows in the result set, as follows:

XQUERY declare default element namespace "http://posample.org";
  db2-fn:xmlcolumn ('CUSTOMER.INFO')/customerinfo/phone
					


--------------------------------------------------------------------
<phone xmlns="http://posample.org" type="work">416-555-1358</phone>
--------------------------------------------------------------------
<phone xmlns="http://posample.org" type="work">905-555-2937</phone>
--------------------------------------------------------------------
<phone xmlns="http://posample.org" type="home">416-555-2937</phone>
--------------------------------------------------------------------
<phone xmlns="http://posample.org" type="cell">905-555-8743</phone>
--------------------------------------------------------------------
<phone xmlns="http://posample.org" type="cottage">613-555-3278</phone>
--------------------------------------------------------------------
					
					

Executing an XQuery expression through XMLQUERY yields 2 rows in the result set, as follows, where all <phone> elements of the second row in the table are concatenated in a single scalar value (an XML sequence):

SELECT XMLQUERY ('declare default element namespace "http://posample.org";
                 $doc/customerinfo/phone' PASSING INFO AS "doc")
FROM CUSTOMER
					

--------------------------------------------------------------------
<phone xmlns="http://posample.org" type="work">416-555-1358</phone>
--------------------------------------------------------------------
<phone xmlns="http://posample.org" type="work">905-555-2937</phone><phone xmlns="http://posample.org" 
type="home">416-555-2937</phone><phone xmlns="http://posample.org" type="cell">905-555-8743</phone><phone 
xmlns="http://posample.org" type="cottage">613-555-3278</phone>
--------------------------------------------------------------------
					
					
Notice that the second row of this result set contains a value that is not a well-formed XML document.

These differences in the result sets exist because XMLQUERY is a scalar function. It executes on each row of the table and the resulting sequence from a row of the table, forms a row of the result set. XQuery, however, returns each item of a sequence as a separate row of the result set.

Professional hosting     Belorussian informational portal         Free SCWCD 1.4 Study Guide     Free SCDJWS 1.4 Study Guide     SCDJWS 1.4 Quiz     Free IBM Certified Associate Developer Study Guide     IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide     SCDJWS 5.0 Quiz