230 likes | 479 Views
XML Schemas. J. Pontes November 15, 2004. Schemas. Defines what a set of one or more document can look like. What elements it contains, order, content, and attributes. These documents are written in XML Schema language. DTD Disadvantages compared to XML. Cannot be parsed by an XML parser
E N D
XML Schemas J. Pontes November 15, 2004
Schemas • Defines what a set of one or more document can look like. • What elements it contains, order, content, and attributes. • These documents are written in XML Schema language
DTD Disadvantages compared to XML • Cannot be parsed by an XML parser • All declarations in DTD are global (impossible to define two elements with same name.) • DTD cannot control what kind of information a given element or attribute can contain.
What XML Schema Buys You • One can define both global elements (used same way all along document) and local elements (particular meaning in a particular context). • XML Schemas contain a system of data types. • Gives more control over XML document
Schema: types of content • Simple Type – text only • Complex Types – contain other elements or attributes.
Simple Type • Date • Integer • String • Customer built types • In DTD we used #OCDATA to capture a name, a date or practically anything.
Complex Type • Describe the structure of a document, rather than content. • Complex Types • Elements that contain elements • Elements that contain element and text • Elements that contain only text • Elements that are empty • Each may contain attributes
Examples of Built-in Simple Types <xsd:element name=“weight” type=“xsd:string”/> <xsd:element name=“population” type=“xsd:integer” />
Custom Simple Type <xsd:simpleType name=“zipcodeType”> <xsd:restriction base=“xsd:string”> <xsd:pattern value=“\d{5}(-\d{4}?” /> </xsd:restriction> <xsd:simpleType> This type limits the content of elements (zipcodeType) to a string with 5 digits, an optional hyphen and 4 extra digits.
Complex Type Definition <xsd:complexType name=“endType”> <xsd:sequence> <xsd:element name=“animal” type=“animalType” minOccurs=“1” maxOccurs=“unbounded”/> </xsd:sequence> <xsd:complexType> Defines endType. It contains another element animal defined with another complex type animalType to define a particular element
Local end Global declarations • Global • Declared at top level of a schema, just immediately below xsd:schema element. They are available to be used throughout the the rest of the schema. • Global element declaration do not determine where an element can appear in the XML document – they only determine what the element will look like. • A global element declaration must be explicitly referenced in order to have it appear in a corresponding XML document. • Simple • Locally declared elements. Limited to the complex type definition in which they are declared and may not not be used elsewhere in the schema. • Such locally declared elements are automatically referenced.
Example of Local and Global Elements <?xml version=“1.0”?> <xsd:schema smlns:xsd=“http://www.w3.org/2000/10/XMLSchema> <xsd:element name=“endanged_species” type=“endType”/> <xsd:element name=“name” type=“xsd:string”/> <xsd:complexType name=“endType”> <xsd:sequence> <xsd:element name=“animal”> <xsd:complexType> <xsd:sequence> <xsd:element ref=“name” minOccurs=“2”/> <xsd:element name=“source” type=“sourceType”/> … </xsd:complexType> automatically referenced. manually referenced. Same name different definitions.
Cont’d <xsd:complexType name=“habitatType”> <xsd:sequence> <xsd:element name=“river”> <xsd:complexType> <xsd:sequence> <xsd:element ref=“name” minOccurs=“1” maxOccurs=“unbounded”/> <xsd:element name=“source” type=“xsd:string”/> … </xsd:complexType> … Same name different definitions.
Beginning a Simple Schema <?xml version=“1.0”?> //Declaring XML <xsd:schema xmlns:xsd=“ http://www.w3.org/2000/10/XMLSchema ” • Any element or type that is prefixed with xsd: will be recognized as coming from namespace* • Schema rules go in this space. </xsd:schema> • Save you schema as a text only with the .xsd extension.
Indicating a Location <?xml version=“1.0”?> <endanged_species xmlns:xsd=“http://www.w3.org/2000/10/XMLSchema-instance ” xsi:noNamespaceSchemaLocation= “ http://www.cookwook.com/ns/end_species/end_species.xsd ”> * * file.xsd previously created.
Making Schemas Annotations <?xml version=“1.0”?> <xsd:schema xmlns:xsd=“ http://www.w3.org/2000/10/XMLSchema ” <xsd:annotation> <xsd:documentation> This schema will be used to validate the set of XML documents for the Endangered Species Project. </xsd:documentation> </xsd:annotation>
Defining Simple Types • When declaring an element, you choose its name and what kind of content it should contain. • xsd:string – string of characters • xsd:decimal – decimal number • xsd:boolean – true of false • xsd:date – CCYY-MM-DD • xsd:time - • xsd:uriReference - element will contain an URL • xsd:language – two letter language listed in ISO639 • Custom – name of a custom simple type. • Code.xsd <xsd:elementname="weight" type="xsd:string" /> <xsd:elementname="population" type="xsd:integer" /> • Code.xml <weight>500 pounds</weight> <population>28</population> When a value is an integer or a string?
More XML Data Types • Click Here for More Data Types
Date and Time Data Types • Code.xsd <xsd:element name=“gestation” type=“xsd:timeDuration”/> (represent a certain amount of time) • Code.xml <gestation>P3M15D</gestation> (PnYnMnDTnHnMnS) n - non negative Period T begins optional time section
Date and Type Data Types • Code.xsd xsd:element name=“bedtime” type=“xsd.time:/> • Code.xml <bedtime>20:15:05-05:00>/bedtime>
Additional xsd:time • xsd:time (hh:mm:ss.sss) • xsd:timeInstant (CCYY-MM-DDTh:mm:ss.sss) • xsd:date CCYY-MM-DD • xsd:month (CCYY-MM • xsd:year (CCYY) • xsd:century (CC) • xsd:recurringDate • xsd:recurringDay
Number Type • xsd:decimal • xsd:integer • xsd:positiveinteger • xsd:negativeinteger • xsd:float • xsd:double
XML Validator • STG XML Validation