XML Schema Chao-Hsien Chu, Ph.D. School of Information Sciences and Technology The Pennsylvania State University XML Declar. Schema Declar. ElementType AttributeType
Purposes of DTD • What elements should be presented? • How many of each element were required? • What kind of element content was permitted? • In what order the elements should appear? DTD is the modeling language for XML, which define the structure of a XML document. An DTD file provides the following information:
Components of XML Systems XML Document (Contents) Well-Formed (Syntax) XML Application XML Parser (Processor) XML DTD (Rule) Validate (Structure)
What is XML Schema • DTD is the original modeling language for XML. It has the following deficiencies: - DTD did not follow the regular XML syntax, thus it requires a special parser to validate. - DTD did not support much of data types. - DTD is weak in extensibility. • Schema is the new modeling language for XML,which closely follows XML syntax.
Problems with DTD If the DTD statement for element Quantity is: <!ELEMENT Quantity (#PCDATA)> Then which of the followings syntaxes are valid? <Quantity>5</Quantity> _________ <Quantity>hello</Quantity> _________ <Quantity>5.5</quantity> _________
Facts About XML Schema • Schema documents use XML syntax and are therefore XML documents. • Schemas are XML documents that conform to DTDs, which define the structure of a XML document. • Many organizations and individuals are creating DTDs and schemas for a broad range of categories (e.g., financial transactions, medical prescriptions, etc.). These collections called repositories which are often available free for download from the web.
Why Schema is Better? • XML Schema has Support for Data Types • XML Schemas use XML Syntax • XML Schemas Secure Data Communication • XML Schemas are Extensible • Well-Formed is not Enough
XML Example (1) <?xml version="1.0" encoding="ISO-8859-1"?> <!– Root Element --> <shiporder orderid="889923" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="shiporder.xsd"> <orderperson>John Smith</orderperson> <shipto> <name>Ola Nordmann</name> <address>Langgt 23</address> <city>4000 Stavanger</city> <country>Norway</country> </shipto>
XML Example (2) <item> <title>Empire Burlesque</title> <note>Special Edition</note> <quantity>1</quantity> <price>10.90</price> </item> <item> <title>Hide your heart</title> <quantity>1</quantity> <price>9.90</price> </item> </shiporder>
Components of Schema • XML Declaration • Comments • Schema Declaration • ElementType Declarations • AttributeType Declarations
Example of DTD File (1) <!ELEMENTshiporder (orderperson, shipto, item+)> <!ATTLIST orderid CDATA #REQUIRED> <!ELEMENT orderperson (#PCDATA)> <!ELEMENT shipto (name, address, city, country)> <!ELEMENT name (#PCDATA)> <!ELEMENT address (#PCDATA)> <!ELEMENT city (#PCDATA)> <!ELEMENT country (#PCDATA)> Attribute List Declarations Element Type Declarations
Example of DTD File (1) <!ELEMENT item (title, note?, quantity, price)> <!ELEMENT title (#PCDATA)> <!ELEMENT note (#PCDATA)> <!ELEMENT quantity (#PCDATA)> <!ELEMENT price (#PCDATA)> Element Type Declarations
XML Schema (1) <?xml version="1.0" encoding="ISO-8859-1" ?> <!– Root Element --> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="shiporder"> <xs:complexType> <xs:sequence> <xs:element name="orderperson" type="xs:string"/> <xs:element name="shipto"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="address" type="xs:string"/> <xs:element name="city" type="xs:string"/> <xs:element name="country" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element>
XML Schema (2) <xs:element name="item" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="title" type="xs:string"/> <xs:element name="note" type="xs:string" minOccurs="0"/> <xs:element name="quantity" type="xs:positiveInteger"/> <xs:element name="price" type="xs:decimal"/> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> <xs:attribute name="orderid" type="xs:string" use="required"/> </xs:complexType> </xs:element> </xs:schema>
XML Declaration • Is used to provide information regarding processing such as processor (name and version), of the processor Syntax: • <?processor instruction = “value of instruction” ?> Examples: • <?xmlversion = “1.0” ?> • <?xml version="1.0" encoding="Big5" ?> • <?xml version = "1.0" standalone = "no"?>
Comments • A place to write a note for reminding, simple documentation, or commenting out codes for debugging, etc., which will not be seen by the end users. <!-- This is a comment area --> • You can use any character inside the comment area except “--” itself • There is no limitation on the length of the comment area.
Selected XML Schema Data Types • boolean. 0 (false) or 1 (true). • char. A single character. • string. A series of characters. • float. A real number. • int. A whole number. • date. A date formatted as YYYY-MM-DD. • time. A time formatted as HH-MM-SS. • id. Text that uniquely identifies an element or attribute. • idref. A reference to an id. • enumeration. A series of values from which only one may be chosen.
Date and Time Data Types • Name Description • Date Defines a date value • dateTime Defines a date and time value • Duration Defines a time interval • gDay Defines a part of a date - the day (DD) • gMonth Defines a part of a date - the month (MM) • gMonthDay Defines a part of a date - the month and day (MM-DD) • gYear Defines a part of a date - the year (CCYY) • gYearMonth Defines a part of a date - the year and month (CCYY-MM) • Time Defines a time value
Numeric Data Types • Name Description • Byte A signed 8-bit integer • DecimalA decimal value • Int A signed 32-bit integer • Integer An integer value • Long A signed 64-bit integer • negativeInteger An integer containing only negative values ( .., -2, -1.) • nonNegativeInteger An integer containing only non-negative values (0, 1, 2, ..) • nonPositiveInteger An integer containing only non-positive values (.., -2, -1, 0) • positiveIntegerAn integer containing only positive values (1, 2, ..) • Short A signed 16-bit integer • unsignedLong An unsigned 64-bit integer • unsignedInt An unsigned 32-bit integer • unsignedShort An unsigned 16-bit integer • unsignedByte An unsigned 8-bit integer
Advantages of XML Schema • Consistency. It follows XML syntax. • Extensibility. It includes data type and namespaces. • Exchangeability/manipulability. • Rigid specification. • Ease of use. Can use with XML API such as DOM and SAX.
Thank You? Any Question?