Creating an XML Schema Definition (XSD) document with the DB2 tools for Visual Studio 2005

Tuesday Oct 24th 2006 by Paul Zikopoulos

Learn how to leverage the Visual Studio 2005 XML tools to create an XML Schema Definition (XSD) document for subsequent registration into the DB2 9 XML Schema Repository (XSR).

In a previous series of articles, I showed you the integration features between the IBM DB2 Universal Database for Linux, UNIX, and Windows Version 8.x (DB2 UDB) product and the Microsoft Visual Studio.NET 2003 integrated development environment (IDE).

In early June 2006, IBM announced the next release of the DB2 product, DB2 9. Part of this announcement included the support for Microsoft Visual Studio 2005 and its accompanying ADO.NET 2.0 driver.

As you may recall, Microsoft announced the Visual Studio 2005 product, along with SQL Server 2005, in late 2005. Around the same time, IBM delivered a beta of the now officially announced DB2 integration into Microsoft Visual Studio 2005 and ADO.NET 2.0.

The product level of this code became generally available when DB2 9 made its debut in August 2006, and you can download it at: https://www14.software.ibm.com/webapp/iwm/web/preLogin.do?source=dbaddvs&S_TACT=105AGX11&S_CMP=TILE=.

For this article, I recommend that you download the free DB2 Express-C product, which includes the pureXML feature, so you can follow the steps outlined in this article. You can download your own free no-database-size-limit copy of DB2 9 (which also includes 64-bit support) at: http://www-306.ibm.com/software/data/db2/express/download.html .

As I’ve been writing about the DB2 integration into Visual Studio 2005, I’ve discussed the following topics in other articles:

In this article, I want to show you how you can leverage the Visual Studio 2005 XML tools to create an XML Schema Definition (XSD) document for subsequent registration into the DB2 9 XML Schema Repository (XSR) that’s part of the DB2 pureXML feature.

The steps in this article will show you how to create an XSD document that defines a complex data type (called CanadianAddress), which can be used in an XML document. Another XSD document will be created that includes the CanadianAddress XSD and optionally used to subsequently validate XML data in your DB2 9 database. Subsequent articles will build on this XSD and use it to perform various tasks like validation on insert, shredding, and so on - all from the Visual Studio IDE!

Creating an XSD in Visual Studio 2005

Visual Studio 2005 comes with a rich set of XML features so it’s great to know that the productivity delivered by these tools can be leveraged with DB2 9.

You can create data types for your XML document from within an XSD and have that data validated by DB2 9 as well.

To create an XML data type within an XSD, perform the following steps:

1.  Open a new file for editing by clicking New>File, and select XML Schema from the General tab:

2.  The XML Schema editor opens. Right-click in the middle of the designer pane, and select Add>New complexType:

Notice the IBM DB2 - Add to XML Schema Repository option from the pop-up menu? This feature gives you the ability to quickly and easily register an XSD in the DB2 9 XSR – more on that in a future article in this series.

3.  The XSD editor opens. Create a complex data type that will be used for Canadian-based addresses in your XML documents by performing the following steps:

a.   Change the name of the data type to CanadianAddress so that when you hover over the XSD editor it looks like this:

b.  Click-in the first empty cell, denoted with an asterisk (), and create an element called Address. (An appears beside the element name to identify it as an element.) Assign a String data type to it by clicking the drop-down arrow in the corresponding field and selecting string:

Note: The default action when you enter new rows in the XSD editor is to define those rows as elements. To define this manually, click the drop-down arrow in the box below the CT (for Complex Type) field in the XSD header, and select element from the drop-down list:

c.   Add the following elements to the complex CanadianAddress data type:

  • City
  • Province
  • Postal Code
  • Country

The XSD editor should now look like this:

When you are working with the XSD editor:

  • The asterisk () indicates that it is a new row.
  • The pencil () indicates that you are editing a current row.
  • The pointer () indicates the row that you currently have focus on for editing.

You can see the XSD-compliant XML code that is generated ‘behind-the-scenes’ by right-clicking the XSD editor and selecting View Code:

4.  If you look at the top of the XSD editors, you will notice asterisks appended to the name of each tab. This indicates that these pages contain unsaved changes:

Save your changes as CanadianAddress.xsd and then close the editor (by clicking ), as shown below:

5.  Create another XSD file that will use the complex CanadianAddress data type by performing the following steps:

a.   Create a new XSD file by pressing New>File as you did in Step 1.

b.  The XML Schema editor opens. Right-click in the middle of the designer, only this time select Add>New element (instead of New complexType as you did in Step 2):

c.   In the same manner in which you added the Address element for the CanadianAddress XSD in Step 3, add a Name element to the first field and associate it with a string data type.

d.  In the second row, add an Address element and manually type in the data type name CanadianAddress beside it.

When you have completed both Steps 5c and 5d, the XSD editor should look this:

6.  Associate the CustomerAddress XSD (which defines the CanadianAddress complex data type) with this new XSD as follows:

a.   Click the gray area (in the previous figure) of the Customer XSD editor.

b.  Ensure that the Properties window displays <xs:schema> using the drop-down control:

If you can’t see the Properties window, press F4 after Step 6a.

c.   Click the ellipsis (...) beside the include field in the Properties window.

The Xmlinclude collection window opens.

d.  Click the Add button.

e.   Enter CanadianAddress.xsd for both the id and schemaLocation fields in the <xs:include> Properties window, and click Close:

f.    Save this XML Schema Definition document as Customer.xsd in the same location (and by using the same method) that you used to save the CanadianAddress XSD in Step 4.

g.   After you’ve saved the Customer XSD file, click View>Refresh to refresh the view of the designer palette.

When you associate the CanadianAddress XSD with the Customer XSD, Visual Studio 2005 automatically associates the two, as shown below:

This is a very useful and informative feature because it lets you quickly see what data types are included in the complex CanadianAddress data type.

If you find your IDE getting too ‘busy’, you can collapse this association by clicking the toggle ():

You verify that the Customer XSD includes the CanadianAddress XSD, which defines the CanadianAddress complex data type, by looking for the INCLUDE function in the code (it’s on the third line):


Wrapping it Up...

In this article, I showed you how to use Visual Studio 2005 to create a somewhat complex XML Schema Definition document that leverages a collection of XSDs: one that will be used for the purpose of validating or decomposing an XML document, while the other defines a complex data type that is used in the XML document

In my next article, I’ll show you how to register this XSD with the XSR that’s built into the DB2 9 data server. I’ll also discuss some of the unique capabilities DB2 9 offers XML developers for XML Schema management.

» See All Articles by Columnist Paul C. Zikopoulos

About the Author

Paul C. Zikopoulos, BA, MBA, is an award-winning writer and speaker with the IBM Database Competitive Technology team. He has more than ten years of experience with DB2 and has written over sixty magazine articles and several books about it. Paul has co-authored the books: DB2 9 New Features, DB2 Version 8: The Official Guide, DB2: The Complete Reference, DB2 Fundamentals Certification for Dummies, DB2 for Dummies, and A DBA's Guide to Databases on Linux. Paul is a DB2 Certified Advanced Technical Expert (DRDA and Cluster/EEE) and a DB2 Certified Solutions Expert (Business Intelligence and Database Administration). In his spare time, he enjoys all sorts of sporting activities, running with his dog Chachi, and trying to figure out the world according to Chloë – his new daughter. You can reach him at: paulz_ibm@msn.com.


IBM, DB2, DB2 Universal Database and PureXML are trademarks or registered trademarks of International Business Machines Corporation in the United States, other countries, or both.

Microsoft and Windows are trademarks of Microsoft Corporation in the United States, other countries, or both.

UNIX is a registered trademark of The Open Group in the United States and other countries.

Linux is a registered trademark of Linus Torvalds in the United States, other countries, or both.

Other company, product, and service names may be trademarks or service marks of others.

© Copyright International Business Machines Corporation, 2006. All rights reserved.


The opinions, solutions, and advice in this article are from the author’s experiences and are not intended to represent official communication from IBM or an endorsement of any products listed within. Neither the author nor IBM is liable for any of the contents in this article. The accuracy of the information in this article is based on the author’s knowledge at the time of writing.

Mobile Site | Full Site