23
Chapter 2 XML Schema Content Create XML Document XML Schema XML Schema DTD vs. XML Schema The “schema” element Referencing a schema in an XML document Simple and complex types Element and attribute declarations Occurrence constraints Content models Annotations chapter 02 XML Schema 2 DTD and XML Schema (1/3) Both of them define the legal building blocks of an XML document blocks of an XML document Define elements and attributes that can appear appear Define the relationships between elements Define default and fixed values for attributes Define default and fixed values for attributes chapter 02 XML Schema 3 DTD vs. XML Schema (2/3) XML Schema uses XML syntax Do not need to learn a new syntax Do not need to learn a new syntax Use any XML parser to parse XMLSchemas XML S h t ld t t XML Schema supports several data types and allows us to create new data types No ‘Integer’ or ‘Double’ types in DTD chapter 02 XML Schema 4

DTD and XML Schema (1/3) DTD vs. XML Schema (2/3)sirisuda/290371/290371_ch2.pdfChapter 2 XML Schema Content Create XML Document XML Schema DTD vs. XML Schema The “schema” element

  • Upload
    others

  • View
    64

  • Download
    0

Embed Size (px)

Citation preview

Page 1: DTD and XML Schema (1/3) DTD vs. XML Schema (2/3)sirisuda/290371/290371_ch2.pdfChapter 2 XML Schema Content Create XML Document XML Schema DTD vs. XML Schema The “schema” element

Chapter 2

XML Schema

Content

Create XML DocumentXML SchemaXML Schema

DTD vs. XML SchemaThe “schema” elementReferencing a schema in an XML documentSimple and complex typesElement and attribute declarationsOccurrence constraintsContent modelsAnnotations

chapter 02 XML Schema 2

DTD and XML Schema (1/3)

Both of them define the legal building blocks of an XML documentblocks of an XML document

Define elements and attributes that can appearappearDefine the relationships between elementsDefine default and fixed values for attributesDefine default and fixed values for attributes

chapter 02 XML Schema 3

DTD vs. XML Schema (2/3)XML Schema uses XML syntax

Do not need to learn a new syntaxDo not need to learn a new syntaxUse any XML parser to parse XMLSchemas

XML S h t l d t tXML Schema supports several data types and allows us to create new data types

No ‘Integer’ or ‘Double’ types in DTD

chapter 02 XML Schema 4

Page 2: DTD and XML Schema (1/3) DTD vs. XML Schema (2/3)sirisuda/290371/290371_ch2.pdfChapter 2 XML Schema Content Create XML Document XML Schema DTD vs. XML Schema The “schema” element

DTD vs. XML Schema (3/3)

XML Schema allows us to group elements to control the recurrence of elements andto control the recurrence of elements and attributes

In DTD we cannot specify the minimum andIn DTD, we cannot specify the minimum and maximum numbers of occurrences

XML Schema supports namespacesXML Schema supports namespacesDTD has limited support of namespaces

chapter 02 XML Schema 5

Sample XML (note.xml)<?xml version="1.0"?><note><note>

<to>Tove</to><from>Jani</from><from>Jani</from><heading>Reminder</heading>

b d D 't f t thi <body>Don't forget me this weekend!</body>

</ t ></note>

chapter 02 XML Schema 6

A Simple DTD "note.dtd"

<!ELEMENT note (to, from, heading, body)><!ELEMENT to (#PCDATA)><!ELEMENT to (#PCDATA)><!ELEMENT from (#PCDATA)><!ELEMENT heading (#PCDATA)><!ELEMENT body (#PCDATA)><!ELEMENT body (#PCDATA)>

chapter 02 XML Schema 7

XML Schema Example "note.xsd"

<?xml version="1.0"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"targetNamespace="http://www w3schools com"targetNamespace= http://www.w3schools.comxmlns="http://www.w3schools.com"elementFormDefault="qualified"><xs:element name="note">

<xs:complexType><xs:sequence><xs:element name="to" type="xs:string"/>

f /<xs:element name="from" type="xs:string"/><xs:element name="heading" type="xs:string"/><xs:element name="body" type="xs:string"/>

</xs:sequence></xs:sequence></xs:complexType>

</xs:element></xs:schema>

chapter 02 XML Schema 8

Page 3: DTD and XML Schema (1/3) DTD vs. XML Schema (2/3)sirisuda/290371/290371_ch2.pdfChapter 2 XML Schema Content Create XML Document XML Schema DTD vs. XML Schema The “schema” element

What does A Schema Consist of?A “schema” elementA variety of sub-elements

“element” elements“element” elementsExample:

<xsd:element name="nation“ type="nationType"/>yp yp“complexType” elements

Example:< d l T ti T > < d l T ><xsd:complexType name="nationType">…</xsd:complexType>

“simpleType” elements

chapter 02 XML Schema 9

The “schema” Element

<?xml version="1.0"?><xs:schema><xs:schema>......</xs:schema></xs:schema>

chapter 02 XML Schema 10

A schema declaration often looksA schema declaration often looks something like this:

<?xml version="1.0"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"targetNamespace="http://www.w3schools.com"xmlns="http://www w3schools com"xmlns= http://www.w3schools.comelementFormDefault="qualified">attributeFormDefault=“unqualified”attributeFormDefault unqualified......</xs:schema>

chapter 02 XML Schema 11

Referencing a Schema in an gXML Document

<?xml version="1.0"?><note xmlns="http://www.w3schools.com"pxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.w3schools.com note.xsd"><to>Tove</to><to>Tove</to><from>Jani</from><heading>Reminder</heading>

b d D 't f t thi k d! /b d<body>Don't forget me this weekend!</body></note>

chapter 02 XML Schema 12

Page 4: DTD and XML Schema (1/3) DTD vs. XML Schema (2/3)sirisuda/290371/290371_ch2.pdfChapter 2 XML Schema Content Create XML Document XML Schema DTD vs. XML Schema The “schema” element

Schema Data TypesSimple type

Do not have sub-elementsDo not have attributesPredefined type or derived from predefined typeExample: <name>Thailand</name>

Complex typeHave either sub-elements or attributesExample:

<nation ..><name>…</name></nation>

chapter 02 XML Schema 13

Definition and Declaration

DefinitionCreate new types (both simple and complexCreate new types (both simple and complex types)

DeclarationDeclarationEnable elements and attributes with specific

d t (b th i l d l ) tnames and types (both simple and complex) to appear in document instances

chapter 02 XML Schema 14

SimpleType Example

<!-- Definition --><xsd:simpleType name=“zipUnion”><xsd:union memberTypes=“ThaiProvincesli t fM I tT ” >listofMyIntType”/></xsd:simpleType>

<!--Declaration--><element name=“zips” type=“zipUnion”><element name= zips type= zipUnion >

chapter 02 XML Schema 15

Common Built-in Simple Data Typesstring, normalizedString

Example: <element name=“Title” type=“string”/>

int, long, short, unsignedIntE l < l t “ It ” t “i t”/>Example: <element name=“numItems” type=“int”/>

time dateTime date durationtime, dateTime, date, durationExample: <element name=“from” type=“time”/>

chapter 02 XML Schema 16

Page 5: DTD and XML Schema (1/3) DTD vs. XML Schema (2/3)sirisuda/290371/290371_ch2.pdfChapter 2 XML Schema Content Create XML Document XML Schema DTD vs. XML Schema The “schema” element

Defining New Simple Types (1/2)

New simple types are defined by deriving them from existing simple types (built-in’s and derived)In particular, we can derive a new simple type by restricting an existing simple typeExample:Example:

<xsd:simpleType name=“passwordType”><xsd:restriction base=“xsd:string”>

<xsd:length value=“8”/></xsd:restriction></xsd:restriction>

</xsd:simpleType>

chapter 02 XML Schema 17

Defining New Simple Types (2/2)We use the “simpleType” element to define and name the new simple typeExample:a p e

<xsd:simpleType name=“passwordType”>

We use the “restriction” element to indicate the existing (base) type, and to identify the “facets” that constrain the range of valuesvaluesExample:

d t i ti b “ d t i ”<xsd:restriction base=“xsd:string”><xsd:length value=“8”/>

</xsd:restriction>

chapter 02 XML Schema 18

Restrictions/Facets

Restrictions are used to control acceptable values for XML elements or attributesMany kinds of restrictions/facets

Restrictions on valuesRestrictions on valuesRestrictions on patternsRestrictions on a set of valuesRestrictions on a set of valuesRestrictions on whitespaces

chapter 02 XML Schema 19

การกําหนดขอบังคับใหประเภทขอมลู ู

chapter 02 XML Schema 20

Page 6: DTD and XML Schema (1/3) DTD vs. XML Schema (2/3)sirisuda/290371/290371_ch2.pdfChapter 2 XML Schema Content Create XML Document XML Schema DTD vs. XML Schema The “schema” element

Restrictions on ValuesWe can restrict the range of the base type by employing two facets called “minInclusive” and “maxInclusive”In XML Schema:

<xsd:simpleType name=“myIntegerType”><xsd:restriction base=“xsd:integer”><xsd:restriction base xsd:integer >

<xsd:minInclusive value=“00000”/><xsd:maxInclusive value=“99999”/>

</xsd:restriction></xsd:simpleType><xsd:element name=“myInteger” type=“myIntegerType”/>

In XML: <myInteger>12345</myInteger>

chapter 02 XML Schema 21

Restriction on Patterns (1/2)To limit the content to a series of numbers or letters, use the pattern constraintIn XML Schema:

<xsd:simpleType name=“SKU”><xsd:restriction base=“xsd:string”>

<xsd:patternvalue=“\d{3}-[A-Z]{2}”/>

</xsd:restriction></xsd:simpleType><xsd:element name=“item” type=“SKU”/>

In XML:<item>872-AA</item>

chapter 02 XML Schema 22

Restrictions on Patterns (2/2)To define a “password” type that has 8 characters which each of them is either a character or a digitIn XML Schema:

<xsd:simpleType name=“passwordType”>< sd est iction base “ sd st ing”><xsd:restriction base=“xsd:string”>

<xsd:pattern value=“[a-zA-Z0-9]{8}”/></xsd:restriction>

</xsd:simpleType><xsd:element name=“password” type=“passwordType”/>

In XML: <password>1qaz2wsx</password>chapter 02 XML Schema 23

In XML: <password>1qaz2wsx</password>

Restriction on a Set of ValuesTo limit the content of an XML element to a set of acceptable values, we would use the enumeration constraintIn XML Schema:

<xsd:simpleType name=“provinceType”><xsd:restriction base=“xsd:string”>

d ti l “Chi R i”<xsd:enumeration value=“Chiang Rai”/><xsd:enumeration value=“Chiang Mai”/>

…< xsd simpleType></xsd:simpleType><xsd:element name=“province” type=“provinceType”/>

In XML: <province>Chiang Rai</province>

chapter 02 XML Schema 24

Page 7: DTD and XML Schema (1/3) DTD vs. XML Schema (2/3)sirisuda/290371/290371_ch2.pdfChapter 2 XML Schema Content Create XML Document XML Schema DTD vs. XML Schema The “schema” element

Restrictions on Whitespaces (1/2)

To specify how whitespace characters should be handled, use the ,whitespace constraint

<xsd:simpleType name=“address”><xsd:restriction base=“xsd:string”/>

d hi S l “ ”/<xsd:whiteSpace value=“preserve”/>…

</xsd:simpleType></xsd:simpleType>

chapter 02 XML Schema 25

Restrictions on Whitespaces (2/2)

Three values of whiteSpacepreserve: the XML processor will not remove any whitespace charactersreplace: All occurrences of tab linefeed andreplace: All occurrences of tab, linefeed, and carriage return are replaced with a single spacecollapse: Like “replace” but several spaces are p p pcollapsed into a single space and leading and trailing spaces are removed

chapter 02 XML Schema 26

Restrictions on LengthsWe can also specify the minimum and maximum lengths of values using “minLength” and “maxLength” attributesIn XML Schema:

<xsd:simpleType name=“passwordType”><xsd:restriction base=“xsd:string”><xsd:restriction base= xsd:string >

<xsd:length value=“8”/>…

</xsd simpleType></xsd:simpleType><xsd:element name=“password” type=“passwordType”/>

In XML: <password>1q2w3e4r</password>chapter 02 XML Schema 27

In XML: <password>1q2w3e4r</password>

List Types (1/3)We can also create new list types by derivation from existing atomic typesWe cannot create list types from existing list typesyp g ypIn XML Schema:

d i l T “li tOfM I tT ”<xsd:simpleType name=“listOfMyIntType”><xsd:list itemType=“myIntegerType”/>

</xsd:simpleType><xsd:element name=“listOfMyInt” type=“listOfMyIntType”/>

In XML: <listOfMyInt>10 20 30</listOfMyInt>

chapter 02 XML Schema 28

Page 8: DTD and XML Schema (1/3) DTD vs. XML Schema (2/3)sirisuda/290371/290371_ch2.pdfChapter 2 XML Schema Content Create XML Document XML Schema DTD vs. XML Schema The “schema” element

List Types (2/3)Several facets can be applied to list types: length, minLength, maxLength, and enumerationIn XML schema:

<xsd:simpleType name=“ProvincesListType”><xsd list itemType “provinceType” ><xsd:list itemType=“provinceType”/>

</xsd:simpleType><xsd:element name=“ProvincesList”

“ ”

In XML

type=“ProvincesListType”/>

In XML:<ProvincesList>Chiang Rai Chiang Mai</provincesList>

chapter 02 XML Schema 29

List Types (3/3)

If we want to restrict that the number of provinces are 74 we can use facet lengthprovinces are 74, we can use facet lengthIn XML Schema:

<xsd:simpleType name=“ThaiProvincesType”><xsd:restriction base “ProvincesListType”><xsd:restriction base=“ProvincesListType”>

<xsd:length value=“74”/></xsd:restriction>

/ d i l T</xsd:simpleType>

chapter 02 XML Schema 30

Union TypesA union type enables an element or attribute value to be one or more instances of one type drawn from the union of multiple atomic and list typesIn XML Schema:

<xsd simpleType name “zipUnionType”><xsd:simpleType name=“zipUnionType”><xsd:union

memberTypes=“ThaiProvincesTypeli OfM I T ”listOfMyIntType”/>

</xsd:simpleType>

In XML: <zipUnion>Khon Kaen 40002</zipUnion>

chapter 02 XML Schema 31

Declaring a Simple Element

The syntax for declaring a simple element<xsd:element name=“xxx” type=“yyy”/><xsd:element name xxx type yyy />

Where xxx is the element name and yyy is the data type of the elementthe data type of the elementExample:

d l t “ "<xsd:element name=“name"type="xsd:string"/>

chapter 02 XML Schema 32

Page 9: DTD and XML Schema (1/3) DTD vs. XML Schema (2/3)sirisuda/290371/290371_ch2.pdfChapter 2 XML Schema Content Create XML Document XML Schema DTD vs. XML Schema The “schema” element

Declaring an Attribute

The syntax for declaring a simple element<xsd:attribute name=“xxx” type=“yyy”/><xsd:attribute name= xxx type= yyy />

Where xxx is the attribute name and yyy is the data type of the attributeExample:p<xsd:attribute name=“id” type=“xsd:ID”/>

chapter 02 XML Schema 33

AttributesAll attributes are declared as simple typesOnly complex elements can have attributesAn element with attributes always has a complex type definitionExample:Example:

<xsd:complexType name=“nationType><xsd:complexType name nationType>…

<xsd:attribute name=“id” type=“xsd:ID”/></xsd:complexType></xsd:complexType>

chapter 02 XML Schema 34

Default & Fixed Values

A default value is automatically assigned toThe element when no value is specifiedThe element when no value is specifiedThe attribute when no attribute is specified

A fi d l i t ti ll i d tA fixed value is automatically assigned to elements

We cannot specify another element value

chapter 02 XML Schema 35

Default Values of Elements & Attributes

Default values of both attributes and elements are declared using the elements are declared using the “default” attributeDefa lt att ib te al es appl hen Default attribute values apply when attributes are missingDefault element values apply when elements are empty

chapter 02 XML Schema 36

Page 10: DTD and XML Schema (1/3) DTD vs. XML Schema (2/3)sirisuda/290371/290371_ch2.pdfChapter 2 XML Schema Content Create XML Document XML Schema DTD vs. XML Schema The “schema” element

Default Element Value Example

The simple empty element which its default value specified in XML schemap

In XML Schema:

<xsd:element name=“price” type=“xsd:decimal” default=“100.0”/>

In XML:

i /<price/><!-- has the same effect as

<price>100</price> -->

chapter 02 XML Schema 37

Default Attribute Value Example

The attribute which its default value specified in XML schema

I XML S hIn XML Schema:

<xsd:element name=“nation>…

<xsd:attribute name=“country”type=“xsd:NMTOKEN” default=“Thailand”/></ d l t>

I XML

</xsd:element>

In XML: <nation>…</nation>has the same effect as<nation country=“Thailand”>…</nation>

chapter 02 XML Schema 38

Fixed Attribute Value ExampleThe attribute which its value is fixed

In XML Schema:

d l t “ ti<xsd:element name=“nation>…

<xsd:attribute name=“country”type=“xsd:NMTOKEN” fixed=“Thailand”/></xsd:element>

In XML: <nation>…</nation>has the same effect as

<nation country=“Thailand”>…</nation>cannot write this

<nation country=“USA”>…</nation>

chapter 02 XML Schema 39

Element Declaration Reference (1/2)

The element declarations have described so far have each associated a name with an existing type definitiondefinitionSometimes it is preferable to use an existing element rather than declare a new elementE lExample:

t ff<staff><manager><name>r</name></manager><employee><name>e</name></employee>

/ ff</staff>

chapter 02 XML Schema 40

Page 11: DTD and XML Schema (1/3) DTD vs. XML Schema (2/3)sirisuda/290371/290371_ch2.pdfChapter 2 XML Schema Content Create XML Document XML Schema DTD vs. XML Schema The “schema” element

Element Declaration Reference (2/2)

The declaration references an existing element, comment, that was declared elsewhere in the schemaThe value of “ref” attribute must reference a global The value of ref attribute must reference a global element which is the element directly under <schema> root element

<element name=“name” type=“xsd:string”/><element name=“manager”>

<xsd:complexType><xsd:element ref=“name”/>…

</element>

chapter 02 XML Schema 41

Element Declaration Reference Example

<xsd:schema>…<xsd:element name="comment" type="xsd:string"/><xsd:element name= comment type= xsd:string /><xsd:complexType name="PurchaseOrderType">

<xsd:sequence>…<xsd:element ref="comment"/>

<xsd:attribute name="orderDate" type="xsd:date"/></xsd:complexType>

</xsd:schema></xsd:schema>

chapter 02 XML Schema 42

Complex Elements

A complex element is an XML element that contains other elements element that contains other elements and/or attributesSeveral kinds of complex elementsSeveral kinds of complex elements

Empty elements

Elements that contain only sub-elements

Elements that contain both sub-elements and text

chapter 02 XML Schema 43

Complex Type DefinitionsNew complex types are defined using the “complexType” element

Example: <xs:complexType Example: <xs:complexType name=“nationType”>

The definitions containElement declarations

Example: <xs:element name=“name” type=“xs:string”/>type xs:string />

Element referencesExample: <xs:element ref=“location”/>

Att ib t d l tiAttribute declarationsExample: <xs:attribute name=“id” type=“xs:ID”/>

chapter 02 XML Schema 44

Page 12: DTD and XML Schema (1/3) DTD vs. XML Schema (2/3)sirisuda/290371/290371_ch2.pdfChapter 2 XML Schema Content Create XML Document XML Schema DTD vs. XML Schema The “schema” element

Defining a Complex Type<xsd:complexType name="nationType">

<xsd:sequence>d l "<xsd:element name="name

type="xsd:string"/>< d element n me "lo tion"<xsd:element name="location"

type="xsd:string"/></xsd:sequence></xsd:sequence><xsd:attribute name="id" type="xsd:ID"/>

</xsd:complexType></xsd:complexType>

chapter 02 XML Schema 45

Sharing the Complex TypeDefining the “address” type

<xsd:complexType <xsd:complexType name=“ThaiAddress">…</xsd:complexType>

Sharing the “address” typeSharing the address type

<xsd:complexType name=“purchaseOrderType”><xsd:sequence>xsd:sequence

<xsd:element name=“shipTo” type=“ThaiAddress”/><xsd:element name=“billTo” type=“ThaiAddress”/>

… </xsd:sequence>

</xsd:complexType>

chapter 02 XML Schema 46

Occurrence Constraints

We can control the number of occurrences of elements by using “minOccurs” and y g“maxOccurs” attributesThe default values of these attributes are 1

The values of these attributes are nonnegative integersg g

The value of “maxOccurs” attribute can also be “unbounded”

chapter 02 XML Schema 47

Constraints for an Optional Element

DTD: An optional element, use symbol ?<!ELEMENT purchaseOrderType (billTo,

hi T t? it >shipTo, comment?, items>XML Schema: use minOccurs=“0”

<xsd:complexType name=“purchaseOrderType><xsd:element ref="comment“ minOccurs="0"/><xsd:element ref= comment minOccurs= 0 />

…</xsd:complexType>

chapter 02 XML Schema 48

Page 13: DTD and XML Schema (1/3) DTD vs. XML Schema (2/3)sirisuda/290371/290371_ch2.pdfChapter 2 XML Schema Content Create XML Document XML Schema DTD vs. XML Schema The “schema” element

Constraints for Multiple Occurrence (1/2)

DTD: A multiple occurrence element, use *<!ELEMENT Items (item*)>

XML Schema: use minOccurs=“0” and XML Schema: use minOccurs= 0 and maxOccurs=“unbounded”

d l T It<xsd:complexType name="Items"><xsd:sequence>

<xsd:element name="item"minOccurs="0“maxOccurs="unbounded">

…</xsd:complexType>

chapter 02 XML Schema 49

Constraints for Multiple Occurrence (2/2)

DTD: A multiple occurrence element , use +<!ELEMENT Items (item+)>

XML Schema XML Schema maxOccurs=“unbounded”

<xsd element name “item” maxOccurs “unbounded” ><xsd:element name= item maxOccurs= unbounded />

DTD: An element with min appearance = 2 max appearance 5 t d it= 5 cannot do it

XML Schema

<xsd:element name=“item” minOccurs=“2” maxOccurs=“5”/>

chapter 02 XML Schema 50

Occurrences of AttributesAttributes may appear once or not at allPossible values of “use” attribute

i drequiredoptionalfixeddefault

The default value of “use” attribute is “optional”Example: <xs:attribute name “id” type “xs:ID”/>Example: <xs:attribute name=“id” type=“xs:ID”/>has the same effect as

<xs:attribute name=“id” type=“xs:ID” use=“optional”/>

chapter 02 XML Schema 51

Explicit Type vs. Anonymous Type

Explicit typeOne in which a name is given to the typeg ypElement that uses the type is generally defined in a different section of the fileObject-oriented in that same explicit type is used as the type for several different elements

I li it t ( l )Implicit type (nameless, anonymous)Use when the type is not needed by multiple elementselements

chapter 02 XML Schema 52

Page 14: DTD and XML Schema (1/3) DTD vs. XML Schema (2/3)sirisuda/290371/290371_ch2.pdfChapter 2 XML Schema Content Create XML Document XML Schema DTD vs. XML Schema The “schema” element

Example of Explicit TypeWhen defining an explicit type, “complexType” or “simpleType” must “name” attribute

<xsd:simpleType name=“zipUnionType”><xsd:union memberTypes=

“ThaiProvinces listOfMyIntType”/>ThaiProvinces listOfMyIntType /></xsd:simpleType>

When declaring an element, we can use the defined type

<element name=“zips” type=“zipUnionType”/>…

<element name=“zips2” type=“zipUnionType”/>chapter 02 XML Schema 53

<element name zips2 type zipUnionType />

Anonymous Type Definitions

A type can be more succinctly defined as an anonymous type

Saves the overhead of having to be named and explicitly referenced

In general you can identify anonymous In general, you can identify anonymous types by

The lack of a “type=“ in an element (or e ac o a type a e e e t (oattribute) declaration andThe presence of an un-named (simple or complex) type definitioncomplex) type definition

chapter 02 XML Schema 54

Example of Anonymous Type<xsd:element name="item" minOccurs=“0”

maxOccurs="unbounded"><xsd:complexType><xsd:complexType>

<xsd:sequence><xsd:element name=“productName” type=“xsd:string”/><xsd:elment name=“quantity” type=“int”/>yp

</xsd:complexType></xsd:element>

chapter 02 XML Schema 55

Element Content

How content of an element gets constructedconstructedThree different ways

C l f i l Complex types from simple typesMixed content

Elements mixed with character contentEmpty content

chapter 02 XML Schema 56

Page 15: DTD and XML Schema (1/3) DTD vs. XML Schema (2/3)sirisuda/290371/290371_ch2.pdfChapter 2 XML Schema Content Create XML Document XML Schema DTD vs. XML Schema The “schema” element

Complex Types from Simple Types

In XML:<USPrice>345.67</USPrice>

In XML Schema:<xsd:element name=“USPrice” type=“decimal”/>yp

In XML:<internationalPrice

currency=“BAHT”>345.23</internationalPrice>

Should we define the type for internationalPrice as complexType or simpleType?

chapter 02 XML Schema 57

Derive Complex Types from Simple Types

<xsd:element name=“internationalPrice”><xsd:complexType>

<xsd simpleContent><xsd:simpleContent><xsd:extension base=“xsd:decimal”>

<xsd:attribute name=“currency”ytype=“xsd:string”/>

</xsd:extension>d i l C t t</xsd:simpleContent>

</xsd:complexType></xsd:element></xsd:element>

chapter 02 XML Schema 58

Mixed Content1Mixed content contains both sub-elements and character dataIn XML: <salutation>

Dear Dr.<name>Prawase Wasi</name></salutation>

In XML Schema:

<xsd:element name="salutation"><xsd:complexType mixed="true">

<xsd:sequence><xsd:sequence><xsd:element name="name“ type="xsd:string"/>

</xsd:sequence></xsd:complexType>

chapter 02 XML Schema 59

p yp</xsd:element>

Mixed Content2In XML:

<quote>This is an interesting quote<text>To live is to choose …</text><by>Kofi Anan</by><by>Kofi Anan</by>

</quote>

In XML Schema:

<xs:element name="quote">l d<xs:complexType mixed="true">

<xs:choice minOccurs="0“ maxOccurs="unbounded"><xs:element name="text" type="xs:string"/>< l t b t t i ><xs:element name="by" type="xs:string"/>

</xs:choice></xs:complexType>

</xs element>chapter 02 XML Schema 60

</xs:element>

Page 16: DTD and XML Schema (1/3) DTD vs. XML Schema (2/3)sirisuda/290371/290371_ch2.pdfChapter 2 XML Schema Content Create XML Document XML Schema DTD vs. XML Schema The “schema” element

Empty Content1In XML:

<internationalPrice currency=“BAHT” value=“345.23”/>y

In XML Schema:

<xsd:element name=“internationalPrice”><xsd:complexType>

<xsd:complexContent>xsd:complexContent <xsd:restriction base=“xsd:anyType”>

<xsd:attribute name=“currency” type=“xsd:string”/><xsd:attribute name=“value” type=“xsd:decimal”/>

…</xsd:element>

chapter 02 XML Schema 61

Empty Content2In XML:

<internationalPrice currency=“BAHT” value=“345 23”/><internationalPrice currency= BAHT value= 345.23 />

In XML Schema:

<xsd:element name=“internationalPrice”><xsd:complexType>

<xsd:attribute name=“currency” type=“xsd:string”/><xsd:attribute name=“value” type=“xsd:decimal”/>

</xsd:complexType></xsd:element>

chapter 02 XML Schema 62

simpleContent vs. complexContent

simpleContent indicates that the content model of the new type contains only character data and no element declarationcharacter data and no element declarationcomplexContent indicates that the content model of the new type containsyp

Sub-elementsSub-elements and character data (mixed content)content)Nothing (empty content)

A complex type defined without l C i d h h d complexContent interpreted as shorthand

for complex content that restricts anyType

chapter 02 XML Schema 63

anyType

Base type from which all simple and complex types are derivedDoes not contain its contents in any wayDefault type when no type is specified<xsd:element name=“anything”

type=“xsd:anyType”/> is same as<xsd:element name=“anything”/><xsd:element name= anything />

Use more constrained types whenever possiblep

chapter 02 XML Schema 64

Page 17: DTD and XML Schema (1/3) DTD vs. XML Schema (2/3)sirisuda/290371/290371_ch2.pdfChapter 2 XML Schema Content Create XML Document XML Schema DTD vs. XML Schema The “schema” element

Extension vs. Restriction

Schema gives you these derivation options:p

extension:Add elements to the end of the childrenAdd additional attributes

restriction:Put additional constraints on the valueRestricting the possible values or occurrences of sub elementsoccurrences of sub-elements

chapter 02 XML Schema 65

“simpleContent” Element Example

In XML:<amount currency=“BAHT”>1000</amount>

XML Schema:

<xsd complexType name “AmountType”><xsd:complexType name=“AmountType”> <xsd:simpleContent>

<xsd:extension base=“xsd:string”><xsd attribute name “currency” type “xsd string”><xsd:attribute name= currency type= xsd:string ></xsd:attribute>

</xsd:extension> </xsd:simpleContent> </xsd:simpleContent></xsd:complexType>

chapter 02 XML Schema 66

“complexContent” Element Example

<xsd:complexType name="bookExtendType"><xsd:complexContent>

<xsd extension base bookType ><xsd:extension base="bookType"><xsd:sequence>

<xsd:element name="bookBib"type="xsd:string"/>

</xsd:sequence>d t i</xsd:extension>

</xsd:complexContent></xsd:complexType></xsd:complexType>

chapter 02 XML Schema 67

Restriction for simpleType Example

Put additional constraints on the valuevalue

<xsd:simpleType name="pageType">p yp p g yp<xsd:restriction base="xsd:integer">

<xsd:minInclusive value="10"/><xsd:maxInclusive value="5000"/><xsd:maxInclusive value 5000 />

</xsd:restriction></xsd:simpleType>

chapter 02 XML Schema 68

Page 18: DTD and XML Schema (1/3) DTD vs. XML Schema (2/3)sirisuda/290371/290371_ch2.pdfChapter 2 XML Schema Content Create XML Document XML Schema DTD vs. XML Schema The “schema” element

Restriction of complexType Example

Restricting the possible values or occurrences of subelements

<xsd:complexType name="book3Type"><xsd:complexContent>

<xsd:restriction base="bookType"><xsd:sequence>

<xsd:element name="bookTitle“type="xsd:string"/><xsd:element name="bookContent“ type="xsd:string"

“minOccurs="1“ axOccurs="1"/></xsd:sequence>

</xsd:restriction>d l C t t </xsd:complexContent>

</xsd:complexType>

chapter 02 XML Schema 69

Building Content Models

XML Schema enables groups of elements to be defined and namedelements to be defined and named

Elements can be used to build up the content models of complex typescontent models of complex types

The group elements “sequence”, “all”, d “ h i ” t l h l t i and “choice” control how elements in

the group should appear

chapter 02 XML Schema 70

“sequence” Element“sequence” specifies that the elements in the group must appear in the same order (sequence) as they are declaredXML Schema

<xsd:complexType name="purchaseOrderType"><xsd:complexType name= purchaseOrderType ><xsd:sequence>

<xsd:element name="shipTo“ type="address"/><xsd:element name "billTo“ type "address"/><xsd:element name= billTo type= address />

…</xsd:sequence>

</ d l T ></xsd:complexType>

chapter 02 XML Schema 71

“sequence” Element XML Example

Valid XML fragment<purchaseOrder>

hi T

Invalid XML fragment<purchaseOrder>

<shipTo>…

</shipTo>

<billTo>…

</shipTo><billTo>

</billTo><shipTo>

</billTo>…h O d

…</shipTo>

</purchaseOrder> …</purchaseOrder>

chapter 02 XML Schema 72

Page 19: DTD and XML Schema (1/3) DTD vs. XML Schema (2/3)sirisuda/290371/290371_ch2.pdfChapter 2 XML Schema Content Create XML Document XML Schema DTD vs. XML Schema The “schema” element

“choice” Element“choice” allows only one of its children to appear in an instance

<xsd:complexType name=“currencyType”><xsd:choice>sd:c o ce

<xsd:element name=“thaiBaht” type=“xsd:decimal”/><xsd:element name=“usDollar” type=“xsd:decimal”/>

</xsd:choice></xsd:choice></xsd:complexType>

chapter 02 XML Schema 73

“choice” Element XML Example

Valid XML fragment Invalid XML fragment

<currency><thaiBaht>40</thaiBaht>

</currency>

<currency><thaiBaht>40</thaiBaht><usDollar>1</usDollar></currency> <usDollar>1</usDollar>

</currency>

chapter 02 XML Schema 74

“all” Element“all” indicates that all the elements in the group may appear once or not at all, and they may appear in any order

<xsd:complexType name=“nameType”>p yp yp<xsd:all>

<xsd:element name=“fname” type=“xsd:string”/><xsd:element name=“lname” type=“xsd:string”/>

</xsd:all></xsd:complexType>

chapter 02 XML Schema 75

“all” Element XML Example

Invalid XML Fragment<name>

Valid XML Fragment

<fname>Panya</fname>

<name><fname>Panya

</name>fname Panya

</fname><lname>Nirankul<lname>Nirankul</lname>

< ></name>

chapter 02 XML Schema 76

Page 20: DTD and XML Schema (1/3) DTD vs. XML Schema (2/3)sirisuda/290371/290371_ch2.pdfChapter 2 XML Schema Content Create XML Document XML Schema DTD vs. XML Schema The “schema” element

“group” Element (1/2)Define related sets of elementsNaming the group so that we canreference it in another complex type definitionanother complex type definition

<xsd:group name="shipAndBill"><xsd:sequence><xsd:sequence>

<xsd:element name="shipTo“ type=“ThaiAddress"/><xsd:element name="billTo“ type=“ThaiAddress"/>

</xsd:sequence></xsd:sequence></xsd:group>

chapter 02 XML Schema 77

“group” Element (2/2)<xsd:complexType name="PurchaseOrderType">

<xsd:sequence><xsd:choice>

<xsd:group ref="shipAndBill"/><xsd:element name="singleUSAddress“

type="USAddress"/></xsd:choice><xsd:element ref="comment“ minOccurs="0"/><xsd:element name="items“ type="Items"/>

</xsd:sequence><xsd:attribute name="orderDate“ type="xsd:date"/>

</xsd:complexType>

chapter 02 XML Schema 78

“attributeGroup” Element

Define related sets of attributes

Naming the group so that we can Naming the group so that we can reference it in another complex type definitiondefinition

<xsd:attributeGroup name="bookDescription">d b b k d<xsd:attribute name="bookID“ type="xsd:integer"/>

<xsd:attribute name="numberPages“ type="pageType"/></xsd:attributeGroup>

chapter 02 XML Schema 79

“attributeGroup” Element

<xsd:complexType name=“bookType”><xsd:sequence>

<xsd:element name=“bookTitle” type=“xsd:string”/><xsd:element name=“bookContent” type=“xsd:string”/>

d</xsd:sequence><xsd:attributeGroup ref=“bookDescription”/>d l</xsd:complexType>

chapter 02 XML Schema 80

Page 21: DTD and XML Schema (1/3) DTD vs. XML Schema (2/3)sirisuda/290371/290371_ch2.pdfChapter 2 XML Schema Content Create XML Document XML Schema DTD vs. XML Schema The “schema” element

Nil Values

Sometimes it is desirable to represent a “null” value being sent to or from a grelational database with an element that is presentXML Schema:

<xsd:element name=“shipDate” ptype=“xsd:date” nillable=“true”/>

XML:<shipDate xsi:nil=“true”></shipDate>

chapter 02 XML Schema 81

Annotations (1/2)

XML Schema provides three elements for annotating schemas for the for annotating schemas for the benefit of both human readers and applicationsapplicationsThese three elements are “ t ti ” “d t ti ” d “annotation”, “documentation”, and “appInfo”

chapter 02 XML Schema 82

Annotations (2/2)

“annotation” element has two element children:

“appinfo” - typically for additional application information (e.g., relational tables, constraint rules, Java method mappings)“d t ti ” i t d d t t i “documentation” – intended to contain the human-readable documentation for the constructthe construct

chapter 02 XML Schema 83

Annotations Example1

<xsd:annotation><xsd:documentation xml:lang "en"><xsd:documentation xml:lang="en">Purchase order schema for abc.comCopyright 2004 abc.com</xsd:documentation></xsd:documentation>

</xsd:annotation>

chapter 02 XML Schema 84

Page 22: DTD and XML Schema (1/3) DTD vs. XML Schema (2/3)sirisuda/290371/290371_ch2.pdfChapter 2 XML Schema Content Create XML Document XML Schema DTD vs. XML Schema The “schema” element

Annotations Example2<xsd:annotation>

<xsd:appinfo><java method name “setBingo” ><java:method name=“setBingo”/>

</xsd:appinfo><xsd:documentationxmlns=“http://www.w3.org/1999/xhtml”><p>This element represents a bingo game.</p>

d d t ti</xsd:documentation></xsd:annotation>

chapter 02 XML Schema 85

Summary

DTD vs. XML SchemaW3C Schema Elements

element, attributesimpleType, complexTypep yp , p ypsimpleContent, complexContentminOccurs, maxOccursenumeration, list, unionsequence, choice, all, groupannotation, documentation

chapter 02 XML Schema 86

ตัวอยางตัวอยาง libarary.xml

chapter 02 XML Schema 87 chapter 02 XML Schema 88

Page 23: DTD and XML Schema (1/3) DTD vs. XML Schema (2/3)sirisuda/290371/290371_ch2.pdfChapter 2 XML Schema Content Create XML Document XML Schema DTD vs. XML Schema The “schema” element

chapter 02 XML Schema 89 chapter 02 XML Schema 90

chapter 02 XML Schema 91