DB2 Developer Workbench, Part 3: Developer
      Workbench and XML
      Basic tasks

      Skill Level: Intermediate


  ...
developerWorks®                                                                      ibm.com/developerWorks



           ...
ibm.com/developerWorks                                                              developerWorks®



      perspectives ...
developerWorks®                                                                      ibm.com/developerWorks




     Conne...
ibm.com/developerWorks                                                         developerWorks®




      On the Select a w...
developerWorks®                                                                 ibm.com/developerWorks




     Select Use...
ibm.com/developerWorks                                                           developerWorks®




      Specify the JDK...
developerWorks®                                                                     ibm.com/developerWorks




     Sectio...
ibm.com/developerWorks                                                              developerWorks®




      Click the Sa...
developerWorks®                                                                          ibm.com/developerWorks



     Fi...
ibm.com/developerWorks                                                             developerWorks®




                • D...
developerWorks®                                                                     ibm.com/developerWorks




     In the...
ibm.com/developerWorks                                                            developerWorks®




      Type customeri...
developerWorks®                                                                  ibm.com/developerWorks




     Change th...
ibm.com/developerWorks                                                            developerWorks®




      Click the Save...
developerWorks®                                                                     ibm.com/developerWorks




     Regist...
ibm.com/developerWorks                                                           developerWorks®




      In the XML Sche...
developerWorks®                                                                 ibm.com/developerWorks



     should see ...
ibm.com/developerWorks                                                       developerWorks®




      In the Navigator vi...
developerWorks®                                                                   ibm.com/developerWorks




     Select c...
ibm.com/developerWorks                                                              developerWorks®




      Section 7. E...
developerWorks®                                                                      ibm.com/developerWorks



     In the...
ibm.com/developerWorks                                                             developerWorks®



      Use the XML Ce...
developerWorks®                                                                 ibm.com/developerWorks




     Observe th...
ibm.com/developerWorks                                                   developerWorks®




      Select the Specify a re...
developerWorks®                                                                    ibm.com/developerWorks




     The XML...
ibm.com/developerWorks                                                           developerWorks®




      In the Database...
developerWorks®                                                                     ibm.com/developerWorks




     Sectio...
ibm.com/developerWorks                                                             developerWorks®



      dropping nodes...
developerWorks®                                                                   ibm.com/developerWorks




     To build...
ibm.com/developerWorks                                                        developerWorks®




      Representative doc...
developerWorks®                                                                  ibm.com/developerWorks




     Verify th...
ibm.com/developerWorks                                                                developerWorks®




      Drag and d...
developerWorks®                                                                    ibm.com/developerWorks



     Figure 5...
ibm.com/developerWorks                                                             developerWorks®




      Section 10. C...
developerWorks®                                                                     ibm.com/developerWorks



     In the ...
ibm.com/developerWorks                                                             developerWorks®



      XMLBasic proje...
developerWorks®                                                                   ibm.com/developerWorks




     In the D...
ibm.com/developerWorks                                                             developerWorks®




      The stored pr...
developerWorks®                                                                    ibm.com/developerWorks




     On the ...
ibm.com/developerWorks                                         developerWorks®




Developer Workbench and XML
© Copyright...
developerWorks®                                                                       ibm.com/developerWorks




     Reso...
Upcoming SlideShare
Loading in …5
×

DB2 Developer Workbench, Part 3: Developer Workbench and XML

709 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
709
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
14
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

DB2 Developer Workbench, Part 3: Developer Workbench and XML

  1. 1. DB2 Developer Workbench, Part 3: Developer Workbench and XML Basic tasks Skill Level: Intermediate Debra R. Eaton (deaton@us.ibm.com) DB2 Software Information Technology Specialist IBM 19 Oct 2006 The IBM® DB2® Developer Workbench (DWB) provides out-of-the-box integrated development for DB2 9 pureXML. DWB is based on the Eclipse open source Integrated Development Environment (IDE). Learn how the DWB resources, perspectives, views, editors, and wizards assist you to work with the XML functionality in DWB. Section 1. Before you start About this tutorial This tutorial takes a basic approach to working with XML in the DWB Eclipse environment, starting by creating an XML table, schema and document, followed by querying XML data with a stored procedure. Objectives • Learn how the DWB resources, perspectives, views, editors, and wizards assist you to work with XML. • Become familiar with tasks such as creating, registering, and editing XML schemas, creating XML instance documents from schemas, creating XML-related database objects such as tables that contain XML data Developer Workbench and XML © Copyright IBM Corporation 1994, 2007. All rights reserved. Page 1 of 42
  2. 2. developerWorks® ibm.com/developerWorks columns, and creating queries for XML data. Prerequisites You must install and configure the SAMPLE database that is included with DB2 9.1. See the DB2 product documentation and First Steps for more information. Ensure the SAMPLE database is XML-enabled by specifying the XML option at database creation. See Resources for links to install DB2 and the Developer Workbench. System requirements To run the examples in this tutorial, you need to install DB2 9.1 and the SAMPLE database. You must be able to connect to the SAMPLE database with a user ID and password. The schema used throughout this tutorial is DEATON. Replace the DEATON schema with your schema, represented in the instructions by SCHEMANAME, when a task requires a schema name. Section 2. Set up the Data perspective Introduction This section steps you through the initial tasks of working with the Data perspective in DWB. In this section, accomplish the following tasks: • Start DWB • Verify the Data perspective • Connect to a database • Create a data development project Start DWB From the Windows desktop, select Start > All Programs > IBM DB2 Developer Workbench > DB2 Developer Workbench 9.1 > DB2 Developer Workbench. Click OK to pick the default location. After you choose the workspace location, a single Workbench window displays. A Workbench window offers one or more Developer Workbench and XML Page 2 of 42 © Copyright IBM Corporation 1994, 2007. All rights reserved.
  3. 3. ibm.com/developerWorks developerWorks® perspectives that contain editors and views. Note: The first time the workspace is used, the DWB welcome screen is displayed. You then have to click on the arrow in the upper, right-hand corner (the Workbench link) to get to the Workbench, where you are shown the Data perspective. You can launch this welcome page using the help menu. Figure 1. Start DWB Verify the Data perspective DWB opens by default in the Data perspective. Data will appear on the title bar in the upper left. The Data icon will be indented on the icon bar in the upper right. Figure 2. Data perspective Developer Workbench and XML © Copyright IBM Corporation 1994, 2007. All rights reserved. Page 3 of 42
  4. 4. developerWorks® ibm.com/developerWorks Connect to a database DWB lists all known database connections in the Database Explorer view. Right-click the SAMPLE database connection in the Database Explorer Connections folder, and select Reconnect from the context menu. Note: You can create more database connections that the DB2 server does not know about with the Create Connection wizard. . Figure 3. New connection In the Database Authorization window, enter your database user ID and password. Select OK. Figure 4. Database authorization Create a project In the Data Project Explorer view, right-click on the white space within the view. Select New >Project. The New Data Development Project wizard will open. Figure 5. New project Developer Workbench and XML Page 4 of 42 © Copyright IBM Corporation 1994, 2007. All rights reserved.
  5. 5. ibm.com/developerWorks developerWorks® On the Select a wizard step, expand the Data folder. Select the Data Development Project option. Click Next. Figure 6. New project wizard selection In the Project name field, type XMLBasic. Click Next Figure 7. Project name Developer Workbench and XML © Copyright IBM Corporation 1994, 2007. All rights reserved. Page 5 of 42
  6. 6. developerWorks® ibm.com/developerWorks Select Use an existing connection. Select SAMPLE in the Existing connections list box. Click Next. Figure 8. New project connection Developer Workbench and XML Page 6 of 42 © Copyright IBM Corporation 1994, 2007. All rights reserved.
  7. 7. ibm.com/developerWorks developerWorks® Specify the JDK home directory in the JDK home text box, and click Finish. The data development project called XMLBasic is displayed in the Data Project Explorer view. Figure 9. New project JDK Developer Workbench and XML © Copyright IBM Corporation 1994, 2007. All rights reserved. Page 7 of 42
  8. 8. developerWorks® ibm.com/developerWorks Section 3. Create a table Introduction You can create a table in DWB with the SQL editor. In this section, accomplish the following tasks: • Create table XMLSAMPLE in the SAMPLE database • Define column DOC as an XML data type Create a table In the Database Explorer view, expand the SAMPLE database schema tree. Right-click the Tables node. Select New > With SQL Editor to launch the SQL Editor. Figure 10. Launch SQL Editor In the SQL Editor, enter the CREATE TABLE statement to create a table called XMLSample, which includes one XML column called doc. Add a DROP TABLE statement to remove a previous version of XMLSample. Figure 11. Create a table in the SQL Editor Developer Workbench and XML Page 8 of 42 © Copyright IBM Corporation 1994, 2007. All rights reserved.
  9. 9. ibm.com/developerWorks developerWorks® Click the Save icon on the task bar. In the Save As window, select the XMLBasic parent folder. Type create XMLSample.sql in the File Name text box. Click OK. Figure 12. Save create XMLSample.sql script In the Data Project Explorer view, in the XMLBasic project SQL Scripts folder, select the script create XMLSample.sql. Right-click on it, and select Run SQL. Developer Workbench and XML © Copyright IBM Corporation 1994, 2007. All rights reserved. Page 9 of 42
  10. 10. developerWorks® ibm.com/developerWorks Figure 13. SQL statement On the Data Output view, verify the script create XMLSample.sql ran successfully. You should see Status = Success, Action = Run, and Object Name = SQL Editor. In the Messages tab, you should see the message Run Successful. On the Database Explorer view, right-click the SAMPLE node. Select Refresh. Expand the SAMPLE database schema tree. Verify the XMLSample table exists in the Tables node. Figure 14. SELECT Section 4. Create an XML schema Introduction You can create an XML schema in DWB with the XML Schema Editor. The XML Schema Editor has two views: the Source view shows the schema source code inside a text editor, and the Graph view shows a graphical representation of the schema. Tabs at the bottom of the XML Schema Editor enable switching between the two views. In the Graph view, add new global elements and attributes with the context menu options. Drill down into element hierarchies by double clicking on the element. The context menu adds and modifies elements in the XML schema structure. In this section, accomplish the following tasks: • Create schema SAMPLESCHEMA Developer Workbench and XML Page 10 of 42 © Copyright IBM Corporation 1994, 2007. All rights reserved.
  11. 11. ibm.com/developerWorks developerWorks® • Define a complex type element called CUSTOMERINFO • Define an element sequence consisting of NAME, CITY, and ZIP in CUSTOMERINFO Create a schema In the Data Project Explorer view, expand the XMLBasic project tree. Right-click the XML Schema Documents node. Select New to launch the Create XML Schema wizard. Figure 15. Launch the Create XML Schema wizard In the New XML Schema window, select the XMLBasic parent folder. Type SampleSchema.xsd in the File Name text box. Click Finish. Figure 16. New schema name Developer Workbench and XML © Copyright IBM Corporation 1994, 2007. All rights reserved. Page 11 of 42
  12. 12. developerWorks® ibm.com/developerWorks In the XML Schema Editor, select the Graph tab at the bottom of the editor to switch to the graphical view of the XML Schema. Right-click the white space in the Element pane. Select Add Element. Figure 17. Add an element in the XML Schema Editor Developer Workbench and XML Page 12 of 42 © Copyright IBM Corporation 1994, 2007. All rights reserved.
  13. 13. ibm.com/developerWorks developerWorks® Type customerinfo in the black highlighted element. Right-click the customerinfo element. Select Set Type > New Complex Type. Figure 18. Set element type Double-click the customerinfo element to create an element hierarchy. To the right of the customerinfo box, appears a sequence box. Figure 19. Create a sequence box Right-click the sequence box. Select Add Element. Figure 20. Create a sequence box Developer Workbench and XML © Copyright IBM Corporation 1994, 2007. All rights reserved. Page 13 of 42
  14. 14. developerWorks® ibm.com/developerWorks Change the element name to Name. Figure 21. Create the Name element Add the element City and Zip to the customerinfo type using the same process as when you added Name. Verify the elements in the XML Schema Editor graphical view and the Outline view. Figure 22. Verify elements in the XML schema graphical view and Outline view Select the Source tab in the XML Schema Editor. Find the customerinfo, Name, City, and Zip elements in the schema. Figure 23. Verify the XML schema in the XML Schema Editor Developer Workbench and XML Page 14 of 42 © Copyright IBM Corporation 1994, 2007. All rights reserved.
  15. 15. ibm.com/developerWorks developerWorks® Click the Save icon on the task bar. Figure 24. Save icon Section 5. Register an XML schema Introduction You can register an XML schema to the XML Schema Repository with the DWB Register XML Schema Wizard. XML schemas need to be registered in the XML Schema Repository (XSR) so the database can decompose (shred) and validate XML data. A user can manage XML schema documents and XML schemas in the XSR by viewing their properties and dropping them from the server when they are no longer needed. In order to register an XML schema, you must have all the documents in your project. For multi-document schemas with dependencies, the schema location in the documents must be the filename of the documents. You can use registered XML schemas to perform validation testing in the Table Editor. When you insert or modify a value for an XML column, you can specify that the document is to be validated, either based on information in the document that indicates the schema which it conforms to, or a schema specified by the user. In this section, accomplish the following task: • Register SAMPLESCHEMA in the SAMPLE schema repository Developer Workbench and XML © Copyright IBM Corporation 1994, 2007. All rights reserved. Page 15 of 42
  16. 16. developerWorks® ibm.com/developerWorks Register an XML schema In the Data Project Explorer view, expand the XMLBasic project tree. Right-click the SampleSchema node. Select Register an XML Schema wizard to launch the Register XML Schema wizard. Figure 25. Launch the Register XML Schema Wizard In the Relational Schema Name and XML Schema window, select the DEATON relational schema name. Type SampleSchema in the XML Schema Name text box. Click Next. Figure 26. Relational Schema Name and XML Schema Name Developer Workbench and XML Page 16 of 42 © Copyright IBM Corporation 1994, 2007. All rights reserved.
  17. 17. ibm.com/developerWorks developerWorks® In the XML Schema Documents window, select the SampleSchema.xsd XML schema document. Click Finish. Figure 27. XML schema document On the Data Output view, verify the XML schema registration ran successfully. You Developer Workbench and XML © Copyright IBM Corporation 1994, 2007. All rights reserved. Page 17 of 42
  18. 18. developerWorks® ibm.com/developerWorks should see Status = Success, Action = Register, and Object Name = SAMPLESCHEMA. In the Messages tab, you should see the message Register Successful. On the Database Explorer view, right-click the SAMPLE node. Select Refresh. Expand the SAMPLE database schema tree. Verify the SAMPLESCHEMA exists in the XML Schemas node. Figure 28. Schema registration success verification Section 6. Generate an XML document from an XML schema Introduction Use the Navigator View to generate a sample XML document from an XML schema. In this section, accomplish the following task: • Generate XML document SAMPLESCHEMA.xml Generate an XML document from an XML schema Select the workbench window menu options Window > Show View > Navigator. If the Navigator view opens in the bottom, right area of DWB, drag the Navigator window on to the Data Project Explorer window. Figure 29. Open the Navigator view Developer Workbench and XML Page 18 of 42 © Copyright IBM Corporation 1994, 2007. All rights reserved.
  19. 19. ibm.com/developerWorks developerWorks® In the Navigator view, expand the XMLBasic project tree. Right-click SampleSchema.xsd. Select Generate > XML File to launch the Create XML file dialog. Click Next. Figure 30. Generate XML File context menu options Developer Workbench and XML © Copyright IBM Corporation 1994, 2007. All rights reserved. Page 19 of 42
  20. 20. developerWorks® ibm.com/developerWorks Select customerinfo in the root element dropdown list. Click Finish. A SampleSchema.xml file is generated from the SampleSchema.xsd file. The SampleSchema.xml file opens in the XML editor in the Source view, which consists of a text editor with color coding. Figure 31. Select the root element Developer Workbench and XML Page 20 of 42 © Copyright IBM Corporation 1994, 2007. All rights reserved.
  21. 21. ibm.com/developerWorks developerWorks® Section 7. Edit an XML document Introduction Use the XML Editor to edit an XML document. In this section, accomplish the following tasks: • Edit XML document SAMPLESCHEMA.xml • Alter element text values Edit an XML document Developer Workbench and XML © Copyright IBM Corporation 1994, 2007. All rights reserved. Page 21 of 42
  22. 22. developerWorks® ibm.com/developerWorks In the XML Editor, select the Design tab to switch to the graphical view of the SampleSchema.xml document hierarchy. Note: You can launch the XML Editor by opening the Navigator view, right-clicking on the XML file, and selecting Open With > XML Editor. Figure 32. XML Editor Source view of an XML document In the Design view of the XML Editor, expand the customerinfo element. Select the Name value in the grid box next to the Name element. Enter Smith. Select the City value in the grid box next to the City element. Enter Chicago. Select the Zip value in the grid box next to the Zip element. Enter 60611. Click the Save icon on the task bar. Figure 33. Enter element values Section 8. Insert an XML document into an XML column Introduction Developer Workbench and XML Page 22 of 42 © Copyright IBM Corporation 1994, 2007. All rights reserved.
  23. 23. ibm.com/developerWorks developerWorks® Use the XML Cell Editor to insert the SampleSchema.xml XML document into the DOC column in the XMLSample table. In this section, accomplish the following tasks: • Validate XML document SAMPLESCHEMA.XML with XMLVALIDATE • Insert SAMPLESCHEMA.XML into XML column DOC in table XMLSAMPLE • View SAMPLESCHEMA.XML in table XMLSAMPLE Insert an XML document into an XML column In the Database Explorer view, expand the SAMPLE database schema tree. Right-click the XMLSAMPLE table node. Select Data > Edit to launch the Table Data Editor. Figure 34. Launch the Table Data Editor The last row of the table data editor has a new row in the XML column. Click on the ellipses button to launch the XML Cell Editor. Select Import to import the XML document from a file system. Figure 35. XML Cell Editor new row Select SampleSchema.xml in the XMLBasic folder. Select Open. Figure 36. Import XML file dialog Developer Workbench and XML © Copyright IBM Corporation 1994, 2007. All rights reserved. Page 23 of 42
  24. 24. developerWorks® ibm.com/developerWorks Observe the XML document in the Source view. Select the Validate XML document option. Click Next. Figure 37. Validate an XML document option Developer Workbench and XML Page 24 of 42 © Copyright IBM Corporation 1994, 2007. All rights reserved.
  25. 25. ibm.com/developerWorks developerWorks® Select the Specify a registered XML schema option. Select SAMPLESCHEMA from the menu options. Click Finish. Figure 38. Specify a registered XML schema Developer Workbench and XML © Copyright IBM Corporation 1994, 2007. All rights reserved. Page 25 of 42
  26. 26. developerWorks® ibm.com/developerWorks The XML document is added as a row in the Table Data Editor. Figure 39. New row in Table Data Editor Click the Save icon on the task bar. The row is validated against the registered schema called SampleSchema and inserted into the XMLSAMPLE table. On the Data Output view, verify the INSERT and XMLVALIDATE statements ran successfully. You should see Status = Success, Action = Edit, and Object Name = XMLSAMPLE. In the Messages tab, you should see the message Inserted 1 row(s). Figure 40. New row Data Output view Developer Workbench and XML Page 26 of 42 © Copyright IBM Corporation 1994, 2007. All rights reserved.
  27. 27. ibm.com/developerWorks developerWorks® In the Database Explorer view, expand the SAMPLE database schema tree. Right-click the XMLSAMPLE table node. Select Data > SAMPLE Contents to retrieve sample data. Figure 41. New row Data Output view On the Data Output view, verify sample data was returned. You should see Status = Success, Action = Run, and Object Name = Sample Contents. In the Results tab, you should see one row returned. Click on the ellipses button to launch the source window. Select XML Tree to see a graphical view of the SampleSchema XML document stored in the XMLSAMPLE table. Figure 42. New row Data Output view Developer Workbench and XML © Copyright IBM Corporation 1994, 2007. All rights reserved. Page 27 of 42
  28. 28. developerWorks® ibm.com/developerWorks Section 9. Create an XQuery Introduction Use XQuery Builder to create an XQuery. The XQuery Builder interprets user GUI actions into queries without exposing you to language semantics. The XQuery Builder tool creates a tree view of an instance of the XML document, which will be queried. You can drag and drop nodes from the node tree view to the design grid(s). The design grid(s) are context sensitive and represent different XQuery language functions. Build the XQuery by adding resources to these grids and drilling into these resources. Different resources represent different functionality in the XQuery language and thus present different grid sets. The query is internally represented as an XML model. Each resource added into the grid is added to the XML model. You must associate a representative sample document used to build the XQuery with an XML column in the table. The instance of the XML document can be located in the file system or in the database from an XML column in a table. Dragging and Developer Workbench and XML Page 28 of 42 © Copyright IBM Corporation 1994, 2007. All rights reserved.
  29. 29. ibm.com/developerWorks developerWorks® dropping nodes from the sample XML document tree to a row on the design grid results in the creation of an XPath object in the XQuery model. This XPath object represents the node path in the target XML document. XQuery is passed to DB2 using the XQuery language keyword XQUERY or the SQL/XML language function XMLQUERY. Only the SQL/XML language function XMLQUERY allows runtime parameters to be passed into the XQuery. Since most queries contain predicates that are populated at runtime, the XQuery Builder only generates SQL/XML language queries. Also, SQL/XML queries are easy to embed in SQL statements created with the SQL Query Builder. In this section, accomplish the following tasks: • Select representative XML document SAMPLESCHEMA.XML • Associate representative XML document SAMPLESCHEMA.XML with XML column DOC in table XMLSAMPLE • Build XMLQuery statement in the XQuery Builder • Run the XMLQuery statement • View the query output Create an XQuery In the Data Project Explorer view, expand the XMLBasic project tree. Right-click the XML Queries node. Select New XML Query to launch the XQuery Builder. Figure 43. Launch the XQuery Builder Type the name SampleQuery in the File name text box, then click Next. Figure 44. Define a XML Query name Developer Workbench and XML © Copyright IBM Corporation 1994, 2007. All rights reserved. Page 29 of 42
  30. 30. developerWorks® ibm.com/developerWorks To build an XQuery, use sample XML documents to represent the XML hierarchical structure. The representative XML document can be loaded from the file system or fetched from the database. You will load the document from the file system. Click Add. Select the Local workspace option. Select Browse, then choose the SampleSchema.xml. Click OK. Click Finish. Figure 45. Select the representative document SampleSchema.xml is the representative document. Click Next. Figure 46. View the representative document Developer Workbench and XML Page 30 of 42 © Copyright IBM Corporation 1994, 2007. All rights reserved.
  31. 31. ibm.com/developerWorks developerWorks® Representative documents must be associated with XML columns so that the query is built to obtain data from those columns. Select SampleSchema.xml. Click Associate. Figure 47. Launch associate Expand XMLSAMPLE table node. Select DOC. Click OK. Figure 48. Select the DOC XML column Developer Workbench and XML © Copyright IBM Corporation 1994, 2007. All rights reserved. Page 31 of 42
  32. 32. developerWorks® ibm.com/developerWorks Verify the XML document SampleSchema.xml is associated with the DOC XML column in the XMLSAMPLE table. Click Finish. Note: The Omit Schema check box results in the Relational schema (not XML schema) being omitted from the XQuery. Figure 49. Finish the association Developer Workbench and XML Page 32 of 42 © Copyright IBM Corporation 1994, 2007. All rights reserved.
  33. 33. ibm.com/developerWorks developerWorks® Drag and drop the customerinfo node from the SampleSchemal.xml tree view to a row on the design grid. The node name appears in the grid and a drill down button appears at the end of the row. Figure 50. Drag a resource onto the grid Click Step into at the end of the customerinfo row in the design grid. If the Step into button is not visible, select the customerinfo row. The FLWOR grid appears. The variable customerinfo, which contains the original XPath object that represented the customerinfo node in the sample XML document, is created and populated in the FLWOR grid. Figure 51. Drill into a resource Add a WHERE predicate to the FOR logic. Drag and drop the Name element from the SampleSchema.xml tree view to the Operand1 column in the where grid. Click the Operator column in the where grid to display a list of all comparison operators possible in the WHERE clause. Select the = operator. Enter Smith in the Operand2 column in the where grid. Click the Save icon on the task bar. Developer Workbench and XML © Copyright IBM Corporation 1994, 2007. All rights reserved. Page 33 of 42
  34. 34. developerWorks® ibm.com/developerWorks Figure 52. Drill into a resource Click the Source tab, and view the generated query. Expand the outline view of the XQuery to view the query in a graphical format. Figure 53. View query in Source and Outline views Select XQuery > Run on the menu bar to execute the query. Click Finish. Figure 54. XQuery context menu On the Data Output view, verify the query ran successfully. You should see Status = Success, Action = Run, and Object Name = SampleQuery.xqm. In the Results tab, you should see one document returned. Click on the ellipses button to launch the source window. Select XML Tree to see a graphical view of the SampleSchema XML document stored in the XMLSAMPLE table. Figure 55. Drill into a resource Developer Workbench and XML Page 34 of 42 © Copyright IBM Corporation 1994, 2007. All rights reserved.
  35. 35. ibm.com/developerWorks developerWorks® Section 10. Create a stored procedure for XML data Introduction Use the Stored Procedure Wizard to create a stored procedure that contains an XQuery. Stored procedures created in either SQL or Java can return result sets that contain XML columns. In this section, accomplish the following tasks: • Create a stored procedure • Import an XQuery • Run the stored procedure Create a stored procedure for XML data Developer Workbench and XML © Copyright IBM Corporation 1994, 2007. All rights reserved. Page 35 of 42
  36. 36. developerWorks® ibm.com/developerWorks In the Data Project Explorer view, expand the XMLBasic project tree. Right-click the Stored Procedures folder. Select New > Stored Procedure to launch the Stored Procedure Wizard. Click Next to continue. Figure 56. Launch the Stored Procedure Wizard In the Name and Language window, type XMLSP in the Name text box. Click Next. Figure 57. Name and language Select Import to import the SampleQuery.xqm SQL query statement from the Developer Workbench and XML Page 36 of 42 © Copyright IBM Corporation 1994, 2007. All rights reserved.
  37. 37. ibm.com/developerWorks developerWorks® XMLBasic project. Select SampleQuery from the options tree. Select Import. Figure 58. Import an XML statement Select Statement 1 in the Statements pane. Select Remove to delete the existing SELECT statement. Click Next. Click Next on the Parameters window. Figure 59. Remove a statement Developer Workbench and XML © Copyright IBM Corporation 1994, 2007. All rights reserved. Page 37 of 42
  38. 38. developerWorks® ibm.com/developerWorks In the Deploy Options window, type XMLSP1 in the Specific name text box. Select the Deploy check box. Click Next. Click Next on the Code fragments window. Click Finish on the Summary window. Figure 60. Stored procedure deploy options Developer Workbench and XML Page 38 of 42 © Copyright IBM Corporation 1994, 2007. All rights reserved.
  39. 39. ibm.com/developerWorks developerWorks® The stored procedure XMLSP is built and deployed to the database. XMLSP appears under the Stored Procedures folder in the Data Project Explorer view. The CREATE PROCEDURE statement appears in the XMLSP editor. Check for a successful deploy of the XMLSP stored procedure in the Data Output view. Figure 61. XMLSP editor In the Database Project Explorer view, select the XMLSP stored procedure under the Stored Procedures folder in the XMLBasic project. Right-click, and select the Run menu option. Figure 62. Run a stored procedure Developer Workbench and XML © Copyright IBM Corporation 1994, 2007. All rights reserved. Page 39 of 42
  40. 40. developerWorks® ibm.com/developerWorks On the Data Output view, verify that the stored procedure ran successfully. You should see Status = Success, Action = Run, and Object Name = XMLSP. In the Results tab, you should see one document returned. Click on the ellipses button to launch the source window. Select XML Tree to see a graphical view of the SampleSchema XML document stored in the XMLSAMPLE table. Figure 63. Stored Procedure Output Developer Workbench and XML Page 40 of 42 © Copyright IBM Corporation 1994, 2007. All rights reserved.
  41. 41. ibm.com/developerWorks developerWorks® Developer Workbench and XML © Copyright IBM Corporation 1994, 2007. All rights reserved. Page 41 of 42
  42. 42. developerWorks® ibm.com/developerWorks Resources Learn • developerWorks resource page for DB2 for Linux, UNIX, and Windows: Find articles and tutorials and connect to other resources to expand your DB2 skills. • developerWorks Information Management zone: Expand your skills on IBM Information Management products. • Learn about DB2 Express-C, the no-charge version of DB2 Express Edition for the community. • DB2 training sites list several learning delivery methods for the use of DB2. . • Stay current with DB2 events atvia developerWorks technical events and Webcasts. Get products and technologies • Download a free trial version ofDB2 9.1. • Now you can use DB2 for free. Download DB2 Express-C, a no-charge version of DB2 Express Edition for the community that offers the same core data features as DB2 Express Edition and provides a solid base to build and deploy applications. • Download the DB2 Developer Workbench. Discuss • Participate in the discussion forum for this content. • Participate in developerWorks blogs and get involved in the developerWorks community. About the author Debra R. Eaton Debra Eaton has worked in DB2 Technical Sales as a Software Information Technology Specialist on the DB2 Migration Team for 10 years. She specializes in converting customer applications from non-DB2 databases to DB2. She has authored IBM Redbooks, white papers, and DB2 Magazine articles on topics related to DB2 application development. In addition, she has presented these topics at IDUG and DB2 technical conferences. Developer Workbench and XML Page 42 of 42 © Copyright IBM Corporation 1994, 2007. All rights reserved.

×