Schema Constraints

XML documents can optionally be validated against a schema to enforce document similarity. Most databases support schema constraints, but BDB XML has the unique ability to store collections of data with schemas that vary from document to document if desired. This is an added level of functionality not commonly found in XML databases.

Recall our phonebook example. The documents for that example had the following structure:

<phonebook>
    <name>
        <first>Tom</first>
        <last>Jones</last>
    </name>   
    <phone type="home">420-203-2032</phone>
</phonebook>

Three things are required to validate this document within BDB XML. First, a schema is required. Because the subject of XML schemas are well beyond the scope of this document, we simply provide one for you here. There are many excellent books and tutorial web sites on the subject, and we suggest you review some of that material if you are not familiar with XML schemas.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="phonebook">
      <xs:complexType>
        <xs:sequence>
            <xs:element name="name" minOccurs="1" maxOccurs="1">
              <xs:complexType>
                <xs:sequence>
                    <xs:element name="first" type="xs:string"/>
                    <xs:element name="last" type="xs:string"/>            
                </xs:sequence>
              </xs:complexType>
            </xs:element>
            <xs:element name="phone" minOccurs="0" maxOccurs="unbounded">
              <xs:complexType>
                <xs:simpleContent>
                  <xs:extension base="xs:string">
                    <xs:attribute name="type" type="xs:string"/>
                  </xs:extension>
                </xs:simpleContent>
              </xs:complexType>
            </xs:element>
        </xs:sequence>        
      </xs:complexType>
    </xs:element>
</xs:schema>

Suppose this schema is available from a web-server at:

http://www.oracle.com/fakeschema.xsd

Second, we need to create a container with validation enabled.

dbxml> createContainer validate.dbxml d validate
Creating document storage container, with validation

Third, we need to attach the schema to a document and insert it into the container.

dbxml> putDocument phone1 '
<phonebook xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation=
        "http://www.oracle.com/fakeschema.xsd">
    <name>
        <first>Tom</first>
        <last>Jones</last>
    </name>   
    <phone type="home">420-203-2032</phone>
</phonebook>' s

Document added, name = phone1

That document was successfully added because it conforms to the schema. Now, try to add an invalid document.

dbxml> putDocument phone2 '
<phonebook xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation=
        "http://www.oracle.com/fakeschema.xsd">
    <name>
        <first>Tom</first>
        <last>Jones</last>
    </name> 
    <phone type="home">420-203-2032</phone>
    <cell-phone>430-201-2033</cell-phone>
</phonebook>' s

stdin:67: putDocument failed, Error: XML Indexer:  Parse error in 
document at line, 10, char 17. Parser message: Unknown element 
'cell-phone']]>

Since the schema doesn't define the cell-phone element and we have schema validation enabled, BDB XML won't allow the document to be added to the container.

XML schemas provide a powerful tool for constraining the structure and content of XML documents.