1 / 41

XML Schemas

XML Schemas. Presented By : Abdelali ZAHI Instructor : Dr H.Haddouti. Terms and Concepts. Schema: In database : refers to the organization or structure for a database In XML community : refers to the structure (or model of a class of

nubia
Download Presentation

XML Schemas

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. XML Schemas Presented By : Abdelali ZAHI Instructor : Dr H.Haddouti Abdelali ZAHI : FALL 2003 : XML Schemas

  2. Terms and Concepts • Schema: • In database : refers to the organization or structure for a database • In XML community : refers to the structure (or model of a class of • documents). This model describes the hierarchy of elements • and allowable content in a valid XML document • the schema defines constraints for an XML vocabulary. XML Schema is • a formal definition for defining a schema for a class of XML • documents. Abdelali ZAHI : FALL 2003 : XML Schemas

  3. Purpose • Provide a document for validation • Extend the functionality of DTDs • Define datatypes • Define elements and their content • Define attributes and their values • Define reusable components and their contents • Introduce notations Abdelali ZAHI : FALL 2003 : XML Schemas

  4. Design Principles •  More expressive than XML DTDs •  Expressed in XML •  Self-describing •  Usable in a wide variety of applications that employ XML •  Straightforwardly usable on the Internet •  Optimized for interoperability •  Simple enough to implement with modest design and runtime resources Abdelali ZAHI : FALL 2003 : XML Schemas

  5. Datatype Requirements • Provide for primitive datatyping, including byte, date, integer, sequence, and SQL datatypes. • Allow creation of user-derived datatypes, such as datatypes that are derived from an existing datatype and that may constrain some of its properties. Abdelali ZAHI : FALL 2003 : XML Schemas

  6. Well-formedness vs validity • A document is considered to be well formedif it meets all the well-following constraints: • The document contains one or more elements. • The document consists of exactly one root element (also known as the document element). • The name of an element’s end tag matches the name defined in the start tag. • No attribute may appear more than once within an element. • Attribute values cannot contain a left-angle bracket (<). • Elements delimited with start and end tags must nest properly within each other. • an XML document is valid if it adheres to a defined model for a class • of XML documents Abdelali ZAHI : FALL 2003 : XML Schemas

  7. Terms and concepts • data primitives: basic types to associate with values (integer, real • number, character, string…etc) • schema components: building blocks that make up the abstract data • model of the schema (Element and attribute declarations, complex • types, simple types, notations…) Abdelali ZAHI : FALL 2003 : XML Schemas

  8. XML SCHEMA STRUCTURES Abdelali ZAHI : FALL 2003 : XML Schemas

  9. A definition describes a complex or simple type that either contains element or attribute declarations or references element or attribute declarations defined elsewhere in the document. Here’s a definition: <xsd:complexType name=”bookType”> <xsd:sequence> <xsd:element name=”title” type=”xsd:string”/> <xsd:element name=”author” type=”xsd:string”/> <xsd:element name=”description” type=”xsd:string”/> </xsd:sequence> </xsd:complexType> A declaration defines an element or attribute, specifying the name and datatype for the component. Here’s a declaration: <xsd:element name=”book”> <xsd:complexType> <xsd:sequence> <xsd:element name=”title” type=”xsd:string”/> <xsd:element name=”author” type=”xsd:string”/> <xsd:element name=”description” type=”xsd:string”/> </xsd:sequence> </xsd:complexType> </xsd:element> Definitions vs. Declarations Abdelali ZAHI : FALL 2003 : XML Schemas

  10. A simple type definition constrains the text that is allowed to appear as content for an attribute value or text-only element without attributes. Here’s a simple type definition: <xsd:simpleType name=”isbnType”> <xsd:restriction base=”xsd:string”> <xsd:pattern value=”[0-9]{10}”/> </xsd:restriction> </xsd:simpleType> A complex type definition constrains the allowable content of elements. Both types govern possible attribute and child elements. Here’s a complex type definition: <xsd:complexType name=”bookType”> <xsd:sequence> <xsd:element name=”title” type=”xsd:string”/> <xsd:element name=”author” type=”xsd:string”/> <xsd:element name=”description” type=”xsd:string”/> </xsd:sequence> </xsd:complexType> Simple Type vs. Complex Type Abdelali ZAHI : FALL 2003 : XML Schemas

  11. Global vs. Local • Declarations and definitions can be declared globally or locally. A • globally defined component is defined as a child of the schema • element. • A locally defined component (also known as inline) is • defined as a child of another schema component. Abdelali ZAHI : FALL 2003 : XML Schemas

  12. Global Component • <xsd:schema • xmlns:xsd=http://www.w3.org/2001/XMLSchema elementFormDefault=”qualified” • targetNamespace=http://www.lanw.com/namespaces/pub • xmlns=” http://www.lanw.com/namespaces/pub ”> • <xsd:element name=”publications”> • <xsd:complexType> • <xsd:sequence> • <xsd:element ref=”book”/> • </xsd:sequence> • </xsd:complexType> • </xsd:element> Abdelali ZAHI : FALL 2003 : XML Schemas

  13. Global component (cont’d) • <xsd:element name=”book” maxOccurs=”unbounded”> • <xsd:complexType> • <xsd:sequence> • <xsd:element ref=”title”/> • <xsd:element ref=”author”/> • <xsd:element ref=”description”/> • </xsd:sequence> • </xsd:complexType> • </xsd:element> • <xsd:element name=”title” type=”xsd:string”/> • <xsd:element name=”author” type=”xsd:string”/> • <xsd:element name=”description” type=”xsd:string”/> • </xsd:schema> Abdelali ZAHI : FALL 2003 : XML Schemas

  14. Local Component • <xsd:schema • xmlns:xsd=”http://www.w3.org/2001/XMLSchema” • elementFormDefault=”qualified” • targetNamespace=”http://www.lanw.com/namespaces/pub” • xmlns=”http://www.lanw.com/namespaces/pub”> • <xsd:element name=”publications”> • <xsd:complexType> • <xsd:sequence> • <xsd:element name=”book” maxOccurs=”unbounded”> • <xsd:complexType> • <xsd:sequence> • <xsd:element name=”title” type=”xsd:string”/> • <xsd:element name=”author” type=”xsd:string”/> • <xsd:element name=”description” type=”xsd:string”/> • </xsd:sequence> • </xsd:complexType> • </xsd:element> • </xsd:sequence> • </xsd:complexType> • </xsd:element> • </xsd:schema> Abdelali ZAHI : FALL 2003 : XML Schemas

  15. Schema Components: • Type definition • Declaration • Attribute group • Indicators Abdelali ZAHI : FALL 2003 : XML Schemas

  16. Type Definitions • The are two classes of types : simple types and complex types. • There are two types of classes : • Named types: defined at the root level and referenced later • Anonymous types : defined nested within other schema components Abdelali ZAHI : FALL 2003 : XML Schemas

  17. Examples: <xs:element name="xxx" type="yyy"/> <xs:element name="lastname" type="xs:string"/> <xs:element name="age" type="xs:integer"/> <xs:element name="dateborn" type="xs:date"/> <lastname>Refsnes</lastname> <age>34</age> <dateborn>1968-03-27</dateborn> Simple types Simple types are used to define all attributes and elements that contain only text and do not have attributes associated with them. Abdelali ZAHI : FALL 2003 : XML Schemas

  18. Default and Fixed Values Simple elements can have a default value OR a fixed value set. A default value is automatically assigned to the element when no other value is specified. In the following example the default value is "red": <xs:element name="color" type="xs:string" default="red"/> A fixed value is also automatically assigned to the element. You cannot specify another value. In the following example the fixed value is "red": <xs:element name="color" type="xs:string" fixed="red"/> Abdelali ZAHI : FALL 2003 : XML Schemas

  19. Complex types • An element is considered to be a complex type if it contains attribute • or child elements. Complex types are used to define complex content • models • <employee> <firstname>John</firstname> <lastname>Smith</lastname> • </employee> • <xs:element name="employee"> • <xs:complexType> • <xs:sequence> • <xs:element name="firstname"type="xs:string"/> • <xs:element name="lastname" type="xs:string"/> • </xs:sequence> • </xs:complexType> • </xs:element> Abdelali ZAHI : FALL 2003 : XML Schemas

  20. Declarations • You can : • define elements and attributes as you need them, nesting the • declarations within other element declarations (“Inline Declarations”). • define declarations at the top level and reference them from within • element declarations (“Referencing Declarations”). • define named type definitions that are then referenced within • element or attribute declarations (“Named Types”). Abdelali ZAHI : FALL 2003 : XML Schemas

  21. Inline Declarations • <xsd:schema …> • <xsd:element name=”publications”> • <xsd:complexType> • <xsd:sequence> • <xsd:element name=”book” maxOccurs=”unbounded”> • <xsd:complexType> • <xsd:sequence> • <xsd:element name=”title” type=”xsd:string”/> • <xsd:element name=”author” type=”xsd:string”/> • <xsd:element name=”description” type=”xsd:string”/> • </xsd:sequence> • <xsd:attribute name=”isbn” type=”xsd:string”/> • </xsd:complexType> • </xsd:element> • </xsd:sequence> • </xsd:complexType> • </xsd:element> • </xsd:schema> Abdelali ZAHI : FALL 2003 : XML Schemas

  22. Referencing declarations • <xsd:schema …> • <xsd:element name=”publications”> • <xsd:complexType> • <xsd:sequence> • <xsd:element ref=”book”/> • </xsd:sequence> • </xsd:complexType> • </xsd:element> • <xsd:element name=”book” maxOccurs=”unbounded”> • <xsd:complexType> • <xsd:sequence> • <xsd:element ref=”title”/> • <xsd:element ref=”author”/> • <xsd:element ref=”description”/> • </xsd:sequence> • <xsd:attribute name=”isbn” type=”xsd:string”/> • </xsd:complexType> • </xsd:element> • <xsd:element name=”title” type=”xsd:string”/> • <xsd:element name=”author” type=”xsd:string”/> • <xsd:element name=”description” type=”xsd:string”/> • </xsd:schema> Abdelali ZAHI : FALL 2003 : XML Schemas

  23. Named Types • <xsd:simpleType name=”isbnType”> • <xsd:restriction base=”xsd:string”> • <xsd:pattern value=”[0-9]{10}”/> • </xsd:restriction> • </xsd:simpleType> • <xsd:complexType name=”pubType”> • <xsd:sequence> • <xsd:element ref=”book”/> • </xsd:sequence> • </xsd:complexType> • <xsd:complexType name=”bookType”> • <xsd:sequence> • <xsd:element ref=”title”/> • <xsd:element ref=”author”/> • <xsd:element ref=”description”/> • </xsd:sequence> • </xsd:complexType> • <xsd:element name=”publications” type=”pubType”/> • <xsd:element name=”book” type=”bookType” maxOccurs=”unbounded”/> • <xsd:element name=”title” type=”xsd:string”/> • <xsd:element name=”author” type=”xsd:string”/> • <xsd:element name=”description” type=”xsd:string”/> Abdelali ZAHI : FALL 2003 : XML Schemas

  24. How to Define an Attribute The syntax for defining an attribute is: <xs:attribute name="xxx" type="yyy"/> where xxx is the name of the attribute and yyy is the data type of the attribute. Here are an XML element with an attribute: <xs:attribute name="lang" type="xs:string"/> Abdelali ZAHI : FALL 2003 : XML Schemas

  25. Optional and Required Attributes All attributes are optional by default. To explicitly specify that the attribute is optional, use the "use" attribute: <xs:attribute name="lang" type="xs:string" use="optional"/> To make an attribute required: <xs:attribute name="lang" type="xs:string" use="required"/> Abdelali ZAHI : FALL 2003 : XML Schemas

  26. Default and Fixed Values for Attributes Attributes can have a default value OR a fixed value specified. A default value is automatically assigned to the attribute when no other value is specified. In the following example the default value is "EN": <xs:attribute name="lang" type="xs:string" default="EN"/> A fixed value is also automatically assigned to the attribute. You cannot specify another value. In the following example the fixed value is "EN": <xs:attribute name="lang" type="xs:string" fixed="EN"/> Abdelali ZAHI : FALL 2003 : XML Schemas

  27. Attributes and Grouping • An attribute group definition is an association between a name and a • set of attribute declarations, enabling reuse of the same set in • several complex type definitions • <xsd:attributeGroup name=”commonAttributes”> • <xsd:attribute name=”title” type=”xsd:string”/> • <xsd:attribute name=”id” type=”xsd:ID”/> • <xsd:attribute name=”style” type=”xsd:string”/> • </xsd:attributeGroup> Abdelali ZAHI : FALL 2003 : XML Schemas

  28. Indicators We have seven types of indicators: Order indicators: All Choice Sequence Occurrence indicators: maxOccurs minOccurs Group indicators: Group name attributeGroup name Abdelali ZAHI : FALL 2003 : XML Schemas

  29. All Indicator The <xs:all> indicator specifies by default that the child elements can appear in any order and that each child element must occur once and only once: <xs:element name="person"> <xs:complexType> <xs:all> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:all> </xs:complexType> </xs:element> Abdelali ZAHI : FALL 2003 : XML Schemas

  30. Choice Indicator The <xs:choice> indicator specifies that either one child elements or the other can occur: <xs:element name="person"> <xs:complexType> <xs:choice> <xs:element name="employee" type="employee"/> <xs:element name="member" type="member"/> </xs:choice> </xs:complexType> </xs:element> Abdelali ZAHI : FALL 2003 : XML Schemas

  31. Sequence Indicator The <xs:sequence> indicator specifies that the child elements must appear in a specific order: <xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> Abdelali ZAHI : FALL 2003 : XML Schemas

  32. Occurrence Indicators <xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="full_name" type="xs:string"/> <xs:element name="child_name" type="xs:string" maxOccurs="10" minOccurs="0"/> </xs:sequence> </xs:complexType> Abdelali ZAHI : FALL 2003 : XML Schemas

  33. Datatype properties • Every datatype is defined to consist of an ordered set of three • properties: value space, lexical space, and facets. Each of these helps • constrain the possible content. • Value Space • The value space defines the allowable set of values for a given datatype. • Lexical Space • The lexical space defines the set of valid literals for a given datatype • Facets • Facets provide a set of defining aspects of a given datatype. If you want to • define a minimum or maximum constraint, or if you need to restrict the • length of a string datatype, you’ll have to use a facet Abdelali ZAHI : FALL 2003 : XML Schemas

  34. Built-in and User-Derived • Built-In • Built-in datatypes are already defined for you by the Datatypes • specification • Primitive datatypes are those not defined in terms of other • datatypes. Primitive datatypes are the basis for all other datatypes. • A derived datatype is one that is derived from a base type. The derived type • inherits its value space from its base type and may also constrain the derived value space to be an explicit subset of the base type’s value space Abdelali ZAHI : FALL 2003 : XML Schemas

  35. Built-in and User-Derived (cont’d) • Derived Datatypes • Restriction Uses facets to restrict the allowable content. • List Derived datatype is a list of white-space-separated values of the • base datatype. • Union There are two or more base datatypes, and the value space– • derived datatype is formed from the union of the value spaces of the • base types. Abdelali ZAHI : FALL 2003 : XML Schemas

  36. Namespaces Abdelali ZAHI : FALL 2003 : XML Schemas

  37. What are they? • Definition: XML namespaces are symbolic references in the form of • URLs used to resolve naming conflicts in documents • Example: • <html> • <head> • <title>Book List</title> • </head> • <body> • <publications> • <book> • <title>Mastering XHTML</title> • <author>Ed Tittel</author> • </book> • <book> • <title>Java Developer’s Guide to E-Commerce • with XML and JSP</title> • <author>William Brogden</author> • </book> • </publications> • </body> • </html> <html xmlns=”http://www.w3.org/1999/xhtml”> <head> <title>Book List</title> </head> <body> <publications xmlns=”http://www.lanw.com/namespaces/pub”> The Role of Namespaces 144 <book> <title>Mastering XHTML</title> <author>Ed Tittel</author> </book> <book> <title>Java Developer’s Guide to E-Commerce with XML and JSP</title> <author>William Brogden</author> </book> </publications> </body> </html> Abdelali ZAHI : FALL 2003 : XML Schemas

  38. Derivation and Substitution • Derivationallows you to derive new types by restricting or extending • the type definition. Extension is only allowed for complex types. Extension <xsd:complexType name=”newBookType”> <xsd:complexContent> <xsd:extension base=”bookType”> <xsd:sequence> <xsd:element name=”description” type=”xsd:string”/> <xsd:element name=”isbn” type=”xsd:string”/> </xsd:sequence> </xsd:extension> </xsd:complexContent> </xsd:complexType> Restriction <xsd:complexType name=”newBookType”> <xsd:complexContent> <xsd:restriction base=”bookType”> <xsd:sequence> <xsd:element name=”title” type=”xsd:string”/> <xsd:element name=”author” type=”xsd:string”/> </xsd:sequence> </xsd:restriction> </xsd:complexContent> </xsd:complexType> Abdelali ZAHI : FALL 2003 : XML Schemas

  39. Substitution • XML Schema supports substitution mechanisms that allow one group • of elements to be replaced by another group of elements. This is • especially helpful when you’re working with a predefined XML • Schema document Abdelali ZAHI : FALL 2003 : XML Schemas

  40. References • W3C XML Schema Recommendationhttp://www.w3.org/TR/xmlschema-0/ • Tools and resources on XML Schemahttp://www.w3.org/XML/Schema • http://www.w3schools.com Abdelali ZAHI : FALL 2003 : XML Schemas

  41. <xsd:Conclibusion> is an essential tag in XML schemas that means • Conclusion and is used when I finish my presentation. • YOUR QUESTIONS ARE WELCOME Abdelali ZAHI : FALL 2003 : XML Schemas

More Related