XML DTD and XML Schema Discussion Sessions 1A and 1B Session 2.

12
S XML DTD and XML Schema Discussion Sessions 1A and 1B Session 2

Transcript of XML DTD and XML Schema Discussion Sessions 1A and 1B Session 2.

Page 1: XML DTD and XML Schema Discussion Sessions 1A and 1B Session 2.

S

XMLDTD and XML

SchemaDiscussion Sessions 1A and 1B

Session 2

Page 2: XML DTD and XML Schema Discussion Sessions 1A and 1B Session 2.

XMLeXtensible Markup Language

<?xml version="1.0" encoding="UTF-8"?><wonders><!-- Wonders of the ancient world --> <wonder> <name>Colossus of Rhodes</name> <location>Greece</location> <height units="feet">107</height> </wonder> <wonder> <name>Great Pyramid of Giza</name> <location>Egypt</location> <height units="meters">147</height> </wonder></wonders>

Page 3: XML DTD and XML Schema Discussion Sessions 1A and 1B Session 2.

DTDDocument Type Definition

<?xml version="1.0" encoding="UTF-8"?><!ELEMENT Bookstore ( Book* ) >

<!ELEMENT Book ( Title, Author+, Remark? ) ><!ATTLIST Book ISBN CDATA #REQUIRED Price CDATA #REQUIRED Edition CDATA #IMPLIED >

<!ELEMENT Title ( #PCDATA ) ><!ELEMENT Author ( #PCDATA | ( FirstName, LastName ) ) ><!ELEMENT FirstName ( #PCDATA ) ><!ELEMENT LastName ( #PCDATA ) ><!ELEMENT Remark ( #PCDATA ) >

Meaning of *, +, and ?

#PCDATA and CDATA

Order of elements and attributes

#REQUIRED and #IMPLIED

Page 4: XML DTD and XML Schema Discussion Sessions 1A and 1B Session 2.

DTDDocument Type Definition

What about an empty element?

Can we define an unsorted list of elements?

An element with anything?

Attributes with choices?

Why to use attribute IDs?

IDREF(S)?

<!ELEMENT Image EMPTY >

<!ELEMENT Author ( #PCDATA | FullName | NickName )* >

<!ELEMENT Remark ANY >

<!ATTLIST Book Language ( EN | ES ) #REQUIRED >

<!ATTLIST Book ISBN ID #REQUIRED >

<!ATTLIST BookCollection ISBNRefs IDREFS #REQUIRED >

Page 5: XML DTD and XML Schema Discussion Sessions 1A and 1B Session 2.

DTDDocument Type Definition

Pros Compact structure Can be defined

inline Wide support

among parsers

Cons Are not written in

XML Don’t support

Namespaces Don’t have data –

typing Have limited

capacity for counters

Page 6: XML DTD and XML Schema Discussion Sessions 1A and 1B Session 2.

XML Schema

Types of elements

Simple type

Complex type

Text only

Elements onlyElements and

textText only

Empty

+

Att

ribute

s

Page 7: XML DTD and XML Schema Discussion Sessions 1A and 1B Session 2.

XML SchemaSimple Type

<location>Egypt</location>

Different ways to define this element:

<xs:element name=”location" type="xs:string" />

<xs:element name="location" type="stringType" />...<xs:simpleType name="stringType"> <xs:restriction base="xs:string"> <xs:maxLength value="256" /> </xs:restriction></xs:simpleType>

Page 8: XML DTD and XML Schema Discussion Sessions 1A and 1B Session 2.

XML SchemaComplex Type

Complex type

Text + Attributes

Elements + AttributesAttributes onlyText + Elements + Attributes

Simple content

Complex content

Page 9: XML DTD and XML Schema Discussion Sessions 1A and 1B Session 2.

XML SchemaComplex Type: Simple Content

<xs:element name="yearBuilt"> <xs:complexType> <!-- An anonymous type --> <xs:simpleContent> <xs:extension base="xs:positiveInteger"> <!-- Inheritance --> <xs:attribute name="era"> <xs:simpleType> <xs:restriction base="xs:string"> <!-- Regular expression --> <xs:pattern value="(BC)|(AD)” /> </xs:restriction> </xs:simpleType> </xs:attribute> </xs:extension> </xs:simpleContent> </xs:complexType></xs:element>

<yearBuilt era="BC">100</yearBuilt>

Just to generate this simple line!

Page 10: XML DTD and XML Schema Discussion Sessions 1A and 1B Session 2.

XML SchemaComplex Type: Complex Content 1

<xs:element name="ancient_wonders"> <xs:complexType> <xs:complexContent> <xs:restriction base="xs:anyType”> <xs:sequence> <xs:element name="wonder" type="wonderType" minOccurs="2" maxOccurs="unbounded"/> </xs:restriction> </xs:complexContent> </xs:sequence> </xs:complexType></xs:element>

What does this segment of code do?

Page 11: XML DTD and XML Schema Discussion Sessions 1A and 1B Session 2.

XML SchemaComplex Type: Complex Content 2

<xs:element name="image"> <xs:complexType> <xs:attribute name="source" default="ancient.jpg" type="xs:string"/> </xs:complexType></xs:element>

An empty element that contains only attributes

Page 12: XML DTD and XML Schema Discussion Sessions 1A and 1B Session 2.

XML SchemaComplex Type: Complex Content 3

<xs:element name="story" minOccurs="0"> <xs:complexType mixed="true"> <xs:sequence> <xs:element name="para" minOccurs="0" maxOccurs="unbounded"> <xs:complexType /> <!-- Completely empty element --> </xs:element> </xs:sequence> <xs:attribute name="language" use="optional" type="xs:string" /> </xs:complexType></xs:element>

A mixed element that combines children elements, text, and attributes