pureXML: Using XML in DB2 for z/OS
XML is gaining popularity for persisting complex data and is frequently used in web-enabled applications and as a means of data transmission. This chapter introduces you to the basics of XML and provides an overview of pureXML, IBM’s implementation of XML support embedded in DB2.
A comprehensive treatment of DB2 pureXML would require a book length treatment, and it is not the intent of this book, or this chapter, to provide an exhaustive treatment of DB2’s support for XML. If you are looking for an introduction, this chapter is a good starting place. References for additional pureXML research are provided at the end of the chapter.
What Is XML?
XML stands for Extensible Markup Language. You may be familiar with HTML, the markup language used to create web pages. Like HTML, XML is based upon Standard Generalized Markup Language (SGML). SGML is a language for defining markup languages that was developed and standardized by the International Organization for Standardization (ISO).
Whereas HTML uses tags to describe how data appears on a web page, XML is designed to transport and store data. In other words, XML uses tags to describe the what—that is, the data itself. XML retains the key SGML advantage of self-description, while avoiding the complexity of full-blown SGML. XML allows tags to be defined by users that describe the data stored in the document. This capability gives users a means for describing the structure and nature of the data in the document. In essence, the document becomes self-describing.
The simple syntax of XML makes it easy to process by machine while remaining understandable to people. Again, use HTML as a metaphor to help you understand XML. HTML uses tags to describe the appearance of data on a page. For example the tag, “text”, would specify that the “text” data should appear in bold face. XML uses tags to describe the data itself, instead of its appearance. For example, consider the following XML describing a customer address:
<CUSTOMER> <first_name>Craig</first_name> <middle_initial>S.</middle_initial> <last_name>Mullins</last_name> <company_name>Mullins Consulting, Inc.</company_name> <street_address>15 Coventry Ct.</street_address> <city>Sugar Land</city> <state>TX</state> <zip_code>77479</zip_code> <country>USA</country> </CUSTOMER>
XML is actually a meta-language—that is, a language for defining other markup languages. These languages are collected in dictionaries called Document Type Definitions (DTDs). The DTD stores definitions of tags for specific industries or fields of knowledge. So, the meaning of a tag must be defined in a “document type declaration” (DTD), such as the following:
<!DOCTYPE CUSTOMER [ <!ELEMENT CUSTOMER (first_name, middle_initial, last_name, street_address, city, state, zip_code, country)> <!ELEMENT first_name (#PCDATA)> <!ELEMENT middle_initial (#PCDATA)> <!ELEMENT last_name (#PCDATA)> <!ELEMENT street_address (#PCDATA)> <!ELEMENT city (#PCDATA)> <!ELEMENT state (#PCDATA)> <!ELEMENT zip_code (#PCDATA)> <!ELEMENT country (#PCDATA)> ]
The DTD for an XML document can either be part of the document or stored in an external file. The XML code samples shown are meant to be examples only. By examining them, you can quickly see how the document describes its contents.
For data management professionals, this is a plus because it eliminates the trouble to track down the meaning of data elements. One of the biggest problems associated with database management and processing is finding and maintaining the meaning of stored data. If the data can be stored in documents using XML, the documents themselves will describe their data content. Of course, the DTD is a rudimentary vehicle for defining data semantics.
More recently, the XML Schema has been introduced to describe the structure of an XML document. An XML Schema has better support for applications, document structure, attributes, and data-typing. For example, here is the previous DTD transformed into an XML Schema:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="customer"> <xs:complexType> <xs:sequence> <xs:element name="first_name" type="xs:string"/> <xs:element name="middle_initial" type="xs:string"/> <xs:element name="last_name" type="xs:string"/> </xs:sequence> </xs:complexType> <xs:complexType name="USAddress"> <xs:sequence> <xs:element name="street" type="xs:string"/> <xs:element name="city"type="xs:string"/> <xs:element name="state"type="xs:string"/> <xs:element name="zip"type="xs:decimal"/> </xs:sequence> <xs:attribute name="country" type="xs:NMTOKEN" fixed="US"/> </xs:complexType> </xs:element> </xs:schema>
Given the benefits of XML Schema over DTDs, more XML documents are adopting them for use in modern XML applications.
The important thing to remember about XML is that it solves a different problem than HTML. HTML is a markup language, but XML is a meta-language. In other words, XML is a language that generates other kinds of languages. The idea is to use XML to generate a language specifically tailored to each requirement you encounter. In short, XML enables designers to create their own customized tags, thereby enabling the definition, transmission, validation, and interpretation of data between applications and between organizations. So, the most important reason to learn XML is that it is quickly becoming the de facto standard for application interfaces.
There are, however, some issues with XML, the most troubling of which is market hype. There is plenty of confusion surrounding XML. Some believe that XML provides metadata where none currently exists, or that XML replaces SQL as a data access method for relational data. Neither of these assertions is true.
There is no way that any technology, XML included, can conjure up information that does not exist. People must create the metadata tags in XML for the data to be described. XML enables self-describing documents; it doesn’t describe your data for you.
Moreover, XML doesn’t perform the same functions as SQL. As a result, XML can’t replace it. As the standard access method for relational data, SQL DML is used to “telling” a relational DBMS what data is to be retrieved. XML, on the other hand, is a document description language that describes the basic contents of data. An XML schema can be associated with an XML document to type XML data. XML might be useful for defining databases but not for accessing them.
DB2, as well as most of the other popular DBMS products, now provides built-in support for native XML. By integrating XML into DB2 databases, you can more directly and quickly access the XML documents, as well as search and store entire XML documents using SQL. Integration can involve simply storing XML in a large VARCHAR or CLOB column, breaking down the XML into multiple columns in one or more DB2 tables, or more commonly to store XML data natively within a column of a DB2 table. As of DB2 V9, you can store XML data natively in a DB2 using pureXML, which allows you to define columns with a data type of XML.
Storing Data Relationally Versus as XML
Before delving into a discussion of how to store and access XML data natively in DB2 for z/OS, first take a moment to contrast the traditional row and column data of DB2 (and other relational database systems) versus XML data.
The self-describing data format of XML enables complex data to be stored in a single document without giving up the ability to query, search, or aggregate the data. And the XML definition (DTD or XML schema) can be modified without requiring any changes to the database schema. Of course, this may be viewed as a pro by some and a con by others. Developers will likely enjoy the added flexibility, whereas DBAs will likely lament the lack of control.
The flexible nature of XML can require more resources (CPU and I/O) to examine and interpret the XML data as opposed to accessing the same data in a traditional row and column format. But the complexity of the schema must be taken into consideration.
XML is often more suitable for applications with complex and variable data structures, and for combining structured and unstructured information. Relational row and column data is most suitable for stable data structures. A complex hierarchy stored in XML, for example, may be queried more efficiently than a similar hierarchy stored in traditional DB2 form. Relational data, though, can offer more query flexibility and optimization choices.
Keeping an XML document intact in an XML column has the advantage of maximum flexibility but can negatively impact performance.
Fully shredding an XML document into a relational format has the advantage of making high volume transactional processes run faster. But shredding has several disadvantages, as well: Converting the XML documents to the equivalent relational model can consume a lot of resources; converting relational data back into an XML document is expensive; and it can be difficult to keep up with changing requirements.
A hybrid approach in which some portions of the document are maintained as XML and other portions are shredded into relational can be a best practice approach both for performance and maintainability.
Objects having sparse attributes are another area in which XML offers an advantage over a traditional relational data format. When a large number of attributes are possible but most are not used by every instance, XML may be a better choice because every attribute would need to be defined and stored with traditional relational data. In relational data, columns might be NULL, but in XML those data items are just not present.
Consider, for example, a product catalog where each product may have a different number of attributes: size, color, weight, length, height, material, style, watts, voltage, resolution, and so on, depending upon the product. A relational approach with one column per attribute would require a large number of columns requiring NULL, which is not ideal. Alternative approaches, such as a table per product or a three-column table that stores name/value pairs for each product ID are equally unappealing. With an XML solution, elements and attributes can be optional, so they would be omitted when they do not apply for a specific product.
Often there are XML schemas already defined by some industry standard organization (for example, ISO). Using those schemas can greatly reduce the time and effort for design and data modeling. Also these schemata are often used for defining the data received or sent by an application. It is often desirable to store XML data redundantly (which is against good relational design) in a relational column or even a subset of the document in an XML column. If schema validation, a relatively costly process, can be done once, then portions of the XML document can be used in other tables without revalidating.
Parsing and serializing XML data also can be a costly process. If XML data is required to be passed as output, it can be beneficial to store those portions of the document redundantly as XML documents. Using some hybrid design approach combining relational and XML can be useful: Highly referenced fields are best in relational columns, whereas sparsely populated or seldom-referenced fields may be better left in XML columns.
At any rate, XML data has its place, and DB2 users are lucky that they can store XML data natively within DB2 databases.