XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for...
-
date post
22-Dec-2015 -
Category
Documents
-
view
230 -
download
1
Transcript of XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for...
XML Schema – Part 1
1. Introduction to XML-Schema
2. Schema basics
3. Mechanisms (strategies) for Designing Schema
4. Creating your own Datatypes
Simple XML?<xml version="1.0>?"
•<purchaseOrder orderDate="1999-10-20">• <shipTo country="US">• <name>Alice Smith</name>• <street>123 Maple Street</street>• <city>Mill Valley</city>• <state>CA</state>• <zip>90952</zip>• </shipTo>
<items>• <item partNum="872-AA">• <productName>Lawnmower</productName>• <quantity>1</quantity>• <USPrice>148.95</USPrice>• <comment>Confirm this is electric</comment>• </item>• <item partNum="926-AA">• <productName>Baby Monitor</productName>• <quantity>1</quantity>• <USPrice>39.98</USPrice>• <shipDate>1999-05-21</shipDate>• </item>• </items>•</purchaseOrder>
XML1. XML is for structuring data
•spreadsheets, address books, configuration parameters, financial transactions, and technical drawings.
•XML is text format for representing structured data. XML makes it easy for a computer to generate data, read data, and ensure that the data structure is unambiguous. XML is extensible, platform-independent, and it supports internationalization and localization.
2. XML looks a bit like HTML
• Like HTML, XML makes use of tags (words bracketed by '<' and '>') and attributes (of the form name="value"). While HTML specifies what each tag and attribute means, and often how the text between them will look in a browser, XML uses the tags only to delimit pieces of data, and leaves the interpretation of the data completely to the application that reads it. if you see "<p>" in an XML file, do not assume it is a paragraph. Depending on the context, it may be a price, a parameter, a person, a p... (and who says it has to be a word with a "p"?).
• XML can keep data separated from your HTML
3. XML is a group of technologies
•XML 1.0 is the specification that defines what "tags" and "attributes" are. Beyond XML 1.0, "the XML family" is a growing set of modules that offer useful services to accomplish important and frequently demanded tasks. XPointer and XFragments are syntaxes in development for pointing to parts of an XML document. An XPointer is a bit like a URL, but instead of pointing to documents on the Web, it points to pieces of data inside an XML file. XSLT, a transformation language used for rearranging, adding and deleting tags and attributes. The DOM is a standard set of function calls for manipulating XML (and HTML) files from a programming language. XML Schemas help developers to precisely define the structures of their own XML-based formats. There are several more modules and tools available or under development. Keep an eye on W3C's technical reports page.
Purpose of XML Schemas (and DTDs)
• Specify:– the structure of instance documents
• "this element contains these elements, which contains these other elements, etc"
– the datatype of each element/attribute• "this element shall hold an integer with the range 0
to 12,000" (DTDs don't do too well with specifying datatypes like this)
What is a Schema?• A piece of information marked up by presence of tags is called element.
• Elements may further be enriched by attaching name-value pairs called attributes.
• Like Data Type Definitions (DTDs)
• Define the document's structure.
•Elements and attributes definition•Empty or text content elements. •Default values for attributes and elements.
• More powerful and flexible than DTDs.• XML syntax.
• Agreed upon Schema• Exchanging XML data. • Verify the received data against schema. • Valid and well-formed.
?>xml version="1.0" encoding="utf-8 <?">xs:schema xmlns:xs
="http://www.w3.org/2001/XMLSchema<"
>xs:element name="book <"> xs:complexType <
<xs:sequence> <xs:element name="title“ type="xs:string"/> <xs:element name="author“ type="xs:string"/> <xs:element name="character“ maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="name"type="xs:string"/> <xs:element name=“dob" type="xs:date“/> </xs:sequence>
/> xs:complexType < </xs:element> </xs:sequence> <xs:attribute name="isbn" type="xs:string"/>
/> xs:complexType </> xs:element<
/>xs:schema> Example 1
Structure of the Data
BOOK
titleauthor character
name dob
isbn
We want to define this structure in the schema
>book isbn="0836217462"> </book<
>title> Pets </title< >author>M.Cat</author<
>name>Snoopy</name< >dob>1966</dob<
>character></character<
<?xml version="1.0“ encoding="UTF-8"?>
<book isbn="0836217462"> <title> Pets </title>
<author>M. Cat</author> <character> <name>Snoopy</name> <dob>1950</dob> </character> <character> <name>Patty</name> <dob>1966</dob> </character> </book>
?>xml version="1.0" encoding="utf-8 <?">xs:schema xmlns:xs
="http://www.w3.org/2001/XMLSchema<"
>xs:element name="book <"> xs:complexType <
<xs:sequence> <xs:element name="title“type="xs:string"/> <xs:element name="author“type="xs:string"/> <xs:element name="character"
maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="name"type="xs:string"/> <xs:element name=“dob" type="xs:date"/> </xs:sequence>
/> xs:complexType </> xs:element <
</xs:sequence> > xs:attribute name="isbn"
type="xs:string </"/> xs:complexType <
/>xs:element</>xs:schema <
XML Schema
Example 1
Explanations:• Type (xsd:string) is prefixed by the namespace prefix
associated with XML Schema, indicating a predefined XML Schema datatype:
<xsd:element name="name" type="xsd:string"/>
• Specify both minOccurs and maxOccurs . • unbounded value , default value (one).• Only in local definition• Facets
<xsd:element name="character" minOccurs="0" maxOccurs="unbounded”> …/…
</xsd:element>
• Attributes after element declarations.• Compositors:
• Sequence (ordered sequence)• All (no order but all)• Choice
Simple/Complex Data type• SimpleType is reserved for data types holding only
values and no attribute or element sub-nodes.
>xsd:element name="title" type="xsd:string"/< >xsd:element name="author" type="xsd:string"/<
• ComplexType - data types which aren’t simple(Book element has attributes and children elements)
<xs:element name="character" maxOccurs="unbounded">
<xs:complexType> <xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name=“dob" type="xsd:date“/> </xsd:sequence>
<xsd:attribute name=“…" type=“..” /> </xsd:complexType>
</xsd:element>
Compositors: Using <sequence> and <choice<<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.binary.org" xmlns="http://www.binary.org" elementFormDefault="qualified"> <xsd:element name="life"> <xsd:complexType> <xsd:sequence minOccurs="0" maxOccurs="unbounded"> <xsd:sequence minOccurs="0" maxOccurs="unbounded"> <xsd:element name="work" type="xsd:string"/> <xsd:element name="eat" type="xsd:string"/> </xsd: sequence> <xsd:choice> <xsd:element name="work" type="xsd:string"/> <xsd:element name="play" type="xsd:string"/> </xsd:choice> <xsd:element name="sleep" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element></xsd:schema>
Compositors: Expressing Any Order<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified"> <xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element name="Book" maxOccurs="unbounded"> <xsd:complexType> <xsd:all> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:all> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> …….
Problem: create an element, Book, which contains Author, Title, Date, ISBN, and Publisher, in any order (Note: this is very difficult and ugly with DTDs).
Namespaces• A namespace is a collection of names used as element or attribute names in an XML document.
• to qualify element names to make them unique• to avoid conflicts between elements with the same name.
• xmlns - keyword for a namespace declaration..• The idea is that when you are dealing with XML documents from ten different (external) sources, name collisions can occur. If you use namespaces, you are distinguishing one element from another based on the namespace prefix. myNameSpace:Employee is not the same as yourNameSpace:Employee. When you declare a namespace you also give it an unique URI ( Universal Resource). •explicit or default declaration.
Explicit and Default namespace declaration• Explicit declaration (bk – qualifier)<BOOKS> <bk:BOOK xmlns:bk="http://www.books.org"
xmlns:money="http://www.money.org"> <bk:TITLE>Tourist guide</bk:TITLE> <bk:PRICE money:currency="US Dollar"> 22.95 </bk:PRICE> </bk:BOOK></BOOKS>• Default<BOOKS> <BOOK xmlns="http://www.books.org"> <TITLE> Tourist guide </TITLE> <PRICE currency="US Dollar">22.95</PRIC>
/> BOOK</>BOOKS<
•Identified by a Universal Resource Identifier (URI) or by Uniform Resource Locator (URL) •It doesn't matter what the URI points to. URIs are used because they are globally unique across the Internet.
?>xml version="1.0" encoding="utf-8 <?">xs:schema xmlns:xs ="http://www.w3.org/2001/XMLSchema<"
<xs:element name="book"> > xs:complexType <
<xs:sequence> <xs:element name="title“ type="xs:string"/> <xs:element name="author“ type="xs:string"/> <xs:element name="character“ maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="name"type="xs:string"/> <xs:element name="since" type="xs:date"/> </xs:sequence>
/> xs:complexType < </xs:element> </xs:sequence> <xs:attribute name="isbn" type="xs:string"/>
/> xs:complexType </> xs:element</>xs:schema <
The elements anddatatypes thatare used to constructschemas - schema - element - complexType - sequence - stringcome from the http://…/XMLSchemanamespace
Example 1Book.xsd
enc
?>xml version="1.0" encoding="utf-8<?" >xs:schema xmlns:xs ="http://www.w3.org/2001/XMLSchema<"
targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified">
>xs:element name="book <"> xs:complexType <
<xs:sequence> <xs:element name="title“ type="xs:string"/> <xs:element name="author“ type="xs:string"/> <xs:element name="character“ maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="name"type="xs:string"/> <xs:element name="since" type="xs:date"/> </xs:sequence>
/> xs:complexType < </xs:element> </xs:sequence> <xs:attribute name="isbn" type="xs:string"/>
/> xs:complexType </> xs:element</>xs:schema<
The default namespace ishttp://www.books.org which is the targetNamespace!
Example 1
“qualified“ This is a directive to any instance documents which conform to this schema: Any elements used by the instance document which were declared in this schema must be namespace qualified.
The Book in what namespace? Since thereis no namespace qualifier it is referencing the Book element in the default namespace, which is the targetNamespace! Thus, this is a reference to the Book element declaration in this schema.
XML Schema Namespace
element
complexType
schema
sequence
http://www.w3.org/2001/XMLSchema
string
integer
boolean
)schema-for-schemas(
Book Namespace(target namespace)
ISBN
Book
Character
Title
Author
name
http://www.books.org
dob
<?xml version="1.0"?><Book xmlns ="http://www.books.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.books.org Book.xsd">
… <Title>My Life and Times</Title>
<Author>Paul McCartney</Author> ...
</Book>1. First, using a default namespace declaration, tell the schema-validator that all of the elementsused in this instance document come from the http://www.books.org namespace.2. Second, with schemaLocation tell the schema-validator that the http://www.books.org namespace is defined by Book.xsd (i.e., schemaLocation contains a pair of values).3. Third, tell the schema-validator that the schemaLocation attribute we are using is the one in the XML Schema-instance namespace.
1
2
3
XMLSchema and XML instance document
Referencing a schema in an XML instance document
Book.xml Book.xsd
targetNamespace="http://www.books.org"
schemaLocation="http://www.books.org Book.xsd"
- defines elements in namespace http://www.books.org
- uses elements from namespace http://www.books.org
A schema defines a new vocabulary. Instance documents use that new vocabulary.
Note multiple levels of checking
Book.xml Book.xsd XMLSchema.xsd(schema-for-schemas)
Validate that the xml documentconforms to the rules describedin Book.xsd
Validate that Book.xsd is a validschema document, i.e., it conformsto the rules described in theschema-for-schemas
Russian Doll Design?>xml version="1.0 <? “
>xs:schema xmlns:xs ="http://www.w3.org/2001/XMLSchema<"
>xs:element name="book <"> xs:complexType <
<xs:sequence> <xs:element name="title“ type="xs:string"/> <xs:element name="author“ type="xs:string"/> <xs:element name="character“ maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="name"type="xs:string"/> <xs:element name=“dob" type="xs:date”/> </xs:sequence>
/> xs:complexType < </xs:element> </xs:sequence> <xs:attribute name="isbn" type="xs:string"/>
/> xs:complexType </> xs:element</>xs:schema<
Example 1
Flat Catalog<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name=“author" type="xs:string"/> <xs:element name="title" type="xs:string"/>
<xs:element name="name" type="xs:string"/> <xs:element name=“dob" type="xs:date"/>
<xs:attribute name="isbn" type="xs:string"/> <!-- definition of complex type elements --> <xs:element name="character">
<xs:complexType> <xs:sequence>
<xs:element ref="name"/> <xs:element ref=“dob"/>
</xs:sequence> </xs:complexType>
</xs:element> <xs:element name="book">
<xs:complexType> <xs:sequence>
<xs:element ref="title"/> <xs:element ref="character"
minOccurs="0” maxOccurs="unbounded</" </xs:sequence> <xs:attribute ref="isbn"/>
</xs:complexType></xs:element> </xs:schema>
Example 2
First definition of the simple types
These are global definitions
Next definition of attributes
Next, definition of complex types
The definition of the cardinality is done when the elements are referenced
Summary Mechanisms of definitions
• Russian Doll Design:• Tight structure• Multiple occurrences of a same element name with different definitions.• Depth in the embedded definitions• Hardly readable and difficult to maintain when documents are complex.
• Flat Catalog:• Catalog of all the elements. • references to element and attribute definitions that need to be within the scope of the referencer.• Using a reference to an element or an attribute is somewhat comparable to cloning an object. The element or attribute is defined first, and it can be duplicated at another place in the document structure by the reference mechanism, in the same way an object can be cloned. The two elements (or attributes) are then two instances of the same class.
Anonymous types (no name)
<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified"> <xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element name="Book" maxOccurs="unbounded"> <xsd:complexType> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element></xsd:schema>
Russian Doll Design
Example 3
Named Types<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified"> <xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element name="Book" type="BookPublication" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:complexType name="BookPublication"> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> </xsd:complexType> …/….. <xsd:element name=“Magazine" type="BookPublication" maxOccurs="unbounded"/>…/..</xsd:schema>
Named type
The advantage ofsplitting out Book'selement declarationsand wrapping themin a named type isthat now this typecan be reused byother elements.
Example 4
Please note that:
<xsd:element name="A"> <xsd:complexType> <xsd:sequence> <xsd:element name="B" …/> <xsd:element name="C" …/> </xsd:sequence> </xsd:complexType></xsd:element>
is equivalent to:
<xsd:element name="A" type="foo"/>
<xsd:complexType name="foo"> <xsd:sequence> <xsd:element name="B" …/> <xsd:element name="C" …/> </xsd:sequence></xsd:complexType>
Element A instantiates complexType foo.
Element A has the complexType definitioninlined in the elementdeclaration.
Summary Definition Mechanisms• Russian Doll Design:
• Tightly follows the structure • Define each element and attribute within its context and to allow multiple occurrences of a same element name to carry different definitions.• Hardly readable and difficult to maintain when documents are complex.
• Flat Catalog:• Catalog of all the elements available in the instance document and, for each of them, lists of child elements and attributes. • Use references to element and attribute definitions that need to be within the scope of the referencer.• Somewhat comparable to cloning an object. The element or attribute is defined first, and it can be duplicated at another place in the document structure by the reference mechanism, in the same way an object can be cloned. The two elements (or attributes) are then two instances of the same class.
• Named Types• Give a name to the simpleType and complexType elements.• Comparable to defining a class and using it to create an object
• Capture the semantics in the XML Schema
• The <annotation> element is used for documenting the schema, both for humans and for programs.
– Use <documentation> for providing a comment to humans
– Use <appinfo> for providing a comment to programs
• The content is any well-formed XML
• Note that annotations have no effect on schema validation
Annotating Schemas
Annotating Schemas<xsd:annotation> <xsd:documentation> The following constraint is not expressible with XML Schema: The value of element A should be greater than the value of element B. So, we need to use a separate tool (e.g., Schematron) to check this constraint. We will express this constraint in the appinfo section (below).</xsd:documentation> <xsd:appinfo> <assert test="A > B">A should be greater than
B</assert> </xsd:appinfo><xsd:/annotation>
Code to check the structure and content(datatype) of the data
Code to actuallydo the work
"In a typical program, up to 60% of the code is spent checking the data!"
Save time and money using XML Schemas
Continued -->
Code to check the structure and content
of the data
Code to actuallydo the work
If your data is structured asXML, and there is a schema,then you can hand the data-checking task off to a schema validator.
Thus, your code is reducedby up to 60%!!!
Big $$ savings!
Save time and money using XML Schemas (cont.)
Classic use of XML Schemas(Trading Partners )
Supplier Consumer
XML data
SchemaValidator
XMLSchema
Softwareto Process
D.
“D. isokay" D
(Schema at third-party, neutral web site)
XML Schema --> GUI
SchemaGUI
Builder HTMLSupplier
WebServer
XML Schema --> Smart Editor
Schema Smart Editor (e.g., XML Spy)
Helps you build yourinstance documents.For example, it popsup a menu showing you what is valid next.It knows this by lookingat the XML Schema!
Element Substitution• We can define a group of substitutable elements (called a
substitutionGroup) by declaring an element (called the head) and then declaring other elements which state that they are substitutable for the head element.
<xsd:element name="subway" type="xsd:string"/><xsd:element name="T" substitutionGroup="subway"
type="xsd:string"/>
subway is the head elementT is substitutable for subway
<xsd:element name="subway" type="xsd:string"/> <xsd:element name="T" substitutionGroup="subway" type="xsd:string"/> <xsd:element name="transportation"> <xsd:complexType> <xsd:sequence> <xsd:element ref="subway"/> </xsd:sequence> </xsd:complexType> </xsd:element>
<transportation> <subway>Red Line</subway> </transportation>
Instance doc:
<transportation> <T>Red Line</T> </transportation>
Alternative instance doc(substitute T for subway):
This example shows the <subway> element being substituted with the <T> element.
Schema
<xsd:element name="subway" type="xsd:string"/><xsd:element name="metro" substitutionGroup="subway" type="xsd:string"/> <xsd:complexType name="transport"> <xsd:sequence> <xsd:element ref="subway"/> </xsd:sequence> </xsd:complexType><xsd:element name="transportation" type="transport"/><xsd:element name="transporte" substitutionGroup="transportation"/>
<transportation> <subway>Red Line</subway> </transportation>
Schema
Instance doc:
<transporte> <metro>Linea Roja</metro> </transporte>
Alternative instance doc(customized for Spanish clients):
Substitution Groups (Example)
Remarks : Transitive A, B, C Not Symmetric
Blocking substitution: <xsd:element name="…" type="…" block="substitution"/>
Creating your own Datatypes
• Simple Types Derivationfor example, you want to create your “special” string which will be at length 10 and etc.
• Complex Types Derivation
• xs:restriction elements. •The different kind of restrictions that can be applied on a datatype are called facets.
• Union of datatypes.
• White space separated lists.
Creating your own Datatypes(Simple datatypes)
<xsd:simpleType name="TelephoneNumber“ type=“xsd:string”/><xsd:simpleType name="TelephoneNumber"> <xsd:restriction base="xsd:string"> <xsd:length value=“10"/> <xsd:pattern value="\d{2}-\d{7}"/> </xsd:restriction></xsd:simpleType>
1. This creates a new datatype called 'TelephoneNumber'. 2. Elements of this type can hold string values, 3. But the string length must be exactly 10 characters long and 4. The string must follow the pattern: dd-ddddddd, where 'd' represents a 'digit'. (Obviously, in this example the regular expression makes the length facet redundant.) patterns are specified using Regular Expressions
5. In general we use: restriction, facet, value.6. Restriction with Complex Type.
1
2
3
4
Creating your own Datatypes
An element declared to be of type TelephoneNumbermust be a string of length=10 and the string must follow the pattern: 2 digits, dash, 7 digits.
<xsd:simpleType name="shape"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="circle"/> <xsd:enumeration value="triangle"/> <xsd:enumeration value="square"/> </xsd:restriction></xsd:simpleType>
<xsd:simpleType name="TelephoneNumber"> <xsd:restriction base="xsd:string"> <xsd:length value=“10"/> <xsd:pattern value="\d{2}-\d{7}"/> </xsd:restriction></xsd:simpleType>
An element declared to be of type shape must be a string with a value of either circle, or triangle, or square.
Multiple Facets - "and" them together, or "or" them together?
General Form of Creating a Simple Datatype by Specifying Facet Values
<xsd:simpleType name= "name"> <xsd:restriction base= "xsd:source"> <xsd:facet value= "value"/> <xsd:facet value= "value"/> … </xsd:restriction></xsd:simpleType>
Facets: - length - minlength - maxlength - pattern - enumeration - minInclusive - maxInclusive - minExclusive - maxExclusive ...
Sources: - string - boolean - number - float - double - duration - dateTime - time ...
The different kind of restrictions that can be applied on a datatype are called facets
Creating your own Datatypes
• A new datatype can be defined from an existing datatype (called the "base" type) by specifying values for one or more of the optional facets for the base type.
• Example. The string primitive datatype has six optional facets:– length
– minLength
– maxLength
– pattern
– enumeration
– whitespace (legal values: preserve, replace, collapse)
Creating a simpleType from another simpleType
• Thus far we have created a simpleType using one of the built-in datatypes as our base type.
• However, we can create a simpleType that uses another simpleType as the base.
Example: creating simpleTypesfrom another simpleTypes
1. simpleType that uses a built-in base type:
<xsd:simpleType name= “VeryBigInteger"> <xsd:restriction base="xsd:integer"> <xsd:minInclusive value="-1290"/> <xsd:maxInclusive value="29035"/> </xsd:restriction></xsd:simpleType>
2. simpleType that uses another simpleType as the base type:
<xsd:simpleType name= " MyInteger "> <xsd:restriction base=“VeryBigInteger "> <xsd:minInclusive value="0"/> <xsd:maxInclusive value="120"/> </xsd:restriction></xsd:simpleType>
Creating Simple Datatypes(list)
>xs:simpleType name="TelephoneBook<" <xs:list itemType=“TelephoneNumber"/>
</xs:simpleType>
< TelephoneBook >02-1234567 03-9876543</ TelephoneBook >
• xs:list defines a whitespace-separated list of values.• “TelephoneNumber“ – from the previous example• You cannot create list types from existing list types, nor from complex types• Facets can be applied to list types ,such as: length, minLength, maxLength• It allows <TelephoneBook> to contain an arbitrarily long list of TelephoneNumbers
Creating simpleType - Union <xsd:simpleType name="name"> <xsd:union memberTypes="space delimited simpleTypes"/></xsd:simpleType> <xsd:simpleType name="TomsFamily"> .../… </xsd:simpleType> <xsd:simpleType name="RogersFamily">
../… </xsd:simpleType> <xsd:simpleType name="CostelloFamily"> <xsd:union memberTypes="TomsFamily RogersFamily "/> </xsd:simpleType>
Creating simpleType - UnionAlternatively,
<xsd:simpleType name="name"> <xsd:union> <xsd:simpleType> … </xsd:simpleType> <xsd:simpleType> … </xsd:simpleType> … </xsd:union> </xsd:simpleType>
Creating Simple Datatypes (union)>xs:simpleType name="isbnType<"
<xs:union> <xs:simpleType>
<xs:restriction base="xs:string"> <xs:pattern value="[0-9]{10}"/>
</xs:restriction> </xs:simpleType>
<xs:simpleType> <xs:restriction base="xs:NMTOKEN">
<xs:enumeration value="TBD"/> <xs:enumeration value="NA"/>
</xs:restriction> </xs:simpleType>
/>xs:union< />xs:simpleType<
NMTOKEN simple type (like string and etc.)used to define only attributes US, BrésilNow isbnType may receive the union of simple types NMTOKEN or string
Complex Datatype Derivation• We can do a form of subclassing Complex
Type definitions: "derived types“.– derive by restriction: create a type which is a
subset of the base type. There are two ways to subset the elements:
• redefine a base type element to have a restricted range of values, or
• redefine a base type element to have a more restricted number of occurrences.
– derive by extension: extend the parent complexType with more elements
<xsd:complexType name="Publication"> <xsd:sequence> <xsd:element name="Title" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Author"type="xsd:string"maxOccurs="unbounded"/> <xsd:element name="Date" type="xsd:gYear"/> </xsd:sequence></xsd:complexType ><xsd:complexType name="BookPublication"> <xsd:complexContent> <xsd:extension base="Publication"> <xsd:sequence> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> </xsd:extension> </xsd:complexContent></xsd:complexType >
<xsd:element name="Book" type="BookPublication" maxOccurs="unbounded"/>
Title
AuthorDatePublication
ISBN
Publisher BookPublication
Derive by Restriction<xsd:complexType name="Publication"> <xsd:sequence> <xsd:element name="Title" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Date" type="xsd:gYear"/> </xsd:sequence></xsd:complexType><xsd:complexType name= "SingleAuthorPublication"> <xsd:complexContent> <xsd:restriction base="Publication"> <xsd:sequence> <xsd:element name="Title" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:gYear"/> </xsd:sequence> </xsd:restriction> </xsd:complexContent></xsd:complexType>
Elements of type SingleAuthorPublication will have 3 child elements - Title, Author, and Date.However, there must be exactly one Author element.Note that in the restriction type you must repeat all the declarations from the base type (except whenthe base type has an element with minOccurs="0" and the subtype wishes to delete it. ).
Prohibiting Derivations
• Sometimes we may want to create a type and disallow all derivations of it, or just disallow extension derivations, or disallow restriction derivations.– Rationale: "For example, I may create a complexType and make it
publicly available for others to use. However, I don't want them to extend it with their proprietary extensions or subset it to remove, say, copyright information."
<xsd:complexType name="Publication" final="#all" …> Publication cannot be extended nor restricted
<xsd:complexType name="Publication" final="restriction" …> Publication cannot be restricted
<xsd:complexType name="Publication" final="extension" …> Publication cannot be extended