DB2 9: Creating database objects & Storing validated XML data

I’m currently developing a Industry Format bundle based on XML standard – METS. To do such thing I had to learn some basic but still powerful features of DB2 9. At the moment my bundle only registers some XML schemas and use them to validate the insertion of some XML samples.

I’ll keep my promise to teach everything I learn, then I will teach three things:

1. register XML schemas

2. insert XML documents without validation

3. validate XML documents insertion

XML

Registering XML schemas

If you dont know what schema registration is, you can understand it as “give my DBMS the ability to verify XML structure correctness against a specified XML schema”. By the way, you can easily create your own schemas to your XML documents with an IBM tool named WebSphere Studio. All you have to do is load the XML sample and select Generate -> XML Schema.

I’ll assume that you read the Express-C: Starting tutorial so that I can use the test.items as my table example.

Assume that you have a XML schema in schemapath named xschema.xsd and remember that XML schemas are only XML documents that dictate the sctructure other XML documents.

So, to registrate it you should run in the CLP:

register xmlschema ‘xschema.xsd’ from ’schemapath/xschema.xsd’ test.xschmema complete;

…or from your favorite shell:

db2 “register xmlschema ‘xschema.xsd’ from ’schemapath/xschema.xsd’ test.xschmema complete;”

This should work, however there is something you should know. When a XML schema becomes too large it’s good practice to separate it in several files. In those cases the schema registration becomes quite different.

Assume that xschema.xsd was divided in two XML schemasxschema1.xsd and xschema2.xsd. The schema registration should be like this:

register xmlschema ‘xschema1.xsd’ from ’schemapath/xschema1.xsd’ test.xschema
add xmlschema document test.xschema add ‘xschema2l.xsd’ from ’schemas/xschema2.xsd’
complete xmlschema teste.xschema

Inserting XML documents

You can insert XML documents in a given table as you insert a string or an integer:

insert into items values (96, ‘João Moura’,’student ambassador‘)

However in most cases it’s kind of difficult to insert the XML document in one single line or one by one, so we use a simple script to specify a list of XML documents and eventually their schema. Here is an example saved in “script.del”:

1,”João Moura”,<xds fil=”joaoinfo.xml” sch=”xschema.xsd”>
2,”Quim Barreiros”,<xds fil=”quiminfo.xml” sch=”xschema.xsd”>
3,”Nuno Veloso”,<xds fil=”nunoinfo.xml”>
4,”João Ferreira”, </xds></xds></xds>

You can say the values to insert and possibly omit some. Although XDS (XML Data Specifier) tag syntax is self-explanatory its worth noting:

<XDS ../> – the tag itself
FIL=’ ‘ – the name of XML document
SCH=” – the XML schema describing the document structure

And then use it to import several entries in using only one line:

import from script.del of del xml from ‘dirofxmldocuments’
insert into test.items

All that it does is execute sequential insert’s reading the output from script’s rows.

Validating XML documents

I’m tired of writing so i’ll be quick:

import from script.del of del xml from ‘dirofxmldocuments’
xmlvalidate using xds
insert into test.items

Conclusion

This is all I can give you for now but be aware of more tutorials and remember:

The more knowledge you share with others the more knowledge will be shared with you.

Hope you enjoy.

One Response to “DB2 9: Creating database objects & Storing validated XML data”

  1. METS Standard with IBM DB2 Express C using XForms as user-interface and Ruby on Rails as a Rest Web-services « .$null@dscape/08 Says:

    [...] Creating database objects & Storing validated XML data [...]

Leave a Reply