creating data schemas

29
Creating Data Schemas Presentation by Chad Borer 2/6/2006

Upload: claus

Post on 06-Jan-2016

49 views

Category:

Documents


0 download

DESCRIPTION

Creating Data Schemas. Presentation by Chad Borer 2/6/2006. To be covered. Overview of the data schema Starting your Schema the right way The Root Element Parent Elements Child Elements Attributes and Restrictions Ending your Schema the right way. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Creating Data Schemas

Creating Data Schemas

Presentation by Chad Borer

2/6/2006

Page 2: Creating Data Schemas

To be covered

Overview of the data schema Starting your Schema the right way The Root Element Parent Elements Child Elements Attributes and Restrictions Ending your Schema the right way

Page 3: Creating Data Schemas

Overview of a Data Schema

An XML Schema is simply a tree structure defining a relational database

As far as functionality goes, it is much like a relational data model

In each, one can find descriptions of each object in the database as well as the relations between them. Also, both place restrictions of the data that is entered

Page 4: Creating Data Schemas

Overview of a Data Schema

Root Element

Parent Element(s)

Child Element(s)

Attributes

MovieDatabase

Genre

Movie

Actor

Birthday

Page 5: Creating Data Schemas

Overview of a Data Schema

XML Declaration

(<?xml version="1.0“…)

Namespace declaration

(<xsd:schema xmlns:…)

Root Element

Parent Elements / Child Elements / Attributes

Page 6: Creating Data Schemas

XML Declaration

Always this one line on every data schema:

<?xml version="1.0" encoding="UTF-8"?>

* Because it is always the same, you can always copy this from an existing schema

Page 7: Creating Data Schemas

XML Namespaces

The Namespace of a schema consists of the lines of code that come after the XML Declaration and before the actual data structure.

They’re the ones you have no idea how to write unless you copy them, and they look like they make absolutely no sense.

Page 8: Creating Data Schemas

XML Namespaces

Purposes of Namespaces To link the Schema to a standard put out by the

W3 Group To declare each Schema as its own when more

than one data Schema is used by an XML file To force the XML file pointing to the Schema to

declare each Namespace in the Schema within itself (confusing, but not necessary to understand right now)

Page 9: Creating Data Schemas

XML Namespaces

“xsd:schema” – Begins the ‘Schema’

“xmlns:xsd=“…” – Binds this Schema with the W3 standard

elementFormDefault=“…” – XML representation attribute

Page 10: Creating Data Schemas

Entities in General

Simple v. Complex Types

If an entity only has one value associated with it, it is said to be a simple type, otherwise it is known as a complex type.

Not the same as Parent and Child elements

Page 11: Creating Data Schemas

Entities in General

Here is the general structure of a complextype entity in an XML Schema:

<xsd:complexType name=“EntityType"><xsd:sequence>

attributes and/or child elements</xsd:sequence>

</xsd:complexType>

The exception is the Root Element.

Page 12: Creating Data Schemas

Entities in General

Don’t confuse the name of the entity and its type. When a child element is included in the description of its parents element, it is given a name and a type associated with it. It is important to distinguish the two apart especially when dealing with stylesheets. You will see this exemplified in the slide after next.

Page 13: Creating Data Schemas

The Root Element

The root element is the object under which all the other ones can be grouped. For example, if you wanted to write a Schema for a movie theater, the movie theater itself is the root element.

If you wanted to write a Schema for a bunch of cars, you would need to find a root element such as Manufacturer or Dealership

Page 14: Creating Data Schemas

The Root Element

<xsd:element name="MovieDatabase">

<xsd:complexType>

<xsd:sequence>

<xsd:element name="Genre" type="GenreType“

minOccurs="1" maxOccurs="unbounded"/>

</xsd:sequence>

</xsd:complexType>

</xsd:element>

Page 15: Creating Data Schemas

The Root Element

<xsd:element name="MovieDatabase">

There can only be one element defined for a Schema and this is it. The name of the element is contained within the quotation marks. All references in XML files or Stylesheets will use this name

Page 16: Creating Data Schemas

The Root Element

<xsd:complexType>

<xsd:sequence>

Just following the tree structure of the Schema. The Root Element is a complexType because it has child elements.

Page 17: Creating Data Schemas

The Root Element

<xsd:element name="Genre" type="GenreType“

minOccurs="1" maxOccurs="unbounded"/>

This is the First parent element in the Schema. The element’s name is “Genre” and all references will go to “Genre”. It is of the GenreType type which assumes the GenreType is defined later in the Schema

Page 18: Creating Data Schemas

The Root Element

</xsd:sequence>

</xsd:complexType>

</xsd:element>

Finishes the tree structure of the Root Element

Page 19: Creating Data Schemas

Parent Elements

<xsd:complexType name="GenreType"> <xsd:sequence> <xsd:element name="name" type="xsd:string"/> <xsd:element name="description"

type="xsd:string"/> <xsd:element name="movie" type="MovieType"

minOccurs="1“ maxOccurs="unbounded"/> </xsd:sequence></xsd:complexType>

Page 20: Creating Data Schemas

Parent Elements

<xsd:element name="movie" type="MovieType" minOccurs="1“ maxOccurs="unbounded"/>

The child element “MovieType” is why a GenreType is a parent element.

Page 21: Creating Data Schemas

Child Elements

<xsd:complexType name="ActorType">

<xsd:sequence>

<xsd:element name="lname" type="xsd:string"/>

<xsd:element name="fname" type="xsd:string"/>

</xsd:sequence>

</xsd:complexType>

Page 22: Creating Data Schemas

Attributes and Restrictions

An attribute is an entity of a simple type, meaning it had only one value.

<xsd:element name="lname" type="xsd:string"/>

This particular attribute is of type “string” derived from the W3 group’s standard Schema as indicated by “xsd:string”.

Page 23: Creating Data Schemas

Attributes and Restrictions

AnyURI Boolean Byte Date DateTime Decimal Double

Duration Float gDay gMonth gMonthDay gYear gYearMonth

The W3 standard Schema’s many simple-type attributes

gID IDREF IDREFS Int Integer Long String

And more

Page 24: Creating Data Schemas

Attributes and Restrictions

Restrictions on simpletypes are common and can be very helpful in making sure data integrity stays high. This child element is listed in ActorType and refers to the type “ssnType” which is not included in the W3 standard Schema. Although you cannot see it here, it is based on a “String”

<xsd:element name="ssn" type="ssnType"/>

Page 25: Creating Data Schemas

Attributes and Restrictions

When placing a restriction on a simple type, a whole new simple type must be created.

<xsd:simpleType name="ssnType">

<xsd:restriction base="xsd:string">

<xsd:pattern value="\d{3}-\d{2}-\d{4}"/>

</xsd:restriction>

</xsd:simpleType>

Page 26: Creating Data Schemas

Attributes and Restrictions

Enumeration FractionDigits Length MaxExclusive MaxInclusive MaxLength

MinExclusive MinInclusive Minlength Pattern TotalDigits whiteSpace

More about these restrictions if you search Google for xml restriction reference

Page 27: Creating Data Schemas

Ending your Schema

Go back through your Schema and make sure each line of all your elements has been closed with either a “/>” at the end of the line, or a </….> at the end of the coding block.

Last thing to include is </xsd:schema> You’re done!

Page 28: Creating Data Schemas

Questions?

Overview of the data schema Starting your Schema the right way The Root Element Parent Elements Child Elements Attributes and Restrictions Ending your Schema the right way

Page 29: Creating Data Schemas

Assignment

A. Draw out a data schema with 4 entities following the tree structure and include attributes for each entity.

B. Create a well-formed data schema for the model you drew.

C. Add 3 restrictions to any of the entities created in your schema. Good ones would include enumeration, pattern, and one of the min/max restrictions