Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. Gregg Lippa Themis Inc. [email_address] DB2 Version 9: Overview of pureXML Plus an XPath Primer, XML Column Indexes, Support for Validation
  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 </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. DB2 Version 9 <ul><li>pureXML technology </li></ul><ul><ul><li>Seamlessly integrating XML with relational data </li></ul></ul><ul><ul><ul><li>Including publishing and parsing functions </li></ul></ul></ul><ul><ul><li>Storing XML data natively in a database table </li></ul></ul><ul><ul><ul><li>Including a new XML data type </li></ul></ul></ul>
  4. 4. An XML Document <patient id=&quot;13579&quot;> <name>John Doe</name> <addr> <street>123 Main</street> <city>Anytown</city> <state>PA</state> <zip>19134</zip> </addr> <phone type=&quot;home&quot;>610-654-1234</phone> <phone type=&quot;work&quot;>610-987-4321</phone> <email></email> <service> <sdate>2008-09-22</sdate> <reason>flu shot</reason> <descrip>gave flu shot</descrip> <cost>40.00</cost> <copay>10.00</copay> </service> <service> <sdate>2008-05-11</sdate> <reason>sore foot</reason> <descrip>referred to specialist</descrip> <cost>60.00</cost> <copay>10.00</copay> </service> </patient> Nodes Well-formed Elements Attributes Values (text) Valid Root
  5. 5. XML – Why? <ul><li>Pervasive </li></ul><ul><li>Versatile </li></ul><ul><li>Self-describing </li></ul><ul><li>Neutral for exchanging data among diverse devices </li></ul><ul><ul><li>Universal standard for data interchange </li></ul></ul><ul><li>Growth of XML data </li></ul><ul><ul><li>XML-based industry and data standards </li></ul></ul><ul><ul><li>SOA and Web services; Services-based frameworks </li></ul></ul><ul><ul><ul><li>messages are encapsulated as XML </li></ul></ul></ul><ul><ul><li>Web 2.0 technologies </li></ul></ul><ul><ul><ul><li>XML feeds </li></ul></ul></ul><ul><ul><ul><li>Syndication services - rendered as XML files </li></ul></ul></ul><ul><li>XML data becoming more critical to enterprise operations </li></ul>
  6. 6. Previous Approaches to XML Document Storage <ul><li>File systems </li></ul><ul><ul><li>Not storing XML documents in a database </li></ul></ul><ul><ul><li>Does not scale well </li></ul></ul><ul><li>Stuffing </li></ul><ul><ul><li>XML data stored as large objects or VARCHAR </li></ul></ul><ul><ul><li>Inefficient for querying the XML data </li></ul></ul><ul><li>Shredding </li></ul><ul><ul><li>Decomposing XML data into multiple columns & tables </li></ul></ul><ul><ul><li>Often leads to complex join requirements </li></ul></ul><ul><ul><li>May be difficult to recreate original XML document </li></ul></ul><ul><li>Utilizing XML-only database systems </li></ul><ul><ul><li>Few options and little expertise available </li></ul></ul>
  7. 7. XML vs Relational Model <ul><li>Major differences: </li></ul><ul><ul><li>XML data is hierarchical ; relational data is tabular </li></ul></ul><ul><ul><li>XML data is self-describing ; relational data is not </li></ul></ul><ul><ul><li>XML data is ordered ; relational data is not </li></ul></ul><ul><li>Which approach is right for my data? </li></ul><ul><ul><li>XML maximizes flexibility of the data structure </li></ul></ul><ul><ul><li>Relational data provides performance benefits for data retrieval </li></ul></ul><ul><ul><li>Relational data supports referential integrity requirements </li></ul></ul><ul><ul><ul><li>Referential constraints cannot be based on XML columns </li></ul></ul></ul><ul><ul><li>Data warehouses are oriented toward relational data </li></ul></ul>
  8. 8. pureXML Capabilities <ul><li>XML data type </li></ul><ul><ul><li>With specialized hierarchical storage structure </li></ul></ul><ul><li>Indexing capabilities </li></ul><ul><ul><li>Based on data within XML documents </li></ul></ul><ul><li>New query languages (XPath and SQL/XML) </li></ul><ul><ul><li>New query optimization techniques too </li></ul></ul><ul><li>XML schema support </li></ul><ul><ul><li>Including validation </li></ul></ul><ul><li>Database utilities support </li></ul><ul><li>Integration with JDBC, ODBC, Embedded SQL </li></ul><ul><li>XML shredding and publishing facilities </li></ul><ul><ul><li>For composing and decomposing XML documents </li></ul></ul>
  9. 9. Benefits of DB2 pureXML Technology <ul><li>Faster development </li></ul><ul><ul><li>Code simplification </li></ul></ul><ul><ul><li>Avoiding XML-relational transformations </li></ul></ul><ul><li>Increased agility </li></ul><ul><ul><li>Versatile XML schema evolution </li></ul></ul><ul><ul><ul><li>Quickly modify applications to support new or changing requirements </li></ul></ul></ul><ul><li>Improved usability </li></ul><ul><ul><li>Exploit previously unmanaged XML data </li></ul></ul><ul><ul><li>Speed up query processing through XML-optimized storage and indexing </li></ul></ul>
  10. 10. pureXML Usage Scenarios <ul><li>Integration of diverse data sources </li></ul><ul><ul><li>DB2 provides the ability to join XML documents </li></ul></ul><ul><li>Form processing </li></ul><ul><ul><li>Store whole electronic forms in DB2 rather than shredding </li></ul></ul><ul><li>Document storage and querying </li></ul><ul><ul><li>Store and manage less structured (document-centric) XML data in DB2 </li></ul></ul><ul><li>Using XML to support transactions </li></ul><ul><ul><li>Service-oriented architectures (SOA) offer message-based transactions </li></ul></ul><ul><ul><li>XML data can then be retrieved, updated, searched and analyzed </li></ul></ul><ul><li>Syndication and XML feeds </li></ul><ul><ul><li>Serve XML feeds through a Web services interface </li></ul></ul><ul><ul><li>Provide a repository for XML data to support these feeds </li></ul></ul>
  11. 11. DB2 Support for XML 0 – XML Storage 1 – Bind in XML 2 – Store as XML 3 – Shred into Relational 4 – Retrieve XML data 5 – Publish as XML 6 – Bind XML output 7 – XML to XML 8 – XML to Relational 9 – Relational to XML Relational DB2 ENGINE XML Textual XML Application that makes use of XML 1 2 3 4 5 6 7 8 9 0 XSR (validation)
  12. 12. pureXML Architecture <ul><li>DB2 9 – a hybrid database system </li></ul><ul><ul><li>One database with both relational and native XML data </li></ul></ul><ul><ul><li>Single hybrid database engine handles all processing </li></ul></ul><ul><li>Application may combine SQL and SQL/XML </li></ul><ul><ul><li>Access relational and XML data in a hybrid database </li></ul></ul><ul><li>XML data is stored separately from other table contents </li></ul><ul><ul><li>Has its own table space </li></ul></ul><ul><li>Supports XML document validation with XML schemas </li></ul><ul><ul><li>XML schemas used for validation are registered with DB2 </li></ul></ul><ul><ul><ul><li>XML Schema Repository (XSR) </li></ul></ul></ul>
  13. 13. XML Data Type <ul><li>DB2 native XML support includes a new XML data type </li></ul><ul><ul><li>An XML column holds one XML document for each row </li></ul></ul><ul><li>XML data is stored in a parsed tree structure </li></ul><ul><li>XML document can also be stuffed into LOB or VARCHAR </li></ul><ul><ul><li>Provides advantages in certain scenarios </li></ul></ul><ul><li>XML storage requirements </li></ul><ul><ul><li>Separate XML tablespace plus space for any needed indexes </li></ul></ul>
  14. 14. <ul><li>Optimized storage </li></ul><ul><ul><li>New XML data type </li></ul></ul><ul><ul><ul><li>Supports insert, update, and delete </li></ul></ul></ul><ul><ul><ul><li>Stores parsed XML documents </li></ul></ul></ul><ul><ul><ul><li>Available when creating or altering tables </li></ul></ul></ul><ul><li>CREATE TABLE PATIENT (PATIENTID CHAR(6), PATIENT_XML XML ) </li></ul><ul><li>CREATE TABLE P2 (PID INT, PTYP CHAR(8), PX1 XML , PX2 XML ) </li></ul><ul><ul><li>Supports access to nodes within XML document via XPath </li></ul></ul><ul><li>Query optimization </li></ul><ul><ul><li>CREATE INDEX supports specification of an xmlpattern </li></ul></ul>The XML Data Type
  15. 15. XML Indexes <ul><li>Indexes are often used to improve query performance </li></ul><ul><li>Indexes on XML columns are supported in DB2 9 </li></ul><ul><ul><li>Uses an XML pattern (XPath) expression </li></ul></ul><ul><ul><li>Indexes paths and values in stored XML documents </li></ul></ul><ul><li>XML index entries provide access to document nodes </li></ul><ul><ul><li>Not limited to providing access to the beginning of a document </li></ul></ul><ul><ul><li>Index keys are created based on XML pattern expressions </li></ul></ul>CREATE INDEX PATINDEX ON PATIENT(PATIENT_XML) GENERATE KEY USING XMLPATTERN '/patient/service/sdate' as SQL VARCHAR(10)
  16. 16. Application Development Support <ul><li>Supports developing apps that include XML requirements </li></ul><ul><ul><li>Language support: C/C++, Java, Assembler, Cobol, PL/I </li></ul></ul><ul><ul><li>API support: JDBC, DB2 / ODBC, Embedded SQL, SQLJ </li></ul></ul><ul><ul><li>SQL/XML query support </li></ul></ul><ul><ul><li>DB2 sample database enhancements </li></ul></ul><ul><li>Universal DB2 driver for JDBC enhanced to support XML </li></ul><ul><ul><li>Provided extension XML type: </li></ul></ul><ul><ul><ul><li>import; </li></ul></ul></ul><ul><ul><ul><li>DB2Xml xml1 = (DB2Xml) rs.getObject (&quot;patient_xml&quot;); </li></ul></ul></ul><ul><ul><ul><li>String s = xml1.getDB2String(); </li></ul></ul></ul><ul><ul><ul><li>InputStream is = xml1.getDB2XMLBinaryStream(&quot;UTF-16&quot;); </li></ul></ul></ul>
  17. 17. DB2 and XPath Navigating Through the XML Tree
  18. 18. DB2 XPath <ul><li>XPath is an expression language </li></ul><ul><ul><li>Designed by the World Wide Web Consortium (W3C) </li></ul></ul><ul><ul><li>Used to navigate XML documents </li></ul></ul><ul><ul><li>XPath expressions are similar to file path notations </li></ul></ul><ul><li>DB2 XPath can be used: </li></ul><ul><ul><li>With the XMLQUERY SQL built-in function </li></ul></ul><ul><ul><ul><li>To extract data from an XML column </li></ul></ul></ul><ul><ul><li>With the XMLEXISTS SQL predicate </li></ul></ul><ul><ul><ul><li>To evaluate data in an XML column </li></ul></ul></ul><ul><ul><li>When creating an XML index </li></ul></ul><ul><ul><ul><li>To determine the XML document nodes to be indexed </li></ul></ul></ul>'/patient/name'  All name elements within patient elements
  19. 19. XPath Expressions <ul><li>XPath expressions: the basic building block of Xpath </li></ul><ul><li>Types of expressions provided by DB2 XPath: </li></ul><ul><ul><li>Primary expressions : basic primitives of the language </li></ul></ul><ul><ul><ul><li>Include literals, variable references, and function calls </li></ul></ul></ul><ul><ul><li>Path expressions : locate nodes within a document tree </li></ul></ul><ul><ul><ul><li>Include Node Tests and Filter Expressions </li></ul></ul></ul><ul><ul><li>Arithmetic expressions : add, subtract, multiply, divide, modulus </li></ul></ul><ul><ul><li>Comparison expressions : compare two values </li></ul></ul><ul><ul><li>Logical expressions : use boolean logic </li></ul></ul><ul><li>Anywhere an expression is expected, any kind of expression can be used </li></ul><ul><ul><li>Operands of an expression are typically other expressions </li></ul></ul>
  20. 20. XPath Data Model Employee Name Address Phone Phone Dept LastName FirstName Sample comment OneEmp.xml Street City State Zipcode Roger Rabbit 123 Main Smallville Wyoming 98765 800-555-1234 212-321-4321 Sales type= &quot;home&quot; type= &quot;cell&quot; mgr= &quot;bob&quot; Key: Document node Comment node Element Node Attribute node Text node
  21. 21. Types of XPath Nodes <ul><li>Document node encapsulates an XML document </li></ul><ul><ul><li>Parent of root element node </li></ul></ul><ul><li>Element node encapsulates an XML element </li></ul><ul><ul><li>Can have one parent and many children </li></ul></ul><ul><li>Attribute node represents an XML attribute </li></ul><ul><ul><li>Belongs to an element </li></ul></ul><ul><li>Text node encapsulates XML character content </li></ul><ul><ul><li>Elements may have these </li></ul></ul><ul><li>Processing Instruction (PI) node </li></ul><ul><ul><li>Encapsulates XML processing instruction </li></ul></ul><ul><li>Comment node encapsulates an XML comment </li></ul><ul><li>Namespaces node is considered to be a node in XPath </li></ul>
  22. 22. XPath Nodes Processing Order course target= ourOwnPIforCourseware descrip To be determined Lots of information th=&quot;http:// themis. com/test&quot; name=format value=“instructor led” title XML for DB2 Document node 1 Element node 2,5,7 Comment node 9 Processing Instruction 10 Text node 6,8 Attribute node 4 Namespace node 3 <?xml version=&quot;1.0&quot;?> <th:course xmlns:th=&quot;; th:format=&quot;instructor led&quot;> <th:title>XML for DB2</th:title> <th:descrip>Lots of information <!-- To be determined --> </th:descrip> <?ourOwnPIforCourseware?> </th:course>
  23. 23. Path Expressions Overview <ul><li>Path expressions navigate the XML tree structure to locate nodes </li></ul><ul><li>Navigation axes are used in XPath; always start at context node </li></ul><ul><ul><li>ForwardAxis moves down through the XML tree: </li></ul></ul><ul><ul><ul><li>attribute (@) </li></ul></ul></ul><ul><ul><ul><li>child ( default ) </li></ul></ul></ul><ul><ul><ul><li>descendant </li></ul></ul></ul><ul><ul><ul><li>self (.) </li></ul></ul></ul><ul><ul><ul><li>descendant-or-self (//=/descendant-or-self:node()/) </li></ul></ul></ul><ul><ul><li>ReverseAxis moves up through the XML tree: </li></ul></ul><ul><ul><ul><li>parent (..) </li></ul></ul></ul><ul><li>Three parts of an axis step: </li></ul><ul><ul><li>Axis specifies a direction of movement </li></ul></ul><ul><ul><li>Node test specifies node selection criteria </li></ul></ul><ul><ul><li>Predicates (zero or more) filter returned sequence </li></ul></ul>Element Comment self parent attribute children descendants Element Element Text Text
  24. 24. Path Expressions Syntax <ul><li>XML document to support upcoming example code: </li></ul><ul><li>XPath expressions support abbreviated syntax in axis steps </li></ul><ul><ul><li>@ is abbreviated syntax for attribute </li></ul></ul><ul><ul><li>// is abbreviated syntax for /descendant-or-self::node()/ </li></ul></ul><ul><ul><li>.. is abbreviated syntax for parent::node() </li></ul></ul><ul><ul><li>. is abbreviated syntax for self::node() </li></ul></ul><ul><ul><li>is abbreviated syntax for child::node() </li></ul></ul><ul><li><patient id=&quot;11123&quot;> </li></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></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>prescribed 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>
  25. 25. Path Expression Examples <ul><li>‘ / ’  slash indicates that path begins at root node </li></ul><ul><ul><li>Whole XML document </li></ul></ul><ul><li>‘ //service’  two slashes at beginning of path expression </li></ul><ul><ul><li>Requested node, service, may be located anywhere in the document </li></ul></ul><ul><ul><li>Returns the entire service element, including all of its children </li></ul></ul><ul><li>‘ //phone/@*’  All attributes appearing under the phone element </li></ul><ul><ul><li>Only attribute under phone is: home </li></ul></ul><ul><li>‘ //@*’  All attributes in the XML document </li></ul><ul><ul><li>All attributes: 11123 home </li></ul></ul><ul><li>‘ /patient/name’  All name elements under patient </li></ul><ul><ul><li>Only name is Sara Lee </li></ul></ul><ul><li>‘ /patient/addr/city/..’  All child elements of addr </li></ul><ul><ul><li>Element addr , the parent of city , plus all child elements of addr </li></ul></ul>
  26. 26. Filter Expressions <ul><li>A filter expression is a path expression followed by predicates in square brackets </li></ul><ul><ul><li>Filter its result based on applying conditions; for example: </li></ul></ul><ul><li>All service information of patients who have a $15.00 copay </li></ul><ul><ul><li>'/patient/*[copay=&quot;15.00&quot;]' </li></ul></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><li>Set context node to patient and filter based on attribute type rather than on an element </li></ul><ul><ul><li>/patient/phone[@type=&quot;home&quot;] </li></ul></ul><ul><ul><li><phone type=&quot;home&quot;>908-842-7531</phone> </li></ul></ul><ul><li>Any patient that has email </li></ul><ul><ul><li>/patient[email] </li></ul></ul><ul><ul><li>Entire XML document </li></ul></ul><ul><li><patient id=&quot;11123&quot;> </li></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></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>prescribed 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>
  27. 27. Arithmetic Expressions <ul><li>Arithmetic expressions: perform operations that involve addition, subtraction, multiplication, division, and modulus </li></ul><ul><li>The XPath arithmetic operators : </li></ul><ul><ul><li>* multiplication </li></ul></ul><ul><ul><li>div division </li></ul></ul><ul><ul><li>idiv integer division </li></ul></ul><ul><ul><li>mod modulus </li></ul></ul><ul><ul><li>+ addition </li></ul></ul><ul><ul><li>- subtraction </li></ul></ul><ul><li>An arithmetic expression results in a numeric value </li></ul><ul><ul><li>Or an empty sequence or an error </li></ul></ul><ul><li>Place arithmetic expressions in parentheses </li></ul>
  28. 28. Arithmetic Expression Example <ul><li>An arithmetic expression to calculate the the remaining balance after the copay (and its return value): </li></ul><ul><ul><li>/patient/service/(cost – copay) </li></ul></ul><ul><ul><li>69.00 </li></ul></ul><ul><li><patient id=&quot;11123&quot;> </li></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></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>prescribed 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>
  29. 29. Comparison Expressions – General Comparisons <ul><li>Comparison expressions allow comparing two values </li></ul><ul><ul><li>The comparison operators are = != < <= > >= </li></ul></ul><ul><li>All services with a cost greater than 60 </li></ul><ul><ul><li>'/patient/service/cost > 60' </li></ul></ul><ul><ul><li>true </li></ul></ul><ul><li>All services with a cost greater than 60, but get the patient’s service info instead of just true or false </li></ul><ul><ul><li>'/patient/service[cost > 60]' </li></ul></ul><ul><ul><li><service> </li></ul></ul><ul><ul><li><sdate>2004-02-29</sdate> </li></ul></ul><ul><ul><li><reason>brittle nails</reason> </li></ul></ul><ul><ul><li><descrip>prescribed hormones </li></ul></ul><ul><ul><li></descrip> </li></ul></ul><ul><ul><li><cost>84.00</cost> </li></ul></ul><ul><ul><li><copay>15.00</copay> </li></ul></ul><ul><ul><li></service> </li></ul></ul><ul><li><patient id=&quot;11123&quot;> </li></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> </li></ul><ul><li></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>prescribed hormones </li></ul><ul><li></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>
  30. 30. Comparison Expressions – Logical Comparisons <ul><li>Logical expressions using AND return true if both of two expressions are true </li></ul><ul><li>Logical expressions using OR return true if one or both expressions are true </li></ul><ul><li>Return phone numbers of the type work or fax (two options shown) '//phone[./@type=&quot;work&quot; or ./@type=&quot;fax&quot;]' '//phone[@type=&quot;work&quot; or @type=&quot;fax&quot;]' </li></ul><ul><li>The result is this phone info </li></ul><ul><ul><li><phone type=&quot;work&quot;>908-842-7531</phone> <phone type=&quot;fax&quot;>908-751-2468</phone> </li></ul></ul><ul><li><patient id=&quot;11123&quot;> </li></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><phone type=&quot;fax&quot;>908-751-2468 </phone> </li></ul><ul><li><email> </li></ul><ul><li></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>prescribed hormones </li></ul><ul><li></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>
  31. 31. The Built-in Function Library <ul><li>Built-in functions offered by the DB2 XPath library: </li></ul><ul><ul><li>String functions </li></ul></ul><ul><ul><li>Numeric functions </li></ul></ul><ul><ul><li>Functions working on boolean values </li></ul></ul><ul><ul><li>Functions working on sequences </li></ul></ul><ul><li>Calls to these function are allowed in an XPath expression anywhere an expression is expected </li></ul><ul><li>Example: </li></ul>SELECT XMLQUERY('fn:concat($x,$y)' PASSING 'come ' AS &quot;x&quot;, 'together' AS &quot;y&quot;) FROM SYSIBM.SYSDUMMY1;
  32. 32. A Sampling of XPath Functions <ul><li>fn:compare compares two strings to see which one is greater </li></ul><ul><li>fn:concat concatenates two or more strings into a single string </li></ul><ul><li>fn:contains determines whether a string contains a given substring </li></ul><ul><li>fn:count returns the number of values in a sequence </li></ul><ul><li>fn:normalize-space strips leading and trailing whitespace characters </li></ul><ul><li>fn:lower-case converts a string to lowercase </li></ul><ul><li>fn:matches determines whether a string matches a given pattern </li></ul><ul><li>fn:position returns the position of the context item in the sequence </li></ul><ul><li>fn:replace replaces characters that match a pattern </li></ul><ul><li>fn:round returns the integer that is closest to a numeric value </li></ul><ul><li>fn:string returns the string representation of a value </li></ul><ul><li>fn:string-length returns the length of a string </li></ul><ul><li>fn:substring returns a substring of a string </li></ul><ul><li>fn:upper-case converts a string to uppercase </li></ul>
  33. 33. XML Namespaces <ul><li>XML namespaces prevent naming collisions </li></ul><ul><ul><li>An XML namespace is a set of names identified by a namespace URI </li></ul></ul><ul><ul><li>Distinguishes element types or attribute names with the same name associated with different DTDs or Schemas </li></ul></ul><ul><li>Namespaces allow qualifying names of elements and attributes </li></ul><ul><ul><li>Contain an optional namespace prefix, a colon, and a local name </li></ul></ul><ul><li>Example: two elements with the same name bound to different URIs: </li></ul><anElement xmlns:p1=&quot;someURI&quot; xmlns:p2=&quot;otherURI&quot; > <ElementABC> < p1:table >excel</p1:table> < p2:table >dining</p2:table> <table>DB2</table> </ElementABC> </anElement> empty prefix; bound to default element namespace
  34. 34. Prolog – Namespace Definition <ul><li>A DB2 XPath expression optionally contains a prolog </li></ul><ul><ul><li>Establishes the processing environment </li></ul></ul><ul><li>Prolog declaration may specify multiple namespace declarations </li></ul><ul><ul><li>May also specify one default namespace declaration </li></ul></ul><ul><li>Prolog declaration is always followed by a semicolon (;) </li></ul><ul><ul><li>Syntax: </li></ul></ul><ul><ul><li>Examples: </li></ul></ul>declare namespace prefix=&quot;namespace string literal&quot;; declare default element namespace &quot;namespace string literal&quot;; declare namespace fn=&quot;;; declare default element namespace &quot;;;
  35. 35. Indexes and XML Indexes Built On Values Within XML Documents
  36. 36. XML Indexing <ul><li>DB2 supports creating indexes on XML columns </li></ul><ul><ul><li>Generated using XML pattern expressions </li></ul></ul><ul><ul><li>Support access to nodes in the document </li></ul></ul><ul><li>Multiple parts of an XML document can satisfy an XML pattern </li></ul><ul><ul><li>Multiple index keys may be generated for insert of a single document </li></ul></ul><ul><li>GENERATE KEY USING XMLPATTERN clause of CREATE INDEX </li></ul><ul><ul><li>Specifies what you want to index </li></ul></ul><ul><ul><li>Contains XML pattern expression </li></ul></ul>CREATE UNIQUE INDEX PATIENT_ID_IX ON PATIENT(PATIENT_XML) GENERATE KEY USING XMLPATTERN '/patient/@id' AS SQL VARCHAR(5) Same as before XML XML node to be indexed Required keywords Type of stored index values
  37. 37. Data Types Associated With Pattern Expressions <ul><li>Keys from XML pattern expression specified in a CREATE INDEX statement must be associated with a data type </li></ul><ul><ul><li>May use either DECFLOAT or VARCHAR(n), where n <= 1000 </li></ul></ul><ul><ul><li>Value being inserted/indexed must be convertible to this type </li></ul></ul>INSERT INTO PATIENT VALUES('12345', '<patient id=&quot;123456&quot; ><name>Jim Beam</name></patient>') DSNT408I SQLCODE = -20305, ERROR: AN XML VALUE CANNOT BE INSERTED OR UPDATED BECAUSE OF AN ERROR DETECTED WHEN INSERTING OR UPDATING THE INDEX IDENTIFIED BY 'DBID~132 OBID~23' ON TABLE *N. REASON CODE = 1. Value too long for VARCHAR(5) index
  38. 38. UNIQUE Keyword in XML Index Definition <ul><li>The UNIQUE keyword is supported in XML index definitions </li></ul><ul><ul><li>However, its meaning is different than in relational index definitions </li></ul></ul><ul><li>When creating a relational index, the UNIQUE keyword enforces uniqueness across all rows in the table </li></ul><ul><li>When creating an index over XML data, the UNIQUE keyword enforces uniqueness across all documents in an XML column </li></ul>
  39. 39. Example Queries and Supporting Indexes <ul><li>Example 1 </li></ul><ul><ul><li>SELECT * FROM PATIENT WHERE XMLEXISTS('$Z/patient/service[copay=&quot;10.00&quot;]' PASSING BY REF PATIENT_XML AS &quot;Z&quot;) </li></ul></ul><ul><li>Supporting index </li></ul><ul><ul><li>CREATE INDEX copayIdx on PATIENT(PATIENT_XML) GENERATE KEY USING XMLPATTERN '/patient/service/copay' AS SQL DECFLOAT </li></ul></ul><ul><li>Example 2 </li></ul><ul><ul><li>SELECT * FROM PATIENT WHERE XMLEXISTS('$Z/patient/phone[@type=&quot;work&quot;]' PASSING BY REF PATIENT_XML AS &quot;Z&quot;) </li></ul></ul><ul><li>Supporting index </li></ul><ul><ul><li>CREATE INDEX phoneTypIdx on PATIENT(PATIENT_XML) GENERATE KEY USING XMLPATTERN '/patient/phone/@type' AS SQL VARCHAR(20) </li></ul></ul><ul><li>Example 3 </li></ul><ul><ul><li>SELECT * FROM PATIENT WHERE XMLEXISTS('$Z/patient/addr[city=&quot;Uptown&quot;]' PASSING BY REF PATIENT_XML AS &quot;Z&quot;) </li></ul></ul><ul><li>Supporting index </li></ul><ul><ul><li>CREATE INDEX cityIdx on PATIENT(PATIENT_XML) GENERATE KEY USING XMLPATTERN '/patient/addr/city' AS SQL VARCHAR(20) </li></ul></ul>
  40. 40. XML Schemas, Validation and XML Schema Repository (XSR)
  41. 41. XML Schema <ul><li>XML Schema Definition (XSD) defines structure of XML instance documents </li></ul><ul><ul><li>Published as a recommendation by W3C </li></ul></ul><ul><ul><li>Defines elements and attributes permitted in a document </li></ul></ul><ul><ul><li>Defines parent / child relationship between elements </li></ul></ul><ul><ul><li>Defines data types, constraints and values for elements and attributes </li></ul></ul><ul><ul><li><xsd:schema xmlns:xsd=&quot;;> </li></ul></ul><ul><ul><li><xsd:element name=&quot;patient&quot;> </li></ul></ul><ul><ul><li><xsd:complexType> </li></ul></ul><ul><ul><li><xsd:sequence> </li></ul></ul><ul><ul><li><xsd:element name=&quot;id&quot; type=&quot;xsd:integer&quot;/> </li></ul></ul><ul><ul><li><xsd:element name=&quot;name&quot; type=&quot;xsd:string&quot;/> </li></ul></ul><ul><ul><li><xsd:element name=&quot;addr&quot; type=&quot;xsd:string&quot;/> </li></ul></ul><ul><ul><li><xsd:element name=&quot;dob&quot; type=&quot;xsd:date&quot;/> </li></ul></ul><ul><ul><li><xsd:element name=&quot;amount&quot; type=&quot;xsd:double&quot;/> </li></ul></ul><ul><ul><li></xsd:sequence> </li></ul></ul><ul><ul><li></xsd:complexType> </li></ul></ul><ul><ul><li></xsd:element> </li></ul></ul><ul><ul><li></xsd:schema> </li></ul></ul>
  42. 42. XML Schema Management With the XSR <ul><li>XML schema repository ( XSR ) </li></ul><ul><ul><li>Set of tables that store XML schemas </li></ul></ul><ul><ul><li>Created during DB2 installation or migration </li></ul></ul><ul><li>XML schemas may be added to the XSR </li></ul><ul><ul><li>Then used to validate XML documents being inserted or updated </li></ul></ul><ul><li>Registering XML schema documents (adding to XSR): </li></ul><ul><ul><li>Call DB2-supplied stored procedures from a DB2 application </li></ul></ul><ul><ul><li>Or invoke a provided JDBC method from a Java application </li></ul></ul><ul><li>Removing XML schema documents from the DB2 XSR </li></ul><ul><ul><li>Call DB2-supplied stored procedure or invoke JDBC method </li></ul></ul>Only schemas, and not DTDs, may be used for XML validation in DB2 Version 9
  43. 43. XML Schema Validation <ul><li>Use SQL INSERT statement to insert data into XML column </li></ul><ul><ul><li>Inserted data must be a well-formed XML document </li></ul></ul><ul><li>Validate the XML against a registered XML schema during insertion using the DSN_XMLVALIDATE function </li></ul><ul><ul><li>User Defined Function </li></ul></ul><ul><li>XML validation determines whether the structure, content, and data types of an XML document are valid according to a corresponding schema </li></ul><ul><li>Validation is optional </li></ul>INSERT into AutoDealers VALUES( '12345', CURRENT DATE, 'Sams Deals', DSN_XMLValidate(:xmlDealerInfo, SYSXSR.DealerInfoSchema));
  44. 44. XML Decomposition <ul><li>Decomposition, or shredding, is the process of storing XML document content in columns of relational tables </li></ul><ul><ul><li>Decomposed data has SQL type of column where it is inserted </li></ul></ul><ul><li>An XML schema consists of one or more XML schema documents </li></ul><ul><li>Annotated XML schema decomposition </li></ul><ul><ul><li>Control of the decomposition process is provided by XML schema annotation </li></ul></ul>
  45. 45. Resources <ul><li>DB2 Version 9.1 for z/OS XML Guide (SC18-9858-03) </li></ul><ul><li>DB2 9 for z/OS Technical Overview (SG24-7330-00) </li></ul><ul><li>DB2 Version 9.1 for z/OS Application Programming and SQL Guide (SC18-9841-01) </li></ul><ul><li>DB2 Version 9.1 for z/OS Utility Guide and Reference (SC18-9855-02) </li></ul><ul><li>DB2 Version 9.1 for z/OS Application Programming Guide and Reference for Java (SC18-9842-01 ) </li></ul>