Download

0 views
484 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
0
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Download

  1. 1. Using XML With DB2 XML-Related Functions and Programming Language Support Gregg Lippa Themis Inc. http://www.themisinc.com [email_address]
  2. 2. <ul><li>Gregg Lippa is currently a Senior Technical Advisor at Themis Inc. He teaches DB2-related courses on SQL, Application Programming, and optimization, performance and tuning as well as Java, J2EE, distributed computing and Java-related topics. Gregg has worked with DB2 as a consultant and trainer for over 20 years and with Java for 10 years. </li></ul><ul><li>This material is taken from the Themis course DB1091: DB2 9 for z/OS pureXML Features . For more information visit http://www.themisinc.com. </li></ul><ul><li>Products and company names mentioned may be trademarks of their respective companies. Mention of third party products or software is for reference only and constitutes neither a recommendation nor an endorsement. </li></ul>
  3. 3. The XML Data Type <ul><li>DB2 native XML support includes a new XML data type </li></ul><ul><ul><li>Available to CREATE TABLE and ALTER TABLE </li></ul></ul><ul><li>XML type column holds one XML document for each row </li></ul><ul><ul><li>Must be a well-formed XML document having: </li></ul></ul><ul><ul><ul><li>One root element, proper end tags, proper nesting, attribute values enclosed within (either single or double) quotes, case-sensitivity </li></ul></ul></ul><ul><ul><li>Insert or update statements fail if XML is not well-formed </li></ul></ul><ul><li>XML storage requirements </li></ul><ul><ul><li>XML data is stored in a parsed tree structure </li></ul></ul><ul><ul><li>Separate XML tablespace plus space for needed indexes </li></ul></ul>CREATE TABLE MYSAMPLE (REGDATA DECIMAL(10,0), XMLDATA XML )
  4. 4. Implicit Objects Created to Support XML Column <ul><li>Creating a table with an XML column causes DB2 to implicitly create several objects to support it : </li></ul><ul><ul><li>A hidden column called DB2_GENERATED_DOC_ID_FOR_XML (a.k.a. DocID ) which uniquely represents each row </li></ul></ul><ul><ul><li>Unique index is defined on the DocID column </li></ul></ul><ul><ul><li>An XML table space </li></ul></ul><ul><ul><li>An XML table with columns docid, min_nodeid, and xmldata </li></ul></ul><ul><ul><li>A NodeID index on the XML table with key DocID and xmldata </li></ul></ul>
  5. 5. SQL/XML Functions <ul><li>DB2 V8 introduced several XML publishing functions: </li></ul><ul><ul><li>XMLAGG </li></ul></ul><ul><ul><li>XMLATTRIBUTES </li></ul></ul><ul><ul><li>XMLCONCAT </li></ul></ul><ul><ul><li>XMLELEMENT </li></ul></ul><ul><ul><li>XMLFOREST </li></ul></ul><ul><ul><li>XMLNAMESPACE </li></ul></ul><ul><li>DB2 V9 adds four new functions for constructing XML documents: </li></ul><ul><ul><li>XMLCOMMENT </li></ul></ul><ul><ul><li>XMLDOCUMENT </li></ul></ul><ul><ul><li>XMLPI </li></ul></ul><ul><ul><li>XMLTEXT </li></ul></ul><ul><li>DB2 V9 offers additional new functions as well: </li></ul><ul><ul><li>XMLSERIALIZE </li></ul></ul><ul><ul><li>XMLPARSE </li></ul></ul><ul><ul><li>XMLQUERY </li></ul></ul><ul><ul><li>XMLEXISTS </li></ul></ul>
  6. 6. XML Publishing Functions Construct XML Sequences <ul><li>Use SQL/XML publishing functions together to publish relational data in XML format </li></ul><ul><li>XMLAGG is the only aggregate function (the rest, shown below, scalar functions) </li></ul><ul><ul><li>Returns an XML sequence containing an item for each non-null value in a set of XML values </li></ul></ul><ul><li>XMLATTRIBUTES constructs XML attributes from the arguments </li></ul><ul><ul><li>Only valid as an argument of XMLELEMENT </li></ul></ul><ul><li>XMLCOMMENT returns a comment node (input argument provides content) </li></ul><ul><li>XMLCONCAT returns a sequence that concatenates two or more XML input arguments </li></ul><ul><li>XMLDOCUMENT returns an XML document node with its child nodes </li></ul><ul><ul><li>A document node is required by every XML document </li></ul></ul><ul><li>XMLELEMENT returns an XML element node (does not create a document node) </li></ul><ul><li>XMLFOREST returns a sequence of XML element nodes </li></ul><ul><li>XMLNAMESPACES declaration constructs namespace declarations </li></ul><ul><ul><li>For use as an argument to XMLELEMENT, XMLFOREST, or XMLTABLE </li></ul></ul><ul><li>XMLPI returns a processing instruction node </li></ul><ul><li>XMLTEXT returns a text node (input argument provides content) </li></ul>
  7. 7. XML Publishing Functions Simple Example INSERT INTO PATIENT VALUES('12345', (SELECT XMLDOCUMENT(XMLELEMENT(NAME &quot; patient&quot;, c.firstname || ' ' || c.lastname), XMLCOMMENT('sample comment')) FROM customer c WHERE c.custno = 55331)); < patient >JIM BEAM</ patient ><!--sample comment-->
  8. 8. XML Publishing Functions Example 2 <ul><li>Construct the following document with its constant values: </li></ul><ul><ul><li><theRoot xmlns=&quot;http://xyz.com/aSchema&quot; att=&quot;123&quot;> <!-- comment line here --> <firstElement>one</firstElement> <secondElement>two</secondElement> </li></ul></ul><ul><ul><li></theRoot> </li></ul></ul><ul><ul><li>The document consists of: </li></ul></ul><ul><ul><ul><li>Three element nodes (theRoot, firstElement, secondElement) </li></ul></ul></ul><ul><ul><ul><li>A namespace declaration </li></ul></ul></ul><ul><ul><ul><li>An att attribute on theRoot </li></ul></ul></ul><ul><ul><ul><li>A comment node </li></ul></ul></ul>
  9. 9. XML Publishing Functions Example 2 SELECT XMLSERIALIZE(XMLELEMENT (NAME &quot;theRoot&quot;, XMLNAMESPACES (DEFAULT ’http://xyz.com/aSchema’), XMLATTRIBUTES (’123’ AS &quot;att&quot;), XMLCOMMENT (’ comment line here ’), XMLFOREST( ’one’ as &quot;firstElement&quot;, ’ two’ as &quot;secondElement&quot;)) AS CLOB(2K)) FROM SYSIBM.SYSDUMMY1 All wrapped in XMLSERIALIZE function call to create a displayable character string from the resulting XML
  10. 10. XML Publishing Functions Example 3 <ul><li>Construct XML document from Lastname column of Employee table </li></ul><ul><li>Format of document to be constructed: </li></ul><ul><ul><li><allEmployees xmlns=&quot;http://xyz.com/aSchema&quot;> <emp>Ashmore</emp> <emp>Harper</emp> <emp>Jones</emp> <emp>Brown</emp> <emp>Smith</emp> <emp>Baker</emp> <emp>Hunter</emp> <emp>Walker</emp> </allEmployees> </li></ul></ul><ul><li>Document contents: </li></ul><ul><ul><li>Root node containing emp element nodes </li></ul></ul><ul><ul><ul><li>An allEmployees element containing multiple emp elements </li></ul></ul></ul><ul><ul><li>A namespace declaration </li></ul></ul>Assume there is an employee table with a lastname column as a basis for this result
  11. 11. XML Publishing Functions Example 3 <ul><li>SELECT statement to construct the document and sample output: </li></ul>SELECT XMLELEMENT (NAME &quot;allEmployees&quot;, XMLNAMESPACES (DEFAULT 'http://xyz.com/aSchema'), XMLAGG(XMLELEMENT (NAME &quot;emp&quot;, e.lastname))) FROM THEMIS.EMPLOYEE e <allEmployees xmlns=&quot;http://xyz.com/aSchema&quot;><emp>Jones</emp> <emp>Smith</emp><emp>Baker</emp><emp>Wang</emp> <emp>Davis</emp><emp>Et Cetera</emp></allEmployees>
  12. 12. DB2 V9 Additional New Functions <ul><li>XMLSERIALIZE </li></ul><ul><ul><li>Converts XML values in to textual XML </li></ul></ul><ul><li>XMLPARSE </li></ul><ul><ul><li>Supports insertion of XML data by converting string expressions into values compatible with XML column </li></ul></ul><ul><li>XMLQUERY </li></ul><ul><ul><li>Extracts data from XML using XPath expressions </li></ul></ul><ul><li>XMLEXISTS </li></ul><ul><ul><li>Determines whether an XPath expression on XML data will return non-empty values </li></ul></ul>
  13. 13. XML Serialization and Parsing <ul><li>XMLSERIALIZE function converts XML from its tree format into text </li></ul><ul><ul><li>Use XMLSERIALIZE to retrieve an XML document from DB2 </li></ul></ul><ul><ul><li>XMLSERIALIZE is given an XML-expression to be converted to a string </li></ul></ul><ul><ul><li>Requires specifying generated data type (CLOB, DBCLOB, or BLOB) </li></ul></ul><ul><ul><li>Optional INCLUDING XMLDECLARATION clause follows type spec: </li></ul></ul><ul><ul><ul><li>Indicates that output will include an explicit XML declaration such as: <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> </li></ul></ul></ul><ul><ul><ul><li>The default is EXCLUDING XMLDECLARATION </li></ul></ul></ul><ul><li>XMLPARSE supports the reverse functionality </li></ul><ul><ul><li>Use XMLPARSE to store XML data in a table’s XML column </li></ul></ul>
  14. 14. XMLSERIALIZE Example <ul><li>Serialize XML value returned by XMLELEMENT function into a CLOB </li></ul><ul><ul><li>Resulting column contains a single XML element with EmpName as the element name and full employee name as element content </li></ul></ul><ul><ul><li>SELECT EMPNO, XMLSERIALIZE(XMLELEMENT( </li></ul></ul><ul><ul><li>NAME &quot;EmpName&quot;, firstnme || ' ' || lastname) </li></ul></ul><ul><ul><li>AS CLOB(80) EXCLUDING XMLDECLARATION) </li></ul></ul><ul><ul><li>FROM DSN8910.EMP </li></ul></ul><ul><ul><li>000010 <EmpName>CHRISTINE HAAS</EmpName> </li></ul></ul><ul><ul><li>000020 <EmpName>MICHAEL THOMPSON</EmpName> </li></ul></ul><ul><ul><li>000030 <EmpName>SALLY KWAN</EmpName> </li></ul></ul><ul><ul><li>000050 <EmpName>JOHN GEYER</EmpName> </li></ul></ul><ul><ul><li>000060 <EmpName>IRVING STERN</EmpName> </li></ul></ul><ul><ul><li>000070 <EmpName>EVA PULASKI</EmpName> </li></ul></ul><ul><ul><li>000090 <EmpName>EILEEN HENDERSON</EmpName> </li></ul></ul><ul><ul><li>000100 <EmpName>THEODORE SPENSER</EmpName> </li></ul></ul><ul><ul><li>000110 <EmpName>VINCENZO LUCCHESI</EmpName> </li></ul></ul>Partial result
  15. 15. <ul><li>XMLPARSE function: insert XML documents into an XML column </li></ul><ul><ul><li>Provide a string expression or XML host variable as input </li></ul></ul><ul><li>XMLPARSE is not required to insert XML document into XML column </li></ul><ul><ul><li>A regular INSERT... VALUES statement also works </li></ul></ul><ul><li>The XMLPARSE advantage is the ability to specify what to do with whitespace that appears between elements (boundary whitespace) </li></ul><ul><ul><li>STRIP WHITESPACE is the default </li></ul></ul><ul><ul><ul><li>For regular INSERT (without XMLPARSE) </li></ul></ul></ul><ul><ul><ul><li>And for INSERT with XMLPARSE </li></ul></ul></ul><ul><ul><li>PRESERVE WHITESPACE is the alternative </li></ul></ul><ul><li>INSERT INTO PATIENT VALUES (98789, XMLPARSE ( DOCUMENT xml-string-or-host-variable PRESERVE WHITESPACE )) </li></ul>XMLPARSE
  16. 16. XMLQUERY <ul><li>To retrieve portions of XML documents, use XMLQUERY function </li></ul><ul><ul><li>Accepts an XPath expression from within an SQL context </li></ul></ul><ul><li>XMLQUERY supports: </li></ul><ul><ul><li>Retrieving parts of XML documents instead of entire XML documents </li></ul></ul><ul><ul><li>Having XML data participate in SQL queries </li></ul></ul><ul><ul><li>Operating on both relational and XML data in a single SQL statement </li></ul></ul><ul><ul><li>Application of further SQL processing on returned XML values </li></ul></ul><ul><ul><ul><li>E.g.: ordering results with the ORDER BY clause </li></ul></ul></ul><ul><ul><ul><ul><li>Requires XMLCAST to cast the results to a non-XML type </li></ul></ul></ul></ul><ul><li>XMLQUERY returns an XML sequence </li></ul><ul><ul><li>Sequence can contain one or more items or be empty </li></ul></ul>
  17. 17. Sample Data for XMLQUERY Examples <ul><li>Assume an XML column containing these two XML documents: </li></ul><ul><ul><li><patient id=&quot;55555&quot;> </li></ul></ul><ul><ul><li><name>Jim Beam</name> </li></ul></ul><ul><ul><li><addr> </li></ul></ul><ul><ul><li><street>789 Pine</street> </li></ul></ul><ul><ul><li><city>Uptown</city> </li></ul></ul><ul><ul><li><state>NJ</state> </li></ul></ul><ul><ul><li><zip>07733</zip> </li></ul></ul><ul><ul><li></addr> </li></ul></ul><ul><ul><li><phone type=&quot;home&quot;>908-554-5454</phone> </li></ul></ul><ul><ul><li><phone type=&quot;work&quot;>908-445-4545</phone> </li></ul></ul><ul><ul><li><phone type=&quot;fax&quot;>908-332-2424</phone> </li></ul></ul><ul><ul><li><email>jbeam@gmail.com</email> </li></ul></ul><ul><ul><li><service> </li></ul></ul><ul><ul><li><sdate>2007-10-31</sdate> </li></ul></ul><ul><ul><li><reason>short of breath</reason> </li></ul></ul><ul><ul><li><descrip>trick or treatment</descrip> </li></ul></ul><ul><ul><li><cost>45.00</cost> </li></ul></ul><ul><ul><li><copay>5.00</copay> </li></ul></ul><ul><ul><li></service> </li></ul></ul><ul><ul><li></patient> </li></ul></ul><ul><ul><li><patient id=&quot;11123&quot;> </li></ul></ul><ul><li><name>Sara Lee</name> </li></ul><ul><li><addr> </li></ul><ul><li><street>33 Maple</street> </li></ul><ul><li><city>Nearly</city> </li></ul><ul><li><state>NJ</state> </li></ul><ul><li><zip>07123</zip> </li></ul><ul><li></addr> </li></ul><ul><li><phone type=&quot;home&quot;>908-842-7531</phone> </li></ul><ul><li><email>saralee@cakemail.com</email> </li></ul><ul><li><service> </li></ul><ul><li><sdate>2004-02-29</sdate> </li></ul><ul><li><reason>brittle nails</reason> </li></ul><ul><li><descrip>prescribe hormones</descrip> </li></ul><ul><li><cost>84.00</cost> </li></ul><ul><li><copay>15.00</copay> </li></ul><ul><li></service> </li></ul><ul><ul><li></patient> </li></ul></ul>
  18. 18. XMLQUERY Example 1 <ul><li>XMLQUERY statement embedded within a SELECT statement requests all XML documents that are stored in PATIENT_XML column of PATIENT table </li></ul><ul><ul><li>SELECT XMLQUERY('/' passing PATIENT_XML ) FROM PATIENT; </li></ul></ul><ul><ul><li><?xml version=&quot;1.0&quot; encoding=&quot;IBM037&quot;?><patient id=&quot;55555&quot;><name>Jim Beam</name><addr><street>789 Pine</ </li></ul></ul><ul><ul><li>DSNT400I SQLCODE = 000, SUCCESSFUL EXECUTION </li></ul></ul><ul><ul><li>DSNT418I SQLSTATE = 01004 SQLSTATE RETURN CODE </li></ul></ul><ul><ul><li>DSNT415I SQLERRP = DSN SQL PROCEDURE DETECTING ERROR </li></ul></ul><ul><ul><li>DSNT416I SQLERRD = 0 0 0 -1 0 0 SQL DIAGNOSTIC INFORMATION </li></ul></ul><ul><ul><li>DSNT416I SQLERRD = X'00000000' X'00000000' X'00000000' X'FFFFFFFF' </li></ul></ul><ul><ul><li>X'00000000' X'00000000' SQL DIAGNOSTIC INFORMATION </li></ul></ul><ul><ul><li>DSNT417I SQLWARN0-5 = W,W,,,, SQL WARNINGS </li></ul></ul><ul><ul><li>DSNT417I SQLWARN6-A = ,,,, SQL WARNINGS </li></ul></ul><ul><ul><li><?xml version=&quot;1.0&quot; encoding=&quot;IBM037&quot;?><patient id=&quot;11123&quot;><name>Sara Lee</name><addr><street>33 Maple</ </li></ul></ul><ul><ul><li>DSNT400I SQLCODE = 000, SUCCESSFUL EXECUTION </li></ul></ul><ul><ul><li>DSNT418I SQLSTATE = 01004 SQLSTATE RETURN CODE </li></ul></ul><ul><ul><li>DSNT415I SQLERRP = DSN SQL PROCEDURE DETECTING ERROR </li></ul></ul><ul><ul><li>DSNT416I SQLERRD = 0 0 0 -1 0 0 SQL DIAGNOSTIC INFORMATION </li></ul></ul><ul><ul><li>DSNT416I SQLERRD = X'00000000' X'00000000' X'00000000' X'FFFFFFFF‘ </li></ul></ul><ul><ul><li>X'00000000' X'00000000' SQL DIAGNOSTIC INFORMATION </li></ul></ul><ul><ul><li>DSNT417I SQLWARN0-5 = W,W,,,, SQL WARNINGS </li></ul></ul><ul><ul><li>DSNT417I SQLWARN6-A = ,,,, SQL WARNINGS </li></ul></ul>Important: Always surround the XPath expression with apostrophes
  19. 19. XMLQUERY Example 2 <ul><li>The previous result did not display all XML documents because DSNTEP2 and SPUFI truncate results after 100 bytes of output since XML does not have a specific length </li></ul><ul><li>Work around this problem by nesting XMLQUERY in an XMLSERIALIZE function </li></ul><ul><ul><li>SELECT XMLSERIALIZE( XMLQUERY('/' passing PATIENT_XML) AS CLOB(2K)) </li></ul></ul><ul><ul><li>FROM PATIENT; </li></ul></ul><ul><ul><ul><li><patient id=&quot;55555&quot;><name>Jim Beam</name><addr><street>789 Pine </street><city>Uptown</city><state>NJ</state><zip>07733</zip> </addr><phone type=&quot;home&quot;>908-554-5454</phone><phone type=&quot;work&quot;> 908-445-4545</phone><phone type=&quot;fax&quot;>908-332-2424</phone><email> jbeam@gmail.com</email><service><sdate>2007-10-31</sdate><reason> short of breath</reason><descrip>trick or treatment</descrip> <cost>45.00</cost><copay>5.00</copay></service></patient> </li></ul></ul></ul><ul><ul><ul><li><patient id=&quot;11123&quot;><name>Sara Lee</name><addr><street>33 Maple </street><city>Nearly</city><state>NJ</state><zip>07123</zip> </addr><phone type=&quot;home&quot;>908-842-7531</phone><email> saralee@cakemail.com</email><service><sdate>2004-02-29</sdate> <reason>brittle nails</reason><descrip>prescribed hormones</descrip> <cost>84.00</cost><copay>15.00</copay></service></patient> </li></ul></ul></ul>Each XML document is actually displayed as one long row Wraparound applied to make complete information visible
  20. 20. XMLQUERY Example 3 <ul><li>Return all phone numbers of all patients </li></ul><ul><ul><li>SELECT XMLSERIALIZE(XMLQUERY('/patient/phone' </li></ul></ul><ul><ul><li>passing PATIENT_XML) AS CLOB(2K)) </li></ul></ul><ul><ul><li>FROM PATIENT; </li></ul></ul><ul><ul><li><phone type=&quot;home&quot;>908-554-5454</phone> </li></ul></ul><ul><ul><li><phone type=&quot;work&quot;>908-445-4545</phone> </li></ul></ul><ul><ul><li><phone type=&quot;fax&quot;>908-332-2424</phone> </li></ul></ul><ul><ul><li><phone type=&quot;home&quot;>908-842-7531</phone> </li></ul></ul><ul><li>Actually, only two rows result – the first one containing three different phone numbers from the first XML document </li></ul><ul><li>When XMLQUERY returns a sequence that contains multiple elements, the elements are concatenated into a single string during the serialization process </li></ul><ul><ul><li>The resulting row is not necessarily a well-formed document </li></ul></ul><ul><ul><li>Applications that receive this result must handle this properly </li></ul></ul>First three phone numbers all belong to Jim Beam
  21. 21. XMLQUERY Example 4 <ul><li>XPath expression may return an empty sequence </li></ul><ul><ul><li>XMLQUERY will also return an empty sequence </li></ul></ul><ul><li>Return all XML documents that have a phone type attribute of “fax” </li></ul><ul><ul><li>SELECT XMLSERIALIZE(XMLQUERY('/patient/phone [@type=&quot;fax&quot;]/.. ' </li></ul></ul><ul><ul><li>passing PATIENT_XML) AS CLOB(2K)) FROM PATIENT; </li></ul></ul><ul><ul><ul><li><patient id=&quot;55555&quot;><name>Jim Beam</name><addr><street>789 Pine </street><city>Uptown</city><state>NJ</state><zip>07733</zip> </addr><phone type=&quot;home&quot;>908-554-5454</phone><phone type=&quot;work&quot;> 908-445-4545</phone><phone type=&quot;fax&quot;>908-332-2424</phone><email> jbeam@gmail.com</email><service><sdate>2007-10-31</sdate><reason> short of breath</reason><descrip>trick or treatment</descrip> <cost>45.00</cost><copay>5.00</copay></service></patient> </li></ul></ul></ul><ul><li>The result is one empty (not shown) and one non-empty sequence </li></ul><ul><li>Eliminate the empty sequences using the XMLEXISTS predicate (covered next) </li></ul>
  22. 22. XMLQUERY Example 5 <ul><li>If XML documents contain namespace definitions, then the XMLQUERY statement must reference them also </li></ul><ul><li>The two XML documents in the table now have namespaces </li></ul><ul><li>Modified SELECT statement to accommodate the namespace </li></ul><ul><ul><li>SELECT XMLSERIALIZE(XMLQUERY(' </li></ul></ul><ul><ul><li>declare default element namespace &quot;http://xyz.com/aSchema&quot; ; </li></ul></ul><ul><ul><li>/patient' passing PATIENT_XML ) AS CLOB(2K)) FROM PATIENT; </li></ul></ul><ul><ul><li><patient xmlns=&quot;http://xyz.com/aSchema&quot; </li></ul></ul><ul><ul><li>id=&quot;55555&quot;> </li></ul></ul><ul><ul><li><name>Jim Beam</name> </li></ul></ul><ul><ul><li><addr> </li></ul></ul><ul><ul><li><street>789 Pine</street> </li></ul></ul><ul><ul><li><city>Uptown</city> </li></ul></ul><ul><ul><li><state>NJ</state> </li></ul></ul><ul><ul><li><zip>07733</zip> </li></ul></ul><ul><ul><li></addr> </li></ul></ul><ul><ul><li><phone type=&quot;home&quot;>908-554-5454</phone> </li></ul></ul><ul><ul><li><phone type=&quot;work&quot;>908-445-4545</phone> </li></ul></ul><ul><ul><li><phone type=&quot;fax&quot;>908-332-2424</phone> </li></ul></ul><ul><ul><li><email>jbeam@gmail.com</email> </li></ul></ul><ul><ul><li><service> </li></ul></ul><ul><ul><li><sdate>2007-10-31</sdate> </li></ul></ul><ul><ul><li><reason>short of breath</reason> </li></ul></ul><ul><ul><li><descrip>trick or treatment</descrip> </li></ul></ul><ul><ul><li><cost>45.00</cost> </li></ul></ul><ul><ul><li><copay>5.00</copay> </li></ul></ul><ul><ul><li></service> </li></ul></ul><ul><ul><li></patient> </li></ul></ul><ul><ul><li><patient xmlns=&quot;http://xyz.com/aSchema&quot; </li></ul></ul><ul><ul><li>id=&quot;11123&quot;> </li></ul></ul><ul><li><name>Sara Lee</name> </li></ul><ul><li><addr> </li></ul><ul><li><street>33 Maple</street> </li></ul><ul><li><city>Nearly</city> </li></ul><ul><li><state>NJ</state> </li></ul><ul><li><zip>07123</zip> </li></ul><ul><li></addr> </li></ul><ul><li><phone type=&quot;home&quot;>908-842-7531</phone> </li></ul><ul><li><email>saralee@cakemail.com</email> </li></ul><ul><li><service> </li></ul><ul><li><sdate>2004-02-29</sdate> </li></ul><ul><li><reason>brittle nails</reason> </li></ul><ul><li><descrip>prescribe hormones</descrip> </li></ul><ul><li><cost>84.00</cost> </li></ul><ul><li><copay>15.00</copay> </li></ul><ul><li></service> </li></ul><ul><ul><li></patient> </li></ul></ul>
  23. 23. XMLEXISTS <ul><li>Use XMLEXISTS predicate to restrict set of rows returned by query </li></ul><ul><ul><li>Code this restriction based on the values in XML columns </li></ul></ul><ul><li>XMLEXISTS predicate specifies an XPath expression </li></ul><ul><ul><li>XMLEXISTS predicate returns false if its XPath expression returns an empty sequence </li></ul></ul><ul><ul><li>Otherwise, it returns true and the rows are returned </li></ul></ul><ul><li>Avoid receiving an empty sequence by using XMLEXISTS: </li></ul><ul><ul><ul><li>SELECT XMLSERIALIZE(XMLQUERY('/patient/phone' </li></ul></ul></ul><ul><ul><ul><li>passing PATIENT_XML) AS CLOB(2K)) FROM PATIENT </li></ul></ul></ul><ul><ul><ul><li>WHERE XMLEXISTS(' /patient/phone[@type=&quot;work&quot;]' </li></ul></ul></ul><ul><ul><ul><li>passing PATIENT_XML ) ; </li></ul></ul></ul>
  24. 24. The XMLTABLE Function Creating Tabular Output From XML Data
  25. 25. XMLTABLE Function <ul><li>XMLTABLE function returns a table from an XML column </li></ul><ul><ul><li>Row-defining XPath expression specifies result table rows </li></ul></ul><ul><ul><li>Column-defining XPath expressions specify row contents </li></ul></ul><ul><ul><li>The returned table can have any types of columns </li></ul></ul><ul><li>Result table structure defined by COLUMNS clause </li></ul><ul><ul><li>Includes column name, data type, and source of column value </li></ul></ul><ul><ul><ul><li>PATH clause used to specify the source of a result table column </li></ul></ul></ul><ul><ul><ul><ul><li>An XPath expression is used for this </li></ul></ul></ul></ul><ul><li>XMLNAMESPACES function may be used </li></ul><ul><ul><li>Applies to all of XMLTABLE’s XPath expressions </li></ul></ul>
  26. 26. XMLTABLE Uses and Examples <ul><li>Results from XPath expression may be easier to process in a table </li></ul><ul><li>A table supports various operations via SQL: </li></ul><ul><ul><li>Iteration over results of XPath expression in SELECT: </li></ul></ul><ul><ul><li>Sorting on values from an XML document: </li></ul></ul><ul><ul><li>Storing some XML values as XML and others as relational data </li></ul></ul><ul><ul><li>Using stored XML documents to supply values for insertion into tables </li></ul></ul><ul><ul><ul><li>A simple form of decomposition (next page) </li></ul></ul></ul>SELECT T.* FROM PATIENT P, XMLTABLE (XMLNAMESPACES(DEFAULT 'http://xyz.com/aSchema'), '/patient' PASSING P.PATIENT_XML COLUMNS &quot;PATNAME&quot; VARCHAR(30) PATH 'name', &quot;LOCATION&quot; VARCHAR(30) PATH ‘addr/city’) T SELECT T.* FROM PATIENT P, XMLTABLE (XMLNAMESPACES(DEFAULT 'http://xyz.com/aSchema'), '/patient' PASSING P.PATIENT_XML COLUMNS &quot;PATNAME&quot; VARCHAR(30) PATH 'name', &quot;addr/city&quot; VARCHAR(30)) T ORDER BY T.PATNAME PATNAME LOCATION ---------------- ---------- Jane Zoe Anytown Bob Bee Uptown PATNAME addr/city ---------------- ---------- Bob Bee Uptown Jane Zoe Anytown
  27. 27. Inserting Values Returned From XMLTABLE <ul><li>Use XMLTABLE to retrieve XML document values stored in the PATIENT table and insert them into the PATIENTSERVICE table </li></ul><ul><li>Definition of PATIENTSERVICE table into which values from these documents will be inserted: </li></ul><ul><li>INSERT statement using XMLTABLE to populate PATIENTSERVICE: </li></ul>CREATE TABLE PATIENTSERVICE (PATIENTNAME VARCHAR(30) NOT NULL, SERVDATE DATE NOT NULL, REASON VARCHAR(30) NOT NULL, DESCRIPTION VARCHAR(30) NOT NULL, COST DECIMAL(7,2) NOT NULL, COPAY DECIMAL(7,2) NOT NULL ) IN XMLDB.XML3TS; PATIENTNAME SERVDATE REASON DESCRIPTION COST COPAY ------------ ---------- ----------- -------------- ----- ----- Bob Bee 2008-05-15 sweet tooth remove it 90.00 10.00 Jane Zoe 2008-02-22 flew shot gave flew shot 41.00 12.00 PATIENTSERVICE after these inserts INSERT INTO PATIENTSERVICE SELECT T.* FROM PATIENT P, XMLTABLE (XMLNAMESPACES(DEFAULT 'http://xyz.com/aSchema'), '/patient' PASSING P.PATIENT_XML COLUMNS &quot;PATIENTNAME&quot; VARCHAR(30) PATH 'name', &quot;SERVDATE&quot; DATE PATH 'service/sdate', &quot;REASON&quot; VARCHAR(30) PATH 'service/reason', &quot;DESCRIPTION&quot; VARCHAR(30) PATH 'service/descrip', &quot;COST&quot; DECIMAL(7,2) PATH 'service/cost', &quot;COPAY&quot; DECIMAL(7,2) PATH 'service/copay' ) as T
  28. 28. Using XMLTABLE to Create Views <ul><li>XMLTABLE function supports creation of views from data in an XML column </li></ul><ul><li>View may include relational column data combined with XML document data </li></ul><ul><li>Example: create the view patient_view with three columns </li></ul><ul><ul><li>The name and email values are taken from the XML document </li></ul></ul><ul><ul><li>CREATE VIEW patient_view AS SELECT patientid, p.name, p.email FROM patient, xmltable('$px/patient' passing patient_xml as &quot;px&quot; columns name char(20) path 'name', email char(20) path 'email') as p; </li></ul></ul><ul><ul><li>SELECT * FROM patient_view; </li></ul></ul><ul><ul><li>PATIENTID NAME EMAIL </li></ul></ul><ul><ul><li>--------- ------------ -------------------- </li></ul></ul><ul><ul><li>13579 John Doe jdoe@mymail.com </li></ul></ul><ul><ul><li>24680 Faye Rae frae@mailone.com </li></ul></ul><ul><ul><li>55555 Jim Beam jbeam@gmail.com </li></ul></ul><ul><ul><li>11123 Sara Lee saralee@cakemail.com </li></ul></ul>
  29. 29. Programming Language Support for DB2 XML
  30. 30. Programming Language Support for DB2 XML <ul><li>Programming languages that support DB2 XML: </li></ul><ul><ul><li>C or C++ (in embedded SQL or DB2 ODBC applications) </li></ul></ul><ul><ul><li>COBOL </li></ul></ul><ul><ul><li>Java (JDBC or SQLJ) </li></ul></ul><ul><ul><li>Assembler </li></ul></ul><ul><ul><li>PL/I </li></ul></ul><ul><li>Entire document or a document fragment (sequence) can be retrieved an from an XML column </li></ul><ul><ul><li>Only an entire document can be placed into an XML column </li></ul></ul><ul><li>Application variable can receive an entire retrieved XML document </li></ul><ul><ul><li>XMLQUERY function containing an XPath expression is used to retrieve an XML sequence </li></ul></ul><ul><ul><ul><li>Use in an SQL FETCH or single-row SELECT INTO </li></ul></ul></ul>
  31. 31. XML and JDBC <ul><li>JDBC applications can store and retrieve XML data </li></ul><ul><li>XML data in applications is in the serialized string format </li></ul><ul><li>JDBC applications can: </li></ul><ul><ul><li>Use setXXX methods to store entire XML document in XML column </li></ul></ul><ul><ul><li>Use getXXX methods to retrieve entire XML document from XML column </li></ul></ul><ul><ul><li>Use XMLQUERY to retrieve a sequence (portion of) an XML column </li></ul></ul><ul><ul><ul><li>Then retrieve the data into an application variable using getXXX methods </li></ul></ul></ul><ul><li>Serialized string data is returned when XML column data is retrieved </li></ul><ul><ul><li>Whether entire XML column contents or a sequence is returned </li></ul></ul>
  32. 32. Retrieve Data From XML Column Into String Variable public void processRecordSetUsingStrings(Connection conn, String id) { PreparedStatement pStmt = null; String sql = null; String doc = null; ResultSet rs = null; try { sql = &quot;SELECT PATIENT_XML FROM PATIENT WHERE PATIENTID = ?&quot;; pStmt = conn.prepareStatement(sql); pStmt.setString(1, id); rs = pStmt.executeQuery(); while (rs.next()) { doc = rs.getString(1); System.out.println(&quot;Document contents: &quot; + doc); } } catch (Exception e) { // handling of exceptions not shown } }
  33. 33. Putting XML Data into the Database <ul><li>For both insert and update: </li></ul><ul><ul><li>XML document must be well-formed </li></ul></ul><ul><ul><li>Application data type can be XML, character, or binary </li></ul></ul><ul><ul><li>In the app, XML data is a serialized string </li></ul></ul><ul><ul><ul><li>Must be converted to XML format when inserted into an XML column </li></ul></ul></ul><ul><ul><ul><li>Invoke XMLPARSE if application data type is not XML </li></ul></ul></ul>INSERT INTO PATIENT (PATIENTID, PATIENT_XML) VALUES('91919', '<patient id=&quot;91919&quot;> <name>Jane Zoe</name> <addr> <street>456 Main</street> <city>Anytown</city> <state>PA</state> </addr> <phone type=&quot;work&quot;>610-987-4321</phone> <email>jzoe@mymail.com</email> <service> <sdate>2008-02-22</sdate> <reason>flu shot</reason> <descrip>gave flu shot</descrip> <cost>41.00</cost> <copay>12.00</copay> </service> </patient>' );
  34. 34. Insertion Into XML Columns <ul><li>In a JDBC application, read XML data from file patient.xml as binary data, and insert it into an XML column: </li></ul><patient id=&quot;91919&quot;> <name>Jane Zoe</name> <addr> <street>456 Main</street> <city>Anytown</city> <state>PA</state> </addr> <phone type=&quot;work&quot;>610-987-4321</phone> <email>jzoe@mymail.com</email> <service> <sdate>2008-02-22</sdate> <reason>flu shot</reason> <descrip>gave flu shot</descrip> <cost>41.00</cost> <copay>12.00</copay> </service> </patient> PreparedStatement pStmt = null; String sql = null; String patid = &quot;91919&quot;; sql = &quot;INSERT INTO Patient &quot; + &quot;(PATIENTID, PATIENT_XML) &quot; + &quot;VALUES (?, ?) &quot; ; pStmt = conn.prepareStatement(sql); pStmt.setString(1, patid); File file = new File(&quot;patient.xml&quot;); pStmt.setBinaryStream(2, new FileInputStream(file), (int)file.length()); pStmt.executeUpdate(); patient.xml
  35. 35. Updates Of XML Columns <ul><li>Supported by SQL UPDATE statement </li></ul><ul><li>XML column values may be used to specify which rows are to be updated (or deleted or selected) </li></ul><ul><ul><li>Use XPath expressions to find values within XML documents </li></ul></ul><ul><li>The XMLEXISTS predicate supports XPath expressions </li></ul><ul><ul><li>Determine whether an empty sequence results from expression </li></ul></ul><ul><ul><li>If it does not, rows will be updated </li></ul></ul>
  36. 36. Updates of XML Columns <ul><li>In a JDBC application, read XML data from file patien2.xml as binary data, and use it to update data in the XML column: </li></ul><patient id=&quot;80808&quot; xmlns=&quot;http://xyz.com/aSchema&quot;> <name>Bob Bee</name> <addr> <street>789 Oak</street> <city>Uptown</city> <state>PA</state> </addr> <phone type=&quot;work&quot;>610-987-5556</phone> <email>bbee@mymail.com</email> <service> <sdate>2008-05-15</sdate> <reason>sweet tooth</reason> <descrip>remove it</descrip> <cost>90.00</cost> <copay>10.00</copay> </service> </patient> PreparedStatement pStmt = null; String sql = null; String patid = &quot;80808&quot;; sql = &quot;UPDATE Patient &quot; + &quot;SET PATIENT_XML = ? &quot; + &quot;WHERE PATIENTID = ? &quot; ; pStmt = conn.prepareStatement(sql); pStmt.setString(2, patid); File file = new File(&quot;patien2.xml&quot;); pStmt.setBinaryStream(1, new FileInputStream(file), (int)file.length()); pStmt.executeUpdate(); patien2.xml
  37. 37. Deletion Of XML Data From Tables <ul><li>Delete rows from table PATIENT with a PATIENTID value of 91919 </li></ul><ul><ul><li>DELETE FROM PATIENT WHERE PATIENTID=91919; </li></ul></ul><ul><li>Delete rows from PATIENT with a city element value of &quot;Uptown&quot; </li></ul><ul><li>DELETE FROM PATIENT WHERE XMLEXISTS (' /patient/addr[city=&quot;Uptown&quot;]' passing PATIENT_XML); </li></ul><ul><li>Delete the XML document in the row of the PATIENT table with a city element value of &quot;Uptown&quot;, but leave the row </li></ul><ul><li>UPDATE PATIENT SET PATIENT_XML = NULL WHERE XMLEXISTS ( '/patient/addr[city=&quot;Uptown&quot;]' passing PATIENT_XML); </li></ul>
  38. 38. Embedded SQL With XML <ul><li>Supported languages: assembler, C, C++, COBOL, or PL/I </li></ul><ul><li>Supported operations: </li></ul><ul><ul><li>Store entire XML document in XML column using INSERT or UPDATE </li></ul></ul><ul><ul><li>Retrieve entire XML document from an XML column using SELECT </li></ul></ul><ul><ul><li>Retrieve a sequence from a document in an XML column </li></ul></ul><ul><ul><ul><li>Use XMLQUERY to generate serialized XML string </li></ul></ul></ul><ul><ul><ul><li>Then SELECT or FETCH it into an application variable </li></ul></ul></ul>01 XML-BLOB USAGE IS SQL TYPE IS XML AS BLOB(2M). EXEC SQL DECLARE :XML-BLOB VARIABLE CCSID UNICODE ; <ul><li>XML host variable types offered by DB2: </li></ul><ul><ul><li>XML AS BLOB </li></ul></ul><ul><ul><li>XML AS CLOB </li></ul></ul><ul><ul><li>XML AS DBCLOB </li></ul></ul><ul><ul><li>XML AS BLOB_FILE </li></ul></ul><ul><ul><li>XML AS CLOB_FILE </li></ul></ul><ul><ul><li>XML AS DBCLOB_FILE </li></ul></ul>
  39. 39. COBOL Retrieval Example ********************************************************************** * Host variable declarations * ********************************************************************** 01 XMLDATA USAGE IS SQL TYPE IS XML AS CLOB(4K). 01 XMLBLOB USAGE IS SQL TYPE IS XML AS BLOB(4K). 01 CLOBNOTXMLTYPE USAGE IS SQL TYPE IS CLOB(4K). ********************************************************************** * Retrieve data from XML column into XML AS CLOB host variable * ********************************************************************** EXEC SQL SELECT PATIENT_XML INTO :XMLDATA FROM PATIENT WHERE PATIENTID = 12345. ********************************************************************** * Retrieve data from XML column into XML AS BLOB host variable * ********************************************************************** EXEC SQL SELECT PATIENT_XML INTO :XMLBLOB FROM PATIENT WHERE PATIENTID = 12345. ********************************************************************** * Retrieve data from XML column into CLOB variable. Use XMLSERIALIZE * * function to convert the data from the XML type to the CLOB type * ********************************************************************** EXEC SQL SELECT XMLSERIALIZE(PATIENT_XML AS CLOB(4K)) INTO :CLOBNOTXMLTYPE FROM PATIENT WHERE PATIENTID = 12345.
  40. 40. COBOL Update Example ****************************************************************** * Host variable declarations * ****************************************************************** 01 XMLDATA USAGE IS SQL TYPE IS XML AS CLOB(4K). 01 XMLBLOB USAGE IS SQL TYPE IS XML AS BLOB(4K). 01 CLOBNOTXMLTYPE USAGE IS SQL TYPE IS CLOB(4K). ****************************************************************** * Update XML column using data in an XML AS CLOB host variable * ****************************************************************** EXEC SQL UPDATE PATIENT SET PATIENT_XML = :XMLDATA WHERE PATIENTID = 12345. ****************************************************************** * Update XML column using data in an XML AS BLOB host variable * ****************************************************************** EXEC SQL UPDATE PATIENT SET PATIENT_XML = :XMLBLOB WHERE PATIENTID = 12345. ****************************************************************** * Update XML column using data in a CLOB host variable. * * Use XMLPARSE function to convert data to XML type. * ****************************************************************** EXEC SQL UPDATE PATIENT SET PATIENT_XML = XMLPARSE(DOCUMENT :CLOBNOTXMLTYPE) WHERE PATIENTID = 12345.
  41. 41. LOAD Utility Supports Loading XML Data <ul><li>Similar to loading other types of data, except: </li></ul><ul><ul><li>For delimited input, XML fields follow LOAD utility delimited format </li></ul></ul><ul><ul><ul><li>Otherwise, XML fields are specified like VARCHAR input </li></ul></ul></ul><ul><ul><ul><ul><li>Field length specified in a 2-byte binary field preceding the data </li></ul></ul></ul></ul><ul><ul><li>LOAD statement uses keyword XML for all XML type input fields </li></ul></ul><ul><ul><ul><li>Keywords PRESERVE WHITESPACE if desired (stripped by default) </li></ul></ul></ul>LOAD DATA INTO TABLE PRODUCTS (PATIENTID POSITION (1) CHAR(6), PATIENT_XML POSITION (8) XML PRESERVE WHITESPACE)
  42. 42. UNLOAD Utility Supports Unloading XML Data <ul><li>In the unloaded file fragment shown here, the XML document has been converted to the EBCDIC 37 encoding scheme </li></ul><ul><li>Specify UNICODE in the UNLOAD statement and use Unicode delimiter characters to maximize portability </li></ul><ul><ul><li>UNLOAD DATA FROM TABLE PATIENT UNICODE </li></ul></ul>****** ****************************** Top of Data ************************* 000001 12345 <?xml version=&quot;1.0&quot; encoding=&quot;IBM037&quot;?><patient id=&quot;12345&quot;><na 000002 43210 <?xml version=&quot;1.0&quot; encoding=&quot;IBM037&quot;?><patient id=&quot;43210&quot;><na 000003 51234 <?xml version=&quot;1.0&quot; encoding=&quot;IBM037&quot;?><patient id=&quot;51234&quot;><na
  43. 43. <ul><li>New XML Data Type in DB2 </li></ul><ul><li>New DB2 Functions to Support It </li></ul><ul><li>Index Support (XMLEXISTS) </li></ul><ul><li>XPath Expression Language </li></ul><ul><li>Programming Support </li></ul><ul><ul><li>Languages </li></ul></ul><ul><ul><li>APIs </li></ul></ul><ul><li>Insert and Update Whole Doc </li></ul><ul><li>Retrieve All or Portion of Doc </li></ul>Review

×