Modeling Elements and Attributes
R. Alexander Milowski
milowski@sims.berkeley.edu
School of Information Management and Systems
#1
Premise
You've got a single document and namespace.
You want to write a schema that constrains this document.
You just want the elements and attributes in the right place.
And maybe a bit of "leaf typing" of element and attribute values (e.g. dates should be dates).
#2
Creating Schema Document
An XML Schema is an XML document:
Namespace:
http://www.w3.org/2001/XMLSchema
Document Element:
schema
The attribute 'targetNamespace' specifies the namespace that the schema defines.
Example:
<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' targetNamespace='urn:publicid:IDN+cde.berkeley.edu:...'> ... </xs:schema>
NOTE: This slides will always use 'xs' for the prefix of the schema namespace.
#3
Target Namespace
The target namespace is your namespace.
It can be any absolute URI.
You can have multiple schemas for the same target namespace.
But you can only use one schema definition of a namespace during validation.
#4
Schema Documents vs Schemas
A schema document is the XML syntax of a schema.
You can have multiple documents (modules) for one schema.
The schema's defintions are the result of processing all the schema documents for all namespaces used.
#5
Top Level Elements
The top-level elements are the children of the 'schema' element:
include, import, redefine, annotation
element, attribute
simpleType, complexType, group, attributeGroup, notation
These must occur first: include, import, redefine.
Otherwise, any order goes.
The schema element can be empty too.
#6
Declaration vs Definition
This is just terminology use in XML Schema.
Declaration:
A declaration is something used in a document (e.g. element and attributes).
Definitions:
A definition is something used only by the schema.
#7
Global Names
Schemas have "global" names that are the only starting place.
Type can only come from elements at the top-level.
Each "global" name is associated with a declaration or definition type:
elements
attributes
complex & simple types
groups
attribute groups
notations
That is, a global name in a schema is a pair of a local-name and kind of object.
#8
Simple vs Complex Types
Simple types are types for values:
numbers: integers, floats, doubles
strings, tokens, names, language codes
dates, times, etc.
Complex types are element structures.
content models
attributes
#9
Predefined Simple Types
#10
Simple Element Declarations
This declares an element of a particular simple type:
<xs:element name="person" type="xs:string"/> <xs:element name="pubdate" type="xs:date"/>
The 'name' attribute defines the local name value in the target namespace.
The 'type' attribute points to a defined simple type.
You can point to your own simple types too.
#11
Declaring Elements with Structure
When elements have structure, you need to define the structure.
The structure is defined by a 'complexType' element:
<xs:element name="person"> <xs:complexType> ... </xs:complexType> </xs:element>
The content of 'complexType' defines the structure of the element.
#12
Element Children
An element's content is defined by combining:
'sequence' element - a sequence of children elements.
'choice' element - a choice of children elements.
'element' element - a specification of a child element.
'all' element - any ordering of child elements.
'any' element - any elements from a particular namespace.
This lecture will cover 'sequence', 'choice', and 'element'.
Keep in mind that 'any' and 'element' cannot be used as a direct child of 'complexType'. :(
#13
Specifying an Element
Referring to an element:
<xs:element ref="my:name"/>
The attribute 'minOccurs' specifies the minimum number of these child elements (default is 1).
The attribute 'maxOccurs' specifies the maximum number of these child elements (default is 1).
<xs:element ref="my:name" minOccurs='2' maxOccurs='5'/>
This says: "element 'my:name" can occur between 2 and 5 times here."
The attribute 'maxOccurs' can have the value 'unbounded.
<xs:element ref="my:name" minOccurs='2' maxOccurs='unbounded'/>
This says: "element 'my:name" can occur 2 or more times here."
Examples in context:
<xs:element name="people"> <xs:complexType> <xs:sequence> <xs:element ref="my:name" maxOccurs="unbounded"/> <xs:sequence> </xs:complexType> </xs:element>
#14
Using Names in Models
When using a name, it has to be in the namespace where the construct is defined.
An element name must be in the namespace of the schema for which it is declared.
But they are QNames so you need to declare the namespaces just like in XSLT.
Example: "my:name"
The local name is 'name'.
The namespace name is the URI to which the prefix 'my' resolves.
So, you should re-declare your target namespace:
<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' targetNamespace='urn:publicid:IDN+cde.berkeley.edu:...' xmlns:my='urn:publicid:IDN+cde.berkeley.edu:...'> ... </xs:schema>
#15
Sequences
The sequence element specifies an exact sequence of children elements.
Example
<xs:sequence> <xs:element ref="my:name"/> <xs:element ref="my:address"/> <xs:element ref="my:ssn"/> </xs:sequence>
This says: "The child sequence 'my:name', 'my:address', 'my:ssn' must occur here."
You can use 'minOccurs' and 'maxOccurs' on sequences.
#16
Choices
The choice element specifies an set of choices.
Example
<xs:choice> <xs:element ref="my:name"/> <xs:element ref="my:address"/> <xs:element ref="my:ssn"/> </xs:choice>
This says: "Either 'my:name', 'my:address', or 'my:ssn' must occur here but not more than one."
You can use 'minOccurs' and 'maxOccurs' on choices.
#17
Attributes
Attributes can be declared in the 'complexType' element via the 'attribute' element.
Example:
<xs:attribute name="href" type="xs:anyURI"/>
The 'use' attribute specifies optionality and can have values 'optional', 'required', 'prohibited'.
Don't worry about the value 'prohibited' for now...
In context:
<xs:element name="link"> <xs:complexType> <xs:attribute name="href" type="xs:anyURI"/> </xs:complexType> </xs:element>
#18
Attributes on Simple Typed Elements
If you want to have an attribute on an element with simple typed content (e.g. string):
<xs:element name="a"> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute name="href" type="xs:anyURI"/> </xs:extension> </xs:simpleContent> </xs:element>
This "extends" the simple type 'xs:string' to have an attribute.
Type extension will be talked about in the next lecture.
#19
Mixed Content
This is mixed content:
<p>Hello <a href="http://www.w3.org">W3C</a></p>
i.e. elements and text at the same level.
There's a simple flag for this:
<xs:element name="p"> <xs:complexType mixed='true'> <xs:sequence> <xs:element name="my:a" minOccurs="0" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> </xs:element>
But you can't type the text--its just a string (e.g. you can't say its an integer).
#20
Local Element Declarations
You can declare elements inside content models:
Example:
<xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element>
which validates:
<my:person><name>Milowski</name></my:person>
The declaration of 'name' is local to 'person'.
#21
Forcing Qualified Local Elements
In use, it has no namespace unless you force it.
The 'form' attribute can make it qualified:
<xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="name" form='qualified' type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element>
which validates:
<my:person><my:name>Milowski</my:name></my:person>
#22
Empty Elements?
This is the simplest declaration:
<xs:element name="stop"/>
This is NOT an empty element like:
<stop/>
The type is xs:anyType which is mixed and allows any children.
This is an empty element:
<xs:element name="stop"> <xs:complexType> <xs:sequence/> </xs:complexType> </xs:element>