290 likes | 399 Views
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.
E N D
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
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 Overview of a Data Schema
Root Element Parent Element(s) Child Element(s) Attributes MovieDatabase Genre Movie Actor Birthday Overview of a Data Schema
XML Declaration (<?xml version="1.0“…) Namespace declaration (<xsd:schema xmlns:…) Root Element Parent Elements / Child Elements / Attributes Overview of a Data Schema
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 XML Declaration
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. XML Namespaces
Purposes of Namespaces XML 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)
“xsd:schema” – Begins the ‘Schema’ “xmlns:xsd=“…” – Binds this Schema with the W3 standard elementFormDefault=“…” – XML representation attribute XML Namespaces
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
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. 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. Entities in General
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 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> 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 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. 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 The Root Element
The Root Element </xsd:sequence> </xsd:complexType> </xsd:element> Finishes the tree structure of the Root Element
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>
Parent Elements <xsd:element name="movie" type="MovieType" minOccurs="1“ maxOccurs="unbounded"/> The child element “MovieType” is why a GenreType is a parent element.
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>
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”.
AnyURI Boolean Byte Date DateTime Decimal Double Duration Float gDay gMonth gMonthDay gYear gYearMonth Attributes and Restrictions The W3 standard Schema’s many simple-type attributes • gID • IDREF • IDREFS • Int • Integer • Long • String And more
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"/> 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> Attributes and Restrictions
Attributes and Restrictions • MinExclusive • MinInclusive • Minlength • Pattern • TotalDigits • whiteSpace • Enumeration • FractionDigits • Length • MaxExclusive • MaxInclusive • MaxLength More about these restrictions if you search Google for xml restriction reference
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!
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
Assignment • Draw out a data schema with 4 entities following the tree structure and include attributes for each entity. • Create a well-formed data schema for the model you drew. • 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