Presentation is loading. Please wait.

Presentation is loading. Please wait.

CITA 330 Section 4 XML Schema. XML Schema (XSD) An alternative industry standard for defining XML dialects More expressive than DTD Using XML syntax Promoting.

Similar presentations


Presentation on theme: "CITA 330 Section 4 XML Schema. XML Schema (XSD) An alternative industry standard for defining XML dialects More expressive than DTD Using XML syntax Promoting."— Presentation transcript:

1 CITA 330 Section 4 XML Schema

2 XML Schema (XSD) An alternative industry standard for defining XML dialects More expressive than DTD Using XML syntax Promoting declaration reuse so common declarations can be factored out and referenced by multiple element or attribute declarations

3 Example XSD

4 Example XSD …

5 XML Namespace Tag and attribute names are supposed to be meaningful Namespace is for reducing the chance of name conflicts A namespace is any unique string, typically in URL form An XML document can define a short prefix for each namespace to qualify tag and attribute names declared under that namespace –“http://www.w3.org/2001/XMLSchema” is the namespace for tag and attribute names of XML Schema –“xs” is defined as a prefix for this namespace –“xs:schema”: the “schema” tag name defined in namespace “xs” or “http://www.w3.org/2001/XMLSchema”

6 XML Namespace To declare "http://cita330.canton.edu" to be the default namespace (to which all unqualified names belong), use attribute xmlns="http://cita330.canton.edu" To declare that all tag/attribute names declared in the current XSD file belong to namespace "http://cita330.canton.edu", use the targetNamespace attribute: Declarations in a schema element without specifying targetNamespace value does not belong to any namespace

7 XML Declarations: Global vs. Local Global declarations: XSD declarations immediately nested in the top-level schema element Global declarations are the key for reusing declarations Local declarations: only valid in their hosting elements (not schema)

8 Declaring Simple Elements To declare element color that can take on any string value – –Element “ blue ” will have value “blue”, and element “ ” will have no value To declare element color that can take on any string value with “red” to be its default value – –Element “ blue ” will have value “blue”, and element will have the default value “red”

9 Declaring Simple Elements To declare element color that can take on only the fixed string value “red” – –Element red will be correct, element blue will be invalid, and element will have the fixed (default) value "red"

10 Declaring Attributes Attribute declarations are always nested in their hosting elements’ declarations To declare that lang is an attribute of type xs:string, and its default value is “EN” – If the above attribute lang doesn’t have a default value but it must be specified for its hosting element –

11 Declaring Complex Elements To declare that product is an empty element type with optional integer-typed attribute pid –Example product elements

12 Declaring Complex Elements To declare that an employee element’s value is a sequence of two nested elements: a firstName element followed by a lastName element, both of type string –Example Tom Sawyer

13 Using Global Type Declarations Global declarations promote declaration reuse

14 Declaring Complex Type Elements To declare a complexType element shoeSize with integer element value and a string-type attribute named country –Example: 35

15 Declaring Mixed Complex Type Elements A mixed complex type element can contain attributes, elements, and text To declare a letter element that can have a mixture of elements and text as its value

16 Declaring Mixed Complex Type Elements Example letter element Dear Mr. John Smith, Your order 1032 will be shipped on 2008-09- 23.

17 Specifying Unlimited Element Order Use “xs:all” elements to replace “xs:sequence” elements is you allow the nested elements to occur in any order <xs:element name="firstName" type="xs:string"/> <xs:element name="lastName" type="xs:string"/> The firstName and lastName elements can occur in any order

18 Specifying Multiple Occurrence of an Element Use occurrence indicators, maxOccurs and minOccurs, to indicate an element can occur how many times Attribute maxOccurs has default value 1 Attribute minOccurs has default value 1 To declare that the dvd element can occur one or unlimited number of times <xs:element name="dvd" minOccurs=“1" maxOccurs="unbounded">

19 Specifying an XML Schema without Target Namespace Assume that –an XML dialect is specified with an XML Schema file schemaFile.xsd without using a target namespace –the Schema file has URL schemaFileURL, which is either a local file system path like “schemaFile.xsd” or a Web URL like “http://csis.pace.edu/schemaFile.xsd” The instance documents of this dialect can be associated with its XML Schema declaration with the following structure, where rootTag is the name of a root element <rootTag xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="schemaFileURL" >

20 Specifying an XML Schema with Namespace Assume that –an XML dialect is specified with an XML Schema file schemaFile.xsd using target namespace namespaceString –the Schema file has URL schemaFileURL, which is either a local file system path like “schemaFile.xsd” or a web URL like http://csis.pace.edu/schemaFile.xsd The instance documents of this dialect can be associated with its XML Schema declaration with the following structure, where rootTag is the name of a root element <rootTag xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="namaspaceString schemaFileURL" >


Download ppt "CITA 330 Section 4 XML Schema. XML Schema (XSD) An alternative industry standard for defining XML dialects More expressive than DTD Using XML syntax Promoting."

Similar presentations


Ads by Google