1 / 23

XML Schemas

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

paiva
Télécharger la présentation

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 J. Pontes November 15, 2004

  2. 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

  3. 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.

  4. 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

  5. Schema: types of content • Simple Type – text only • Complex Types – contain other elements or attributes.

  6. Simple Type • Date • Integer • String • Customer built types • In DTD we used #OCDATA to capture a name, a date or practically anything.

  7. 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

  8. Examples of Built-in Simple Types <xsd:element name=“weight” type=“xsd:string”/> <xsd:element name=“population” type=“xsd:integer” />

  9. 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.

  10. 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

  11. 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.

  12. 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.

  13. 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.

  14. 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.

  15. 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.

  16. 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>

  17. 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?

  18. More XML Data Types • Click Here for More Data Types

  19. 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

  20. Date and Type Data Types • Code.xsd xsd:element name=“bedtime” type=“xsd.time:/> • Code.xml <bedtime>20:15:05-05:00>/bedtime>

  21. 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

  22. Number Type • xsd:decimal • xsd:integer • xsd:positiveinteger • xsd:negativeinteger • xsd:float • xsd:double

  23. XML Validator • STG XML Validation

More Related