Non-empty sequences returned by XMLQUERY
If evaluation of the XQuery expression that you specify in XMLQUERY results in a non-empty sequence, the XMLQUERY function returns that sequence.
Example: Suppose that two of the documents in the INFO column
of the sample CUSTOMER table look like these:
<customerinfo xmlns="http:⁄⁄posample.org" Cid="1002">
<name>Jim Noodle<⁄name>
<addr country="Canada">
<street>25 EastCreek<⁄street>
<city>Markham<⁄city>
<prov-state>Ontario<⁄prov-state>
<pcode-zip>N9C 3T6<⁄pcode-zip>
<⁄addr>
<phone type="work">905-555-7258<⁄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>
You execute the following statement: SELECT CID, XMLQUERY ('declare default element namespace "http://posample.org";
/customerinfo/phone' passing INFO)
AS "PHONE FROM INFO"
FROM CUSTOMER
WHERE CID IN (1002,1003)
The result table contains the
following two rows:
CID | PHONE FROM INFO |
---|---|
1002 | <?xml version="1.0" encoding="IBM037"?><phone xmlns="http://posample.org" type="work">905-555-7258</phone> |
1003 | <?xml version="1.0" encoding="IBM037"?><phone xmlns="http://posample.org" type="work">905-555-7258</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> |
The first row contains a sequence of one <phone> element, and the second row has a sequence of four <phone> elements. This result demonstrates that when XMLQUERY returns a sequence that contains more than one element, the serialization process concatenates the elements into a single string. The result in the second row is not a well-formed document. Ensure that any application that receives this result can properly handle this behavior.