| Introduction to Schema |
|
| Evolution of XML |
|
| What is XML? |
|
| The eXtensible Markup Language (XML) is a general-purpose markup language. Its primary purpose is to facilitate the sharing of data across different information systems, particularly via the Internet. |
|
| It is a simplified subset of the Standard Generalized Markup Language (SGML), and is designed to be relatively human-legible. By adding semantic constraints, application languages can be implemented in XML. These include XHTML, RSS,MathML, GraphML, Scalable Vector Graphics(SVG), MusicXML, and thousands of others. Moreover, XML is sometimes used as the specification language for such application languages. |
|
| XML is nothing by itself. XML is more of a "common ground" standard. The main benefit of XML is that you can take data from a program like MSSQL(Microsoft SQL), convert it into XML, then share that XML with a slough of other programs, platforms, etc. Each of these receiving platforms can then convert the XML into a structure the platform uses normally and presto! you have just communicated between two potentially very different platforms! |
|
| History of Markup languages…... |
|
| A markup language combines text and extra information about the text. The extra information, for example about the text's structure or presentation, is expressed using markup, which is intermingled with the primary text. |
|
| The best-known markup language in modern use is HTML (HyperText Markup Language), one of the foundations of the World Wide Web. Originally markup was used in the publishing industry in the communication of printed work between authors, editors, and printers. |
|
| GenCode |
|
| The idea of "markup languages" was apparently first presented in 1967, by William W. Tunnicliffe at a conference. Tunnicliffe would later lead the development of a standard called GenCode for the publishing industry. |
|
| Book designer Stanley Fish also published speculation along similar lines in the late1960s. Brian Reid, in his 1980 dissertation at Carnegie Mellon University, developed the theory and a working implementation of descriptive markup in actual use. |
|
| However, IBM researcher Charles Goldfarb is more commonly seen today as the "father" of markup languages, because of his work on IBM GML, and then as chair of the International Organization for Standardization committee that developed SGML, the first widely used descriptive markup system. |
|
| SGML |
|
| Standard Generalized Markup Language |
|
| The first language to make a clear and clean distinction between structure and presentation was certainly Scribe, which was developed by Brian Reid and described in his doctoral thesis in 1980. |
|
| Scribe was revolutionary in a number of ways, not least that it introduced the idea of styles separated from the marked up document, and of a grammar controlling the usage of descriptive elements. Scribe influenced the development of Generalized Markup Language (GML)(later SGML) and is a direct ancestor to HTML and LaTeX. |
|
| SGML specified a syntax for including the markup in documents, as well as one for separately describing what tags were allowed, and where (the Document Type Definition (DTD) or schema). |
|
| This allowed authors to create and use any markup they wished, selecting tags that made the most sense to them and were named in their own natural languages. Thus, SGML is properly a meta-language, and many particular markup languages are derived from it. |
|
| From the late 80s on, most substantial new markup languages have been based on SGML system, including for example TEI and DocBook. SGML was promulgated as an International Standard by International Organization for Standardization, ISO 8879, in 1986. |
|
| The Rise of XML |
|
| The flexibility of SGML for lively information display was understood by early digital media publishers in the late 1980s prior to the rise of the Internet By the mid-1990s some practitioners of SGML had gained experience with the then-new WWW, and believed that SGML offered solutions to some of the problems the Web was likely to face as it grew. |
|
| Dan Connolly added SGML to the list of W3C's activities when he joined the staff in 1995; work began in mid-1996 when Jon Bosak developed a charter and recruited collaborators. Bosak was well connected in the small community of people who had experience both in SGML and the Web. He received support in his efforts from Microsoft. |
|
| XML was compiled by a working group of eleven members, supported by an (approximately) 150-member Interest Group. Technical debate took place on the Interest Group mailing list and issues were resolved by consensus or, when that failed, majority vote of the Working Group. |
|
| The decision record was compiled by Michael Sperberg-McQueen on December 4th 1997. James Clark served as Technical Lead of the Working Group, notably contributing the empty-element " " syntax and the name "XML". |
|
| Other names that had been put forward for consideration included "MAGMA" (Minimal Architecture for Generalized Markup Applications), "SLIM" (Structured Language for Internet Markup) and "MGML" (Minimal Generalized Markup Language). |
|
| The co-editors of the specification were originally Tim Bray and Michael Sperberg-McQueen. Halfway through the project Bray accepted a consulting engagement with Netscape, provoking vociferous protests from Microsoft. Bray was temporarily asked to resign the editorship. This led to intense dispute in the Working Group, eventually solved by the appointment of Microsoft's Jean Paoli as a third co-editor. |
|
| The XML Working Group never met face-to-face; the design was accomplished using a combination of email and weekly teleconferences. The major design decisions were reached in twenty weeks of intense work between July and November of 1996, when the first Working Draft of an XML specification was published. Further design work continued through 1997, and XML 1.0 became a W3C Recommendation on February 10, 1998. |
|
| XML 1.0 achieved the Working Group's goals of Internet usability, general-purpose usability, SGML compatibility, facilitation of easy development of processing software, minimization of optional features, legibility, formality, conciseness, and ease of authoring. Like its antecedent SGML, XML allows for some redundant syntactic constructs and includes repetition of element identifiers. In these respects, terseness was not considered essential in its structure. |
|
|
|
|
|
| Introduction to Schema |
|
| Introduction to DTD |
|
| Document Type Definition is a language that describes the contents of an SGMLdocument. The DTD is also used with XML, and the DTD definitions may be embedded within an XML document or in a separate file. |
|
| DTDs cannot be used to define XML namespaces, and DTDs are written using their own syntax, not XML syntax. As a result, XML schemas such as W3C XML Schema andRELAX NG are used to define the content of an XML document. |
|
| DTD is native to the SGML and XML specifications, and since its introduction other specification languages such as XML Schema and RELAX NG have been released with additional functionality. |
|
| As an expression of a schema, a DTD specifies, in effect, the syntax of an "application" of SGML or XML, such as the derivative language HTML or XHTML. This syntax is usually a less general form of the syntax of SGML or XML. |
|
| In a DTD, the structure of a class of documents is described via element and attribute-list declarations. Element declarations name the allowable set of elements within the document, and specify whether and how declared elements and runs of character data may be contained within each element. |
|
| Attribute-list declarations name the allowable set of attributes for each declared element, including the type of each attribute value, if not an explicit set of valid value(s). |
|
| Sample DTD document: |
|
<?xml version="1.0" encoding="ISO-8859-1"?>
<!ELEMENT ebiz
(associate_details,emp_details,sponser_details)>
<!ATTLIST ebiz companysite C
DATA #FIXED "http://www.ebizel.com">
<!ELEMENT associate_details
(associate,addressdetails,payment+)>
<!ELEMENT associate
(name,contact_no+)>
<!ATTLIST associate type CDATA #REQUIRED>
<!ELEMENT addressdetails
(address+,city?,state,pin)>
<!ATTLIST addressdetails
communication_mode CDATA #IMPLIED>
<!ELEMENT payment
(amount+,duration+)>
<!ATTLIST payment mode CDATA #REQUIRED>
<!ELEMENT duration
(duration_from,duration_to)>
<!ENTITY developer
"aman.ravish@ebizel.com">
<!ENTITY copyright
"eBIZ.com, All right Reserved, 2007">
|
|
|
|
|
| Introduction to Schema |
|
| XML Schema in sight |
|
| XML Schema, published as a W3C Recommendation in May 2001, is one of severalXML schema languages. It was the first separate schema language for XML to achieve Recommendation status by the W3C. |
|
| The W3C XML Schema Definition Language is an XML language for describing and constraining the content of XML documents. W3C XML Schema is a W3CRecommendation. |
|
| Like all XML schema languages, XML Schema can be used to express a schema: a set of rules to which an XML document must conform in order to be considered 'valid'according to that schema. |
|
| However, unlike most other schema languages, XML Schema was also designed with the intent of validation resulting in a collection of information adhering to specific datatypes, which can be useful in the development of XML document processing software, but which has also provoked criticism. |
|
| An XML Schema instance is an XML Schema Definition (XSD) and typically has the filename extension ".xsd". The language itself is sometimes informally referenced asXSD. |
|
| It has been suggested that WXS (for W3C XML Schema) is a more appropriate initialism, though this acronym has not been in a widespread use and W3C working group rejected it. XSD is also an initialism for XML Schema Datatypes, the datatype portion of XML Schema. |
|
| What is XML Schema? |
|
| XML Schema is an XML-based alternative to DTD. |
|
| An XML schema describes the structure of an XML document. |
|
| The XML Schema language is also referred to as XML Schema Definition (XSD). The purpose of an XML Schema is to define the legal building blocks of an XML document, just like a DTD, but unlike DTD’s schemas are XML based, extensible, easy to use and more flexible then DTD’s. |
|
| XML schema languages use XML syntax thus make sense and schema documents are easy to understand.XML schema languages use XML syntax thus make sense and schema documents are easy to understand. |
|
| Schema define the document's structure - which elements are children of others, the order the child elements can appear, and the number of child elements. Schemas specify if an element is empty or if it can include text. They can also specify default values for attributes. |
|
| Schema standards are defined by the World Wide Web Consortium (W3C). TheW3C site provides a comprehensive reference of XML schemas. |
|
| An XML Schema Definition (XSD) is an instance of an XML schema written in XMLSchema. An XSD defines a type of XML document in terms of constraints upon what elements and attributes may appear, their relationship to each other, what types of data may be in them, and other things. |
|
| It can be used with validation software in order to ascertain whether a particular XMLdocument is of that type, and to produce a PSVI. |
|
| XSDs were the first W3C-recommended XML schemas to provide a namespace and datatype aware alternative to using XML's native Document Type Definitions (DTDs). |
|
| Schemas are more powerful and flexible than DTDs and use XML syntax. |
|
| Independent developers can agree to use a common Schema for exchanging XMLdata. Your application can use this agreed upon Schema to verify the data it receives. Verifying an XML document against the schema is known as validating the document. |
|
| XML Schema Definition files usually have the filename extension ".xsd". |
|
| An XML Schema: |
|
|
| • Defines elements that can appear in a document. |
|
| • Defines attributes that can appear in a document. |
|
| • Defines which elements are child elements. |
|
| • Defines the order of child elements. |
|
| • Defines the number of child elements. |
|
| • Defines whether an element is empty or can include text. |
|
| • Defines data types for elements and attributes. |
|
| • Defines default and fixed values for elements and attributes. |
|
|
|
|
|
| Introduction to Schema |
|
| Schema vs. DTD |
|
| The DTD provides a basic grammar for defining an XML Document in terms of the metadata that comprise the shape of the document. An XML Schema provides this, plus a detailed way to define what the data can and cannot contain. It provides far more control for the developer over what is legal, and it provides an Object Oriented approach, with all the benefits this entails. |
|
| So, if XML Schemas provide an Object Oriented approach to defining an XMLdocument's structure, and if XML Schemas give us the power to define re-useable types such as an ISBN number based on a wide range of pre-defined types, why would we use a DTD? There are in fact several good reasons for using the DTD instead of the schema. |
|
| Firstly, and rather an important point, is that XML Schema is a new technology. This means that whilst some XML Parsers support it fully, many still don't. If you use XML to communicate with a legacy system, perhaps it won't support the XML Schema. |
|
| Many systems interfaces are already defined as a DTD. They are mature definitions, rich and complex. The effort in re-writing the definition may not be worthwhile. |
|
| DTD is also established, and examples of common objects defined in a DTD abound on the Internet -- freely available for re-use. A developer may be able to use these to define a DTD more quickly than they would be able to accomplish a complete re-development of the core elements as a new schema. |
|
| Finally, you must also consider the fact that the XML Schema is an XML document. It has an XML Namespace to refer to, and an XML DTD to define it. This is all overhead. |
|
| When a parser examines the document, it may have to link this all in, interperate theDTD for the Schema, load the namespace, and validate the schema, etc., all before it can parse the actual XML document in question. |
|
| If you're using XML as a protocol between two systems that are in heavy use, and need a quick response, then this overhead may seriously degrade performance. |
|
| The XML Schema recommendation describes the content and structure of XMLdocuments in XML. It includes the full capabilities of Document Type Definitions (DTDs) so that existing DTDs can be converted to XML Schema. XML Schemas have additional capabilities compared to DTDs. |
|
| Sample XML document: email.xml |
|
<?xml version="1.0" encoding="UTF-8"?>
<email>
<sender>aman.kumar@ebizel.com</sender>
<recipient>
<to>support@ebizel.com</to>
<cc>info@ebizel.com</cc>
</recipient>
<subject>How r U?</subject>
<message>
<header>Hello Dear</header>
<body>Hi dear how r U? miss u a lot</body>
<footer>nothing</footer>
</message>
</email>
|
|
| the XML document above defines the structure of a sample email document. |
| Click here to view this XML file with DTD. |
|
| Document Type Definition for the above XML file: email.dtd |
|
<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT email (sender,recipient,subject,message)>
<!ELEMENT sender (#PCDATA)>
<!ELEMENT recipient (to,cc*,bcc*)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT cc (#PCDATA)>
<!ELEMENT bcc (#PCDATA)>
<!ELEMENT subject (#PCDATA)>
<!ELEMENT message (header*,body?,footer*)>
<!ELEMENT header (#PCDATA)>
<!ELEMENT body (#PCDATA)>
<!ELEMENT footer (#PCDATA)>
|
|
| XML Schema Definition for the same XML document: email.xsd |
|
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="email">
<xs:complexType>
-------
</xs:complexType>
</xs:element>
</xs:schema>
|
|
| Click here to view the file with XSD. |
|
| After looking at both DTD and XML Schema, you can easily notice the difference between these two. The most basic difference that anyone can spot is XML schemacontains syntax that similar to XML and uses XML for Schema definition, DTD on the other hand is an external language that defines the structure. |
|
|
|
|
|
| Getting started with XML Schema |
|
| First Schema Document |
|
| As you know XML Schema is an XML-based alternative to DTD and an XML schemadescribes the structure of an XML document. |
|
| The XML Schema language is also referred to as XML Schema Definition (XSD). |
|
| Prerequisite |
|
| Before you continue you should have a basic understanding of the following: |
|
• HTML / XHTML
• XML and XML Namespaces
• A basic understanding of DTD
• Basic File Operations(New, Save, etc..) |
|
| If you want to study these subjects first, Check eBIZ.com HTML, XHTML, XML andDTD tutorials. |
|
| First Schema Document: |
|
1. <?xml version="1.0" encoding="UTF-8"?>
2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
3. <xs:element name="email">
4. <xs:complexType>
5. <xs:sequence>
6. <xs:element ref="sender"/>
7. <xs:element ref="recipient"/>
8. <xs:element ref="subject"/>
9. <xs:element ref="message"/>
10. </xs:sequence>
11. </xs:complexType>
12. </xs:element>
13. <xs:element name="sender" type="xs:string"/>
14. <xs:element name="recipient">
15. <xs:complexType>
16. <xs:sequence>
17. <xs:element ref="to"/>
18. <xs:element ref="cc"/>
19. </xs:sequence>
20. </xs:complexType>
21. </xs:element>
22. <xs:element name="to" type="xs:string"/>
23. <xs:element name="cc" type="xs:string"/>
24. <xs:element name="subject" type="xs:string"/>
25. <xs:element name="message">
26. <xs:complexType>
27. <xs:sequence>
28. <xs:element ref="header"/>
29. <xs:element ref="body"/>
30. <xs:element ref="footer"/>
31. </xs:sequence>
32. </xs:complexType>
33. </xs:element>
34. <xs:element name="header" type="xs:string"/>
35. <xs:element name="body" type="xs:string"/>
36. <xs:element name="footer" type="xs:NCName"/>
37. </xs:schema>
|
|
| Click here to view this XSD file |
|
| Explanation of email.xsd Like other XML documents, XSD begin with <?xml ..?> PI. |
|
| Syntax at line 2: |
| <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">, declares a namespace xs. |
|
| We have created an complex element email [which is going to be the root element of our email.xml document] on line 3 and 4: |
|
<xs:element name="email">
<xs:complexType> |
|
| At Line no 5 we have started declaration for sequence of elements with this declaration: |
|
| <xs:sequence> |
|
| From line 6-9 we have defined 4 reference elements sender, recipient, subject, message. |
|
1. <xs:element ref="sender"/>
2. <xs:element ref="recipient"/>
3. <xs:element ref="subject"/>
4. <xs:element ref="message"/>
|
|
| Line no 13 contain declaration for element sender whose data type is string. |
|
| 1. <xs:element name="sender" type="xs:string"/> |
|
| You will learn about more Schema data types later in this tutorial. |
|
| Using XSD with XML document: email.xml |
|
<?xml version="1.0" encoding="UTF-8"?>
<email xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="email.xsd">
<sender>aman.kumar@ebizel.com</sender>
<recipient>
<to>support@ebizel.com</to>
<cc>info@ebizel.com</cc>
</recipient>
<subject>How r U?</subject>
<message>
<header>Hello Dear</header>
<body>Hi dear how r U? miss u a lot</body>
<footer>nothing</footer>
</message>
</email>
|
|
|
|
|
|
| Getting started with XML Schema |
|
| Simple Elements |
|
| A simple element is an XML element that can contain only text. It cannot contain any other elements or attributes. |
|
| However, the "only text" restriction is quite misleading. The text can be of many different types. It can be one of the types included in the XML Schema definition(boolean, string, date, etc.), or it can be a custom type that you can define yourself. |
|
| You can also add restrictions (facets) to a data type in order to limit its content, or you can require the data to match a specific pattern. |
|
| Simple Type |
|
| Simple types are element or attribute value that can contain only a basic type. The types that the W3C Schema provides are comparable to what one can have as a variable value in a programming language, an integer, a string, a regular expression pattern, etc. |
|
| The equivalent DTD construct would be any attribute value, or any element with a content model that can contain only parsed character data (#PCDATA). The attribute lang in the greeting.xml instance displayed below is an example of a simple type. |
|
| Defining a Simple Element |
|
| The syntax for defining a simple element is: |
|
| <xs:element name="xyz" type="abc" /> |
|
| where xyz is the name of the element and abc is the data type of the element. |
|
| XML Schema has a lot of built-in data types. The most common types are: |
|
1. xs:string
2. xs:decimal
3. xs:integer
4. xs:boolean |
|
| Simple elements may have a default value OR a fixed value specified. A default value is automatically assigned to the element when no other value is specified. |
|
| Example |
|
| Here are some XML elements: |
|
<?xml version="1.0" encoding="UTF-8"?>
<simple-data> This is example of
XS simpel element </simple-data>
<dob>1966-03-07</dob>
and so on
|
|
| Creating User defined Simple Types |
|
| We can construct our own Simple Type, in addition to the predefined simple types. Element <simpleType> is used to define a simple type. We can place more restrictions on our user defined simple types by using <restriction> element. |
|
| Why create user defined simple types? |
|
| One of the key reason for creating User Defined Simple Types is restriction. Consider the scenario where you have been asked to develop a schema that contains many elements, entities and attributes. |
|
| In this XSD you want to create an element dob(data of birth) and you want to restrict the minimum and maximum values. For this, you have to create a simple type dob and place restrictions for min & max inclusive values. |
|
| Source code for simple-types.xsd: |
|
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:simpleType name="dob">
<xs:restriction base="xs:date">
<xs:minInclusive
value="1967-01-01" ></xs:minInclusive>
<xs:maxInclusive value="1989-01-01"></xs:maxInclusive>
</xs:restriction>
</xs:simpleType>
</xs:schema>
|
|
| Click here to view this example. |
|
|
|
|
|
| Getting started with XML Schema |
|
| Simple Attributes |
|
| All attributes are declared as simple types. |
|
| Simple elements cannot have attributes. If an element has attributes, it is considered to be of a complex type. But the attribute itself is always declared as a simple type. |
|
| How to Define an Attribute? |
|
| The syntax for defining an attribute is: |
|
| <xs:attribute name="xxx" type="yyy"/> |
|
| where xxx is the name of the attribute and yyy specifies the data type of the attribute. |
|
| XML Schema has a lot of built-in data types. The most common types are:; |
|
• xs:string
• xs:decimal
• xs:integer
• xs:boolean
• xs:date
• xs:time
• xs:dataTime
• xs:byte
• xs:double
• xs:decimal |
|
| Example |
|
| Here is an XML element with an attribute: |
|
| <message type="hai">Hello and welcome to eBIZ education's Schema World!</message> |
|
| And here is the corresponding attribute definition: |
|
| <xs:attribute name="type" use="required" type="msgType" /> |
|
| Default and Fixed Values for Attributes |
|
| Attributes may have a default value OR a fixed value specified. |
|
| A default value is automatically assigned to the attribute when no other value is specified. |
|
| In the following example the default value is "hai": |
|
<!-- attribute type declaration--> <xs:attribute
name="type" use="optional" default="hai" type="msgType" /> |
|
| A fixed value is also automatically assigned to the attribute, and you cannot specify another value. |
|
| Optional and Required Attributes |
|
| Attributes are optional by default. To specify that the attribute is required, use the "use" attribute: |
|
| Declaring optional attribute: |
|
<!-- attribute type declaration--> <xs:attribute
name="type" use="optional" default="hai" type="msgType" /> |
|
| Declaring required attribute: |
|
| <xs:attribute name="lang" use="required" type="xs:language"/> |
|
| Restrictions on Content |
|
| When an XML element or attribute has a data type defined, it puts restrictions on the element's or attribute's content. If an XML element is of type "xs:date" and contains a string like "Hello World", the element will not validate. |
|
| With XML Schemas, you can also add your own restrictions to your XML elements and attributes. These restrictions are called facets. |
|
| Sample XSD document: simple-attrib.xsd: |
|
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" >
<xs:annotation>
<xs:documentation xml:lang="en">
eBIZ Welcome XML Schema
See <a href="http://www.w3.org/2001/XMLSchema">
the W3C</a> for more info
</xs:documentation>
</xs:annotation>
<!-- welcome elmement , a complex type -->
<xs:element name="welcome">
<xs:complexType>
<xs:sequence>
<xs:element ref="message" minOccurs="1" />
<xs:element ref="date" minOccurs="0" />
</xs:sequence>
<xs:attribute name="lang" use="required" type="xs:language"/>
</xs:complexType>
</xs:element>
<!-- message element, a complex type -->
<xs:element name="message">
<xs:complexType mixed="true">
<!-- attribute type declaration-->
<xs:attribute name="type" use="optional" default="hai" type="msgType" />
</xs:complexType>
</xs:element>
<!-- date element w/ reference to the date type -->
<xs:element name="date" type="date"/>
<!-- define the date pattern simple type -->
<xs:simpleType name="date">
<xs:restriction base="xs:string">
<xs:pattern value="\d\d-\d\d-\d\d\d\d"/>
</xs:restriction>
</xs:simpleType>
<!--define time of day simpte type -->
<xs:simpleType name="msgType">
<xs:restriction base="xs:string">
<xs:enumeration value="hai" />
<xs:enumeration value="bye" />
</xs:restriction>
</xs:simpleType>
</xs:schema>
|
|
| Click here to view this example. |
|
|
|
|
|
|
| Getting started with XML Schema |
|
| Simple Restrictions |
|
| Restrictions are used to define acceptable values for XML elements or attributes. Restrictions on XML elements are called facets. |
|
| Restrictions on Values |
|
| The following example defines an element called "dob" with a restriction. The value of age cannot be lower than 0 or greater than 120: |
|
<xs:element name="message">
<xs:complexType mixed="true">
<!-- attribute type declaration-->
<xs:attribute name="type" use="optional"
default="hai" type="msgType" />
</xs:complexType>
</xs:element>
<xs:simpleType name="msgType">
<xs:restriction base="xs:string">
<xs:enumeration value="hai" />
<xs:enumeration value="bye" />
</xs:restriction>
</xs:simpleType>
|
|
| Restrictions on a Series of Values |
|
| To limit the content of an XML element to define a series of numbers or letters that can be used, we would use the pattern constraint. |
|
| The example below defines an element called "letter" with a restriction. The only acceptable value is ONE of the LOWERCASE letters from a to z: |
|
<xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
|
|
| The next example defines a simple type called "empIdType" with a restriction. The acceptable values should follow this format eBIZ0012 etc : |
|
<xs:simpleType name="empIdType">
<xs:restriction base="xs:string">
<xs:pattern value="[e][B][I][Z][0-1][0-1][0-1][0-1]"></xs:pattern>
<xs:whiteSpace value="replace"></xs:whiteSpace>
</xs:restriction>
</xs:simpleType>
|
|
| The next example also defines an element called "initials" with a restriction. The only acceptable value is THREE of the LOWERCASE OR UPPERCASE letters from a to z: |
|
<xs:element name="initials">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z][a-zA-Z][a-zA-Z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
|
|
| The next example defines an element called "choice" with a restriction. The only acceptable value is ONE of the following letters: x, y, OR z: |
|
<xs:element name="choice">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[xyz]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
|
|
| The next example defines an element called "solution" with a restriction. The only acceptable value is Mr OR Miss OR Mrs OR Dr: |
|
<xs:element name="solution" >
<xs:simpleType>
<xs:restriction base="xs:string" >
<xs:pattern value="Mr|Miss|Mrs|Dr"></xs:pattern>
</xs:restriction>
</xs:simpleType>
</xs:element>
|
|
| The next example defines an element called "TID" with a restriction. There must be exactly eight characters in a row and those characters must be lowercase or uppercase letters from a to z, or a number from 0 to 9: |
|
<xs:element name="TID">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z0-9]{9}"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
|
|
| Restrictions on Whitespace Characters |
|
| To specify how whitespace characters should be handled, we would use the whiteSpace constraint. |
|
| This example defines an element called "address" with a restriction. The whiteSpace constraint is set to "preserve", which means that the XML processor WILL NOT remove any white space characters: |
|
<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="preserve"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
|
|
| This example also defines an element called "address" with a restriction. The whiteSpace constraint is set to "replace", which means that the XML processor WILL REPLACE all white space characters (line feeds, tabs, spaces, and carriage returns) with spaces: |
|
<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="replace"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
|
|
|
|
|
|
| XML Schema Data types |
|
| Introduction |
|
| The World Wide Web Consortium (W3C) XML Schema Part 2: DataTypes is the specification for defining data types used in XML Schemas. This specification defines built-in primitive data types, derived data types, and facets. |
|
 |
|
|
|
|
|
| XML Schema Data types |
|
| XS Numeric Types |
|
|
|
| Name | Base | Size and Description | Example |
| byte | derived short | 127 to-128. Sign is omitted, “+”assumed. | -1, 0, 126, +100. §3.3.19 pt2 |
| decimal | atomic | Arbitrary precision decimal numbers. Sign
omitted, “+”is assumed. Leading and trailing
zeroes are optional. If the fractional part is
zero, the period and following zero(es) can be
omitted. §3.2.3 pt2 | |
| double | atomic | Double-precision 64-bit floating point type -
legal literals {0, -0, INF, -INF and NaN} | Exam-
ple, -1E4, 12.78e-2, 12 and INF §3.2.5 pt2 |
| float | atomic | 32-bit floating point type - legal literals {0, -0,
INF, -INF and NaN} | Example, -1E4,
1267.43233E12, 12.78e-2, 12 and INF
§3.2.4 pt2 |
| int | derived long | 2147483647 to -2147483648. Sign omitted, “+”
is assumed. | -1, 0, 126789675,
+100000. §3.3.17 pt2 |
| integer | derived decimal | Integer or whole numbers - Sign omitted, “+”is
assumed. | -1, 0, 12678967543233,
+100000. §3.3.13 pt2 |
| long | derived integer | 9223372036854775807 to -
9223372036854775808. Sign omitted, “+”
assumed. | -1, 0, 12678967543233, +100000.
§3.3.16 pt2 |
| negativeInteger | derived
nonPositive | Infinite set {...,-2,-1}. | -1, -12678967543233, -100000.
§3.3.15 pt2 |
| nonnegative-eInteger | derived integer | Infinite set {0, 1, 2,...}. Sign omitted, “+”
assumed. | 1, 0, 12678967543233, +100000.
§3.3.20 pt2 |
nonPositive- Integer | derived integer | Infinite set {...,-2,-1,0}. | -1, 0, -
126733, -100000. §3.3.14 pt2 |
| positive-Integer | derived
nonNegativeInteger | Infinite set {1, 2,...}. Optional “+”sign,. | 1, 12678967543233, +100000.
§3.3.25 pt2 |
| short | derived int | 32767 to -32768. Sign omitted, “+”assumed. | -1, 0, 12678, +10000.
§3.3.18 pt2 |
unsigned- Byte | derived
unsignedShort | 0 to 255. a finite-length | 0, 126, 100. §3.3.24 pt2 |
| unsignedInt | derived
unsignedLong | 0 to 4294967295 | 0, 1267896754, 100000. §3.3.22 pt2 |
unsigned- Long | derived
nonNegative | 0 to 18446744073709551615. | 0, 12678967543233, 100000.
§3.3.21 pt2 |
unsigned- Short | derived unsignedInt | 0 to 65535. | 0, 12678, 10000. §3.3.23 pt2 |
|
|
|
|
|
|
| XML Schema Data types |
|
| XS Logic, Binary and Text types |
|
|
| XS Logic type(s) |
|
| Data Type | Base | Size/Description | |
| Boolean | atomic | binary valued logic legal literals {true, false, 1, 0} | |
|
|
| XS Binary Type(s) |
|
| Data Type | Base | Size/Description | Example |
| base64Binary | atomic | Base64-encoded arbitrary binarydata. | |
| hexBinary | atomic | Arbitrary hex-encoded binarydata. | Example, “0FB7”is a
hex encoding for 16-bit int 4023 (binary 111110110111). |
|
|
|
|
|
|
| XML Schema Data types |
|
| XS Date and Time types |
|
| Date and time data types are used for values that contain date and time. |
|
| Date Data Type |
|
| The date data type is used to specify a date. |
|
| The date is specified in the following form "YYYY-MM-DD" where: |
|
• YYYY indicates the year
• MM indicates the month
• DD indicates the day |
|
| Note: All components are required! |
|
| The following is an example of a date declaration in a schema: |
|
| <xs:element name="start" type="xs:date"/> |
|
| An element in your document might look like this: |
|
| <start>2002-09-24</start> |
|
| Time Zones |
|
| To specify a time zone, you can either enter a date in UTC time by adding a "Z" behind the date - like this: |
|
| <start>2002-09-24Z</start> |
|
| or you can specify an offset from the UTC time by adding a positive or negative time behind the date - like this: |
|
<start>2002-09-24-06:00</start>
or
<start>2002-09-24+06:00</start> |
|
| Time Data Type |
|
| The time data type is used to specify a time. |
|
| The time is specified in the following form "hh:mm:ss" where: |
|
• hh indicates the hour
• mm indicates the minute
• ss indicates the second |
|
| Note: All components are required! |
|
| The following is an example of a dateTime declaration in a schema: |
|
| <xs:element name="startdate" type="xs:dateTime"/> |
|
| An element in your document might look like this: |
|
| <startdate>2002-05-30T09:00:00</startdate> |
|
| Or it might look like this: |
|
| <startdate>2002-05-30T09:30:10.5</startdate> |
|
| Time Zones |
|
| To specify a time zone, you can either enter a dateTime in UTC time by adding a "Z" behind the time - like this: |
|
| <startdate>2002-05-30T09:30:10Z</startdate> |
|
| or you can specify an offset from the UTC time by adding a positive or negative time behind the time - like this: |
|
<startdate>2002-05-30T09:30:10-06:00</startdate>
or
<startdate>2002-05-30T09:30:10+06:00</startdate> |
|
| Duration Data Type |
|
| The duration data type is used to specify a time interval |
|
| The time interval is specified in the following form "PnYnMnDTnHnMnS" where: |
|
• P indicates the period (required)
• nY indicates the number of years
• nM indicates the number of months
• nD indicates the number of days
• T indicates the start of a time section (required if you are going to specify hours, minutes, or seconds)
• nH indicates the number of hours |
|
| The following is an example of a duration declaration in a schema: |
|
| <xs:element name="period" type="xs:duration"/> |
|
| An element in your document might look like this: |
|
| <period>P5Y</period> |
|
| The example above indicates a period of five years. |
|
| Or it might look like this: |
|
| <period>P5Y2M10D</period> |
|
| The example above indicates a period of five years, two months, and 10 days. |
|
| Or it might look like this: |
|
| <period>P5Y2M10DT15H</period> |
|
| The example above indicates a period of five years, two months, 10 days, and 15 hours. |
|
| Or it might look like this: |
|
| <period>PT15H</period> |
|
| The example above indicates a period of 15 hours. |
|
| Negative Duration |
|
| To specify a negative duration, enter a minus sign before the P: |
|
| <period>-P10D</period> |
|
| The example above indicates a period of minus 10 days. |
|
| 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 (YYYY) |
| gYearMonth | Defines a part of a date - the year and month (YYYY-MM) |
| time | Defines a time value |
|
|
| Restrictions on Date Data Types |
|
| Restrictions that can be used with Date data types: |
|
• enumeration
• maxExclusive
• maxInclusive
• minExclusive
• minInclusive
• pattern
• whiteSpace |
|
|
|
|
|
| XML Schema Data types |
|
| Data Types & Restrictions |
|
| Simple types (both built-in and derived) have facets. A facet is a single defining aspect that helps determine the set of values for a simple type. |
|
| For example, length, minInclusive, and maxInclusive are common facets for the built-in data types. All of the facets for a simple type define the set of legal values for that simple type. |
|
| A facet is defined as an element. Each facet element has a fixed attribute that is a Boolean value. When a simple type is defined, you can prevent derivatives of that type from modifying the value of specified facets. |
|
| To prevent modification of a facet, add the fixed attribute to the facet and set its value to true. |
|
| Facets can only appear once in a type definition except for enumeration and pattern facets. Enumeration and pattern facets can have multiple entries and are grouped together. |
|
| Example |
|
| The following example shows a simple type with the fixed attribute set to true which prevents the length from having a value other than 7. |
|
<xs:simpleType name="zip">
<xs:restriction base="xs:string">
<xs:length value="6" fixed="true"/>
</xs:restriction>
</xs:simpleType>
|
|
| List of Constraining Facets |
|
| The constraining facets (facets that can be used to constrain the values of simple types), their descriptions, and the built-in data types that they apply to, are listed below. |
|
| enumeration |
|
| Specified set of values. This constrains a data type to the specified values. For more information, see the W3C XML Schema Part 2: Datatypes Recommendation at http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#element-enumeration. |
|
| fractionDigits |
|
| Value with specific maximum number of decimal digits in the fractional part. For more information, see the W3C XML Schema Part 2: Datatypes Recommendation at http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#element-fractionDigits. |
|
| length |
|
| Number of units of length. Units of length depend on the data type. This value must be a nonNegativeInteger. For more information, see the W3C XML Schema Part 2: Datatypes Recommendation at http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#element-length. |
|
| maxExclusive |
|
| Upper bound value (all values are less than this value). This value must be the same data type as the inherited data type. For more information, see the W3C XML Schema Part 2: Datatypes Recommendation at http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#element-maxExclusive. |
|
| maxInclusive |
|
| Maximum value. This value must be the same data type as the inherited data type. For more information, see the W3C XML Schema Part 2: Datatypes Recommendation at http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#element-maxInclusive. |
|
| maxLength |
|
| Maximum number of units of length. Units of length depend on the data type. This value must be a nonNegativeInteger. For more information, see the W3C XML Schema Part 2: Datatypes Recommendation at http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#element-maxLength. |
|
| minExclusive |
|
| Lower bound value (all values are greater than this value). This value must be the same data type as the inherited data type. For more information, see the W3C XML Schema Part 2: Datatypes Recommendation at http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#element-minExclusive. |
|
| minInclusive |
|
| Minimum value. This value must be the same data type as the inherited data type. For more information, see the W3C XML Schema Part 2: Datatypes Recommendation at http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#element-minInclusive. |
|
| minLength |
|
| Minimum number of units of length. Units of length depend on the data type. This value must be a nonNegativeInteger. For more information, see the W3C XML Schema Part 2: Datatypes Recommendation at http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#element-minLength. |
|
| pattern |
|
| Specific pattern that the data type's values must match. This constrains the data type to literals that match the specified pattern. The pattern value must be a regular expression. For more information, see the W3C XML Schema Part 2: Datatypes Recommendation at http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#element-pattern. |
|
| TotalDigits |
|
| Value with specific maximum number of decimal digits. For more information, see the W3C XML Schema Part 2: Datatypes Recommendation at http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#element-totalDigits. |
|
| whiteSpace |
|
| Value must be one of preserve, replace, or collapse. The whiteSpace facet cannot be changed for most numeric data types. For more information, see the W3C XML Schema Part 2: Datatypes Recommendation at http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#element-whiteSpace. |
|
| preserve | No normalization is performed; the value is not changed for element content as required by the W3C XML 1.0 Recommendation. |
| replace | All occurrences of #x9 (tab), #xA (line feed) and #xD (carriage return) are replaced with #x20 (space). |
| collapse | After the processing implied by replace, contiguous sequences of #x20s are collapsed to a single #x20, and leading and trailing #x20s are removed. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
0 comments: