Jason Bennett's Developer Corner

 






Click to see the XML version of this web page.




View David Jason Bennett's profile on LinkedIn

 

 

A Little About Jason Bennett ...

I've had an interest in publishing technical articles and HELPFUL code for a few years.  I am (by trade and hobby) a developer who specializes in Oracle technologies and web based architectures.  I have been an employee of both TUSC and Oracle Corporation.  My intent here is to share my ideas and coding experiences with the developer community as a whole.  As with all developers some of my ideas are great and some of them are ....  well you know.  Anyway, I hope you find something here that will aid in your endeavor, or spark a new idea. 

I am more than happy to assist with technical issues and will even write a little code if need be. If you find something on the site that is really useful and you'd like to make a contribution (absolutely up to you and absolutely not required), just click the "Make a Donation" button on the left!

Good luck and good coding !




  Monday, March 02, 2009


Creating an XML Cursor using XMLTable and XQuery

 

Oracle has added some really cool XML features over the years.  Not the least of which include the XMLType, XQuery, and the XMLTable function.  Using these three features, we can take any XML document (or fragment) and query it like a table.  This type functionality makes it really easy import and exchange data between systems in an XML message format.  This entry assumes some familiarity with the afore mentioned Oracle features (XMLType, XQuery, and XMLTable), and XPath. (Note: This technique will not work with Oracle 10g Express Edition.  XQuery is not implemented in that version of the Oracle RDBMS.)

The example in this entry will take a simple XML document containing a repeating record set with an identifying attribute for each record set and turn it into a cursor.  Here is the example code:

 

DECLARE

 

CURSOR crsrSampleData(cv_sample_data XMLType) IS

SELECT

       a.record_id,

       a.name,

       a.id_no

FROM

       XMLTable('for $i in $xml return $i//SampleRecord'

                 PASSING cv_sample_data as XML

                 COLUMNS

                    record_id   VARCHAR2(16) PATH '/SampleRecord/@id',

                    name        VARCHAR2(30) PATH '/SampleRecord/SampleName',

                    id_no       NUMBER(10)   PATH '/SampleRecord/SampleIdNo') a;

 

/* Create an XMLType to pass as a cursor variable. */

v_xml_doc XMLType := XMLType('<?xml version="1.0" standalone="yes"?>

                              <SampleData>

                                <SampleRecord id="12345678">

                                  <SampleIdNo>1284793</SampleIdNo>

                                  <SampleName>ALLEN</SampleName>

                                </SampleRecord>

                                <SampleRecord id="56789345">

                                  <SampleIdNo>345678</SampleIdNo>

                                  <SampleName>JOE</SampleName>

                                </SampleRecord>

                                <SampleRecord id="786543456">

                                  <SampleIdNo>4564321</SampleIdNo>

                                  <SampleName>TOM</SampleName>

                                </SampleRecord>

                              </SampleData>');

  

  

BEGIN

 

   FOR data_rec IN crsrSampleData(v_xml_doc) LOOP

  

      DBMS_OUTPUT.PUT_LINE('Record Id='||data_rec.record_id);

      DBMS_OUTPUT.PUT_LINE('Name='||data_rec.name); 

      DBMS_OUTPUT.PUT_LINE('Id#='||data_rec.id_no);

  

   END LOOP;

 

END;

/

 

Execution of the code sample above will produce the following result:

 

Record Id=12345678

Name=ALLEN

Id#=1284793

Record Id=56789345

Name=JOE

Id#=345678

Record Id=786543456

Name=TOM

Id#=4564321  

 

The XMLTable function is the key piece of code in the example above.  The XMLTable function allows us to shred an XML document into a table like structure of our choosing based upon the data contained in the XML document.  In the example above, we map our XMLType cursor variable to the $xml XQuery variable in the XQuery statement 'for $i in $xml return $i//SampleRecord' using the PASSING statement.  The XQuery statement will return the sequence of SampleRecord tag nodes. Next, in the COLUMNS statement of the XMLTable function, we use XPath statements to map the desired data elements and attributes to column names that we define.  We have taken an XML document and put it in a format that is both familiar and easy to handle for the average PL/SQL developer.

 


10:42:36 PM    

Click here to visit the Radio UserLand website. © Copyright 2009Jason Bennett.
Last update: 3/11/2009; 4:52:53 PM.

March 2009
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
Jan   Apr