XMLTABLE example: Returning one row for each occurrence of an item

If an XML document contains multiple occurrences of an element, you can use XMLTABLE to generate a row for each occurrence of the element.

For example, the following XML documents are stored in the sample CUSTOMER table:
<customerinfo xmlns="http:⁄⁄posample.org" Cid="1001">
  <name>Kathy Smith<⁄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 can use a query like this to create a table in which every phone value is returned in a separate row:
SELECT X.*
FROM CUSTOMER C, XMLTABLE (XMLNAMESPACES(DEFAULT 'http:⁄⁄posample.org'),
  '$cust⁄customerinfo⁄phone' PASSING C.INFO as "cust"
  COLUMNS "CUSTNAME" VARCHAR(30) PATH '..⁄name',
  "PHONETYPE" VARCHAR(30) PATH '@type',
  "PHONENUM" VARCHAR(15) PATH '.') as X
  WHERE CID=1001 OR CID=1003
                         
The result table of the SELECT statement is:
Table 1. Result table from a query that uses XMLTABLE to retrieve multiple occurrences of an item
CUSTNAME PHONETYPE PHONENUM
Kathy Smith work 905-555-7258
Robert Shoemaker work 905-555-7258
Robert Shoemaker home 416-555-2937
Robert Shoemaker cell 905-555-8743
Robert Shoemaker cottage 613-555-3278
The following SELECT statement returns each phone element as an XML document, instead of a string value:
SELECT X.*
FROM CUSTOMER C, XMLTABLE (xmlnamespaces (DEFAULT 'http:⁄⁄posample.org'),
  '$cust⁄customerinfo⁄phone' PASSING C.INFO as "cust"
  COLUMNS "CUSTNAME" CHAR(30) PATH '..⁄name',
  "PHONETYPE" CHAR(30) PATH '@type',
  "PHONENUM" XML PATH '.') as X
  WHERE CID=1001 OR CID=1003
This query yields the following results for the two XML documents:
Table 2. Result table
CUSTNAME PHONETYPE PHONENUM
Kathy Smith work <phone xmlns="http:⁄⁄posample.org" type="work">905-555-7258<⁄phone>
Robert Shoemaker work <phone xmlns="http:⁄⁄posample.org" type="work">905-555-7258<⁄phone>
Robert Shoemaker home <phone xmlns="http:⁄⁄posample.org" type="work">416-555-2937<⁄phone>
Robert Shoemaker cell <phone xmlns="http:⁄⁄posample.org" type="work">905-555-8743<⁄phone>
Robert Shoemaker cottage <phone xmlns="http:⁄⁄posample.org" type="work">613-555-3278<⁄phone>