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
23
Embed
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
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
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
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>
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)>
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
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:
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
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 >
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:
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:
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”/>
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
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>…
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
Default Element Value Example
The simple empty element which its default value specified in XML schemap
<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
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
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
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
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
Example of Explicit TypeWhen defining an explicit type, “complexType” or “simpleType” must “name” attribute
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”
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
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>
“group” Element (1/2)Define related sets of elementsNaming the group so that we canreference it in another complex type definitionanother complex type definition
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
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