XML-Data
W3C Note January 5, 1998
- XML-Data was the first in a series of papers to describe a method for creating XML data schema.
- An XML data schema allows you to define constraints on the structure of an XML document.
Presentation Outline
· Why do we need XML Data Schema? (Isn’t a DTD good enough?)
· How XML Data Schemas are Used
· Features of XML Data Schema
· Summary
Why do we need XML Data Schema?
Schema Definition in SQL:
create table account
(account-number char(10) not null,
branch-name char(15),
balance integer,
primary key (account-number),
foreign key (branch-name) references branch,
check (balance >=0))
A DTD that is not quite equivalent to the above schema
<!DOCTYPE account-table [
<!ELEMENT account-table (account*)>
<!ELEMENT account (balance)>
<!ELEMENT balance (#PCDATA)>
<!ATTLIST account account-number ID #REQUIRED
branch-name IDREF #IMPLIED>
]>
Why do we need XML Data Schema?
“Equivalent” XML Data Schema
<schema xmlns=’http://www.w3.org/1999/XMLSchema’>
<element name=’account-table’ type=’accounts’/>
<type name=’accounts’>
<element name=’account’ minOccurs=’0’ maxOccurs=’*’>
<type>
<element name=’account-number’ minOccurs=’1’>
<datatype source=’string’>
<length value=’10’/>
</datatype>
</element>
<element name=’branch-name’ type=’string’>
<datatype source=’string’>
<length value=’15’/>
</datatype>
</element>
<element name=’balance’>
<datatype source=’integer’>
<minInclusive value=’0’/>
</datatype>
</type>
</element>
</type>
</schema>
Features of XML Data Schema
· Many primitive datatypes are available and new datatypes can be defined
<element name="shipTo" type="Address" />
<type name="Address" >
<element name="name" type="string" />
<element name="street" type="string" />
<element name="city" type="string" />
<element name="state" type="string" />
<element name="zip" type="integer" />
<attribute name="type" type="string" />
</type>
<shipTo type="US">
<name>John Doe</name>
<street>123 Maple Street</street>
<city>Ann Arbor</city>
<state>MI</state>
<zip>48109</zip>
</shipTo>
Features of XML Data Schema
· Attribute group definitions provide a construct to replace some uses of parameter entities.
<attributeGroup name="myAttrGroup">
<attribute .../>
...
</attributeGroup>
<type name="myelement" content="empty">
<attributeGroup ref="myAttrGroup"/>
</type>
Features of XML Data Schema
· New element types can be defined by adding additional content to the content model of another type definition
<type name="personName">
<element name="title" minOccurs="0"/>
<element name="forename" minOccurs="0" maxOccurs="*"/>
<element name="surname"/>
</type>
<type name="extendedName" source="personName" derivedBy="extension">
<element name="generation" minOccurs="0"/>
</type>
<element name="addressee" type="extendedName"/>
<addressee>
<forename>John</forename>
<forename>Doe</forename>
<surname>Doe</surname>
<generation>Jr</generation>
</addressee>
Features of XML Data Schema
· New element types can be defined by adding restrictions or eliminating content from another type definition
<type name="personName">
<element name="title" minOccurs="0"/>
<element name="forename" minOccurs="0" maxOccurs="*"/>
<element name="surname"/>
</type>
<type name="simpleName" source="personName" derivedBy="restriction">
<restrictions>
<element name="title" maxOccurs="0"/>
<element name="forename" minOccurs="1" maxOccurs="1"/>
</restrictions>
</type>
<element name="who" type="simpleName"/>
<who>
<forename>John</forename>
<surname>Doe</surname>
</who>
Features of XML Data Schema
· Using “unique” and “key” constraints, the capabilities of XML’s ID/IDREF mechanism have been extended
Element content and combinations of attribute values and content can be declared to be unique
Combinations of attribute values and/or element content can be declared to be keys (not only unique, but always present)
Constraints can have document-wide scope or hold within the scope of particular elements
Features of XML Data Schema
<element name="state">
<type>
<element name="stateCode" type="twoLetterCode"/>
<element name="vehicle">
<type>
. . .
<attribute name="regNo" type="integer"/>
</type>
</element>
. . .
</type>
</element>
<key name="regKey">
<selector>.//vehicle[@regNo]</selector>
<field>@regNo</field>
<field>ancestor::state/stateCode</field>
</key>
How XML Data Schema are Used
Sample XML Data Schema
<!DOCTYPE schema
SYSTEM 'http://www.w3.org/TR/1999/WD-xmlschema-1-19991217/schema.dtd' >
<schema xmlns="http://www.w3.org/1999/XMLSchema">
<element name="family" type="Set-of-People"/>
<type name="Set-of-People">
<element name="person" type="name-and-age"
minOccurs=’0’ maxOccurs=’*’/>
</type>
<type name="name-and-age">
<element name="full-name" type=’string’/>
<element name="age">
<datatype source=’integer’>
<minInclusive value=’0’/>
</datatype>
</element>
</type>
</schema>
How XML Data Schema are Used
Sample XML Document that Conforms to the Schema
<family xmlns="http://www.family.org/myfamily">
<person>
<full-name>John Doe</full-name>
<age>50</age>
</person>
<person>
<full-name>Jane Doe</full-name>
<age>50</age>
</person>
</family>
How XML Data Schema are Used
· An XML data schema is expressed in XML instance syntax
· An XML data schema must be valid with respect to a schema DTD
· An XML document can identify a schema to which it is supposed to conform
· An XML document is schema-valid with respect to a schema if it meets the structural and datatyping constraints expressed in the schema
· The XML document processor must be schema-aware to determine if a document is schema-valid
Summary
· XML Data Schemas provide a powerful mechanism for defining datatypes and constraints on document structure
· XML Data Schemas are expressed in XML instance syntax
· The XML document processor must be schema-aware to determine if a document is valid with respect to a schema