ࡱ > 1 3 * + , - . / 0 [@ bjbj44 "` Vi Vi x
x
x
x
L d j D $ R F / / D ] $ 2 I 0 M x
s . y $ ݮ , Z 0 $ H t / / $ 4 D
4 XML documents can have a reference to a DTD or an XML Schema.
A Simple XML Document
Look at this simple XML document called "note.xml":
Tove
Jani
Reminder
Don't forget me this weekend!
A Simple DTD
This is a simple DTD file called "note.dtd" that defines the elements of the XML document above ("note.xml"):
Line 1 defines the note element to have four elements: "to, from, heading, body". Line 2-5 defines the to element to be of the type "#PCDATA", the from element to be of the type "#PCDATA", and so on...
A Simple XML Schema
This is a simple XML Schema file called "note.xsd" that defines the elements of the XML document above ("note.xml"):
The note element is said to be of a complex type because it contains other elements. The other elements (to, from, heading, body) are said to be simple types because they do not contain other elements. You will learn more about simple and complex types in the following chapters.
A Reference to a DTD
This XML document has a reference to a DTD:
Tove
Jani
Reminder
Don't forget me this weekend!
A Reference to an XML Schema
This XML document has a reference to an XML Schema:
Tove
Jani
Reminder
Don't forget me this weekend!
The element is the root element of every XML Schema!
The Element
The element is the root element of every XML Schema:
...
...
The element may contain some attributes. A schema declaration often looks something like this:
...
...
The following fragment:
xmlns:xs="http://www.w3.org/2001/XMLSchema"indicates that the elements and data types used in the schema (schema, element, complexType, sequence, string, boolean, etc.) come from the "http://www.w3.org/2001/XMLSchema" namespace. It also specifies that the elements and data types that come from the "http://www.w3.org/2001/XMLSchema" namespace should be prefixed with xs: !!
This fragment:
targetNamespace="http://www.w3schools.com"indicates that the elements defined by this schema (note, to, from, heading, body.) come from the "http://www.w3schools.com" namespace.
This fragment:
xmlns="http://www.w3schools.com"indicates that the default namespace is "http://www.w3schools.com".
This fragment:
elementFormDefault="qualified"indicates that any elements used by the XML instance document which were declared in this schema must be namespace qualified.
Referencing a Schema in an XML Document
This XML document has a reference to an XML Schema:
Tove
Jani
Reminder
Don't forget me this weekend!
The following fragment:
xmlns="http://www.w3schools.com"specifies the default namespace declaration. This declaration tells the schema-validator that all the elements used in this XML document are declared in the "http://www.w3schools.com" namespace.
Once you have the XML Schema Instance namespace available:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"you can use the schemaLocation attribute. This attribute has two values. The first value is the namespace to use. The second value is the location of the XML schema to use for that namespace:
xsi:schemaLocation="http://www.w3schools.com note.xsd"XML Schemas define the elements of your XML files.
A simple element is an XML element that can contain only text. It cannot contain any other elements or attributes.
What is a Simple Element?
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 that are 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, and you can require the data to match a defined pattern.
How to Define a Simple Element
The syntax for defining a simple element is:
where xxx is the name of the element and yyy is the data type of the element.
Here are some XML elements:
Refsnes
34
1968-03-27And here are the corresponding simple element definitions:
Common XML Schema Data Types
XML Schema has a lot of built-in data types. Here is a list of the most common types:
xs:string
xs:decimal
xs:integer
xs:boolean
xs:date
xs:time
Declare Default and Fixed Values for Simple Elements
Simple elements can have a default value OR a fixed value set.
A default value is automatically assigned to the element when no other value is specified. In the following example the default value is "red":
A fixed value is also automatically assigned to the element. You cannot specify another value. In the following example the fixed value is "red":
All attributes are declared as simple types.
Only complex elements can have attributes!
What is an Attribute?
Simple elements cannot have attributes. If an element has attributes, it is considered to be of complex type. But the attribute itself is always declared as a simple type. This means that an element with attributes always has a complex type definition.
How to Define an Attribute
The syntax for defining an attribute is:
where xxx is the name of the attribute and yyy is the data type of the attribute.
Here are an XML element with an attribute:
SmithAnd here are a corresponding simple attribute definition:
Common XML Schema Data Types
XML Schema has a lot of built-in data types. Here is a list of the most common types:
xs:string
xs:decimal
xs:integer
xs:boolean
xs:date
xs:time
Declare Default and Fixed Values for Attributes
Attributes can 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 "EN":
A fixed value is also automatically assigned to the attribute. You cannot specify another value. In the following example the fixed value is "EN":
Creating Optional and Required Attributes
All attributes are optional by default. To explicitly specify that the attribute is optional, use the "use" attribute:
To make an attribute required:
Restrictions on Content
When an XML element or attribute has a type defined, it puts a restriction for the element's or attribute's content. If an XML element is of type "xs:date" and contains a string like "Hello Mother", the element will not validate.
But, there is more... with XML Schemas, you can add your own restrictions to your XML elements and attributes. These restrictions are called facets.You can read more about facets in the next chapter.
Restrictions are used to control acceptable values for XML elements or attributes. Restrictions on XML elements are called facets.
Restrictions on Values
This example defines an element called "age" with a restriction. The value of age can NOT be lower than 0 or greater than 100:
Restrictions on a Set of Values
To limit the content of an XML element to a set of acceptable values, we would use the enumeration constraint.
This example defines an element called "car":
The "car" element is a simple type with a restriction. The acceptable values are: Audi, Golf, BMW.
The example above could also have been written like this:.
Note: In this case the type "carType" can be used by other elements because it is not a part of the "car" element.
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.
This example defines an element called "letter":
The "letter" element is a simple type with a restriction. The only acceptable value is ONE of the LOWERCASE letters from a to z.
The next example defines an element called "initials":
The "initials" element is a simple type with a restriction. The only acceptable value is THREE of the UPPERCASE letters from a to z.
This example also defines an element called "initials":
The "initials" element is a simple type with a restriction. The only acceptable value is THREE of the LOWERCASE OR UPPERCASE letters from a to z.
This example defines an element called "choice":
The "choice" element is a simple type with a restriction. The only acceptable value is ONE of the following letters: x, y, OR z.
The next example defines an element called "prodid":
The "prodid" element is a simple type with a restriction. The only acceptable value is FIVE digits in a sequence, and each digit must be in a range from 0 to 9.
Other Restrictions on a Series of Values
Some other restrictions that can be defined by the pattern constraint:
This example defines an element called "letter":
The "letter" element is a simple type with a restriction. The acceptable value is zero or more occurrences of lowercase letters from a to z.
This example also defines an element called "letter":
The "letter" element is a simple type with a restriction. The acceptable value is one or more occurrences of a lowercase letter followed by a uppercase letter from a to z.
This example defines an element called "gender":
The "gender" element is a simple type with a restriction. The only acceptable value is male OR female.
This example defines an element called "password":
The "password" element is a simple type 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.
Restrictions on White Space Characters
To specify how white space characters should be handled, we would use the whiteSpace constraint.
This example defines an element called "address":
The "address" element is a simple type with a restriction. The whiteSpace constraint is set to "preserve", which means that the XML processor WILL NOT remove any white space characters.
This example also defines an element called "address":
This "address" element is a simple type 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.
This example also defines an element called "address":
This "address" element is a simple type with a restriction. The whiteSpace constraint is set to "collapse", which means that the XML processor WILL REMOVE all white space characters (line feeds, tabs, spaces, carriage returns are replaced with spaces, leading and trailing spaces are removed, multiple spaces are reduced to a single space).
Restrictions on Length
To limit the length of a value in an element, we would use the length, maxLength, and minLength constraints.
This example defines an element called "password":
The "password" element is a simple type with a restriction. The value must be exactly eight characters.
This example defines another element called "password":
This "password" element is a simple type with a restriction. The value must be minimum five characters and maximum eight characters.
Restrictions for Datatypes
ConstraintDescriptionenumerationDefines a list of acceptable valuesfractionDigitsSpecifies the maximum number of decimal places allowed. Must be equal to or greater than zerolengthSpecifies the exact number of characters or list items allowed. Must be equal to or greater than zeromaxExclusiveSpecifies the upper bounds for numeric values (the value must be less than this value)maxInclusiveSpecifies the upper bounds for numeric values (the value must be less than or equal to this value)maxLengthSpecifies the maximum number of characters or list items allowed. Must be equal to or greater than zerominExclusiveSpecifies the lower bounds for numeric values (the value must be greater than this value)minInclusiveSpecifies the lower bounds for numeric values (the value must be greater than or equal to this value)minLengthSpecifies the minimum number of characters or list items allowed. Must be equal to or greater than zeropatternDefines the exact sequence of characters that are acceptable totalDigitsSpecifies the exact number of digits allowed. Must be greater than zerowhiteSpaceSpecifies how white space (line feeds, tabs, spaces, and carriage returns) are handledA complex element contains other elements and/or attributes.
What is a Complex Element?
A complex element is an XML element that contains other elements and/or attributes.
There are four kinds of complex elements:
empty elements
elements that contain only other elements
elements that contain only text
elements that contain both other elements and text
Note: Each of these elements may contain attributes as well!
Examples of Complex XML Elements
A complex XML element, "product", which is empty:
A complex XML element, "employee", which contains only other elements:
John
Smith
A complex XML element, "food", which contains only text:
Ice creamA complex XML element, "description", which contains both elements and text:
It happened on 03.03.99 ....
How to Define a Complex Element
Look at this complex XML element, "employee", which contains only other elements:
John
Smith
We can define a complex element in an XML Schema in different ways:.
1. The "employee" element can be declared directly by naming the element, like this:
If you use the method described above, only the "employee" element can use the specified complex type. Notice that the child elements, "firstname" and "lastname", are surrounded by the indicator. This means that the child elements must appear in the same order as they are declared; "firstname" first and "lastname" second. You will learn more about indicators in the HYPERLINK "http://www.w3schools.com/schema/schema_complex_indicators.asp" XSD Indicators chapter.
2. The "employee" element can have a type attribute that refers to the name of the complex type to use:
If you use the method described above, several elements can refer to the same complex type, like this:
You can also base a complex type element on an existing complex type and add some elements, like this:
An empty complex element can contain attributes; but it cannot have any content between the opening and closing tags.
Define Complex Types for Empty Elements
An empty XML element:
The "product" element above has no content at all. To define a type with no content, we must define a type that allows only elements in its content, but we do not actually declare any elements, like this:
In the example above, we define a complexType having complexContent, i.e. only elements. The complexContent element signals that we intend to restrict or extend the content model of a complex type, and the restriction of integer declares one attribute but does not introduce any element content.
However, it is possible to declare the product element more compactly, like this:
Or you can give the complexType element a name, and let the "product" element have a type attribute that refers to the name of the complexType (if you use this method, several elements can refer to the same complex type):
An "elements only" complex type contains an element that contains only other elements.
Define Complex Types with Elements Only
An XML element, "person", that contains only other elements:
John
Smith
You can define the "person" element in a schema, like this:
Notice the tag. It means that the elements defined ("firstname" and "lastname") must appear in that order inside a "person" element.
Or you can give the complexType element a name, and let the "person" element have a type attribute that refers to the name of the complexType (if you use this method, several elements can refer to the same complex type):
A complex text element can contain both attributes and text.
Define Complex Text-Only Elements
This type contains only simple content (text and attributes), therefore we add a simpleContent element around the content. When using simple content, you must define an extension OR a restriction within the simpleContent element, like this:
....
....
OR
....
....
Tip: Use the extension element to expand on the base simple type for the element, and use the restriction element to limit the base simple type for the element.
Here is an example of an XML element, "shoesize", that contains text-only:
35The following example declares a complexType, "shoesize". The content is defined as an integer data type and the "shoesize" element also contains an attribute named "country":
We could also give the complexType element a name, and let the "shoesize" element have a type attribute that refers to the name of the complexType (if you use this method, several elements can refer to the same complex type):
A mixed complex type element can contain attributes, elements, and text.
Define Complex Types with Mixed Content
An XML element, "letter", that contains both other elements and text:
Dear Mr.John Smith.
Your order 1032
will be shipped on 2001-07-13.
Notice the text that appears between the elements. "name", "orderid", and "shipdate" are all children of "letter". The following schema declares the "letter" element:
Note: To enable character data to appear between the child-elements of "letter", the mixed attribute must be set to "true". The tag means that the elements defined (name, orderid and shipdate) must appear in that order inside a "letter" element.
We could also give the complexType element a name, and let the "letter" element have a type attribute that refers to the name of the complexType (if you use this method, several elements can refer to the same complex type):
We can control HOW elements are to be used in documents with indicators.
Indicators
We have seven types of indicators:
Order indicators:
All
Choice
Sequence
Occurrence indicators:
maxOccurs
minOccurs
Group indicators:
Group name
attributeGroup name
Order Indicators
Order indicators are used to define how elements should occur.
All Indicator
The indicator specifies by default that the child elements can appear in any order and that each child element must occur once and only once:
Note: When using the indicator you can set the indicator to 0 or 1 and the indicator can only be set to 1 (the and are described later).
Choice Indicator
The indicator specifies that either one child element or another can occur:
Sequence Indicator
The indicator specifies that the child elements must appear in a specific order:
Occurrence Indicators
Occurrence indicators are used to define how often an element can occur.
Note: For all "Order" and "Group" indicators (any, all, choice, sequence, group name, and group reference) the default value for maxOccurs and minOccurs is 1!!!!!
maxOccurs Indicator
The indicator specifies the maximum number of times an element can occur:
The example above indicates that the "child_name" element can occur a minimum of one time (the default value for minOccurs is 1) and a maximum of ten times in a "person" element.
minOccurs Indicator
The indicator specifies the minimum number of times an element can occur:
The example above indicates that the "child_name" element can occur a minimum of zero times and a maximum of ten times in a "person" element.
To allow an element to appear an unlimited number of times, use the maxOccurs="unbounded" statement:
A working example:
An XML file called "Myfamily.xml":
Hege Refsnes
Cecilie
Tove Refsnes
Hege
Stale
Jim
Borge
Stale Refsnes
The XML file above contains a root element named "persons". Inside this root element are defined several "person" elements. Each "person" element must contain a "full_name" child element and it can contain up to five "child_name" child elements.
Here is the schema file "family.xsd":
Group Indicators
Group indicators are used to define related sets of elements.
Element Groups
Element groups are defined with the group declaration, like this:
...
You must define an all, choice, or sequence element inside the group declaration. The following example defines a group named "persongroup", that defines a group of elements that must occur in an exact sequence:
After you have defined a group, you can reference it in another group or complex type definition, like this:
Attribute Groups
Attribute groups are defined with the attributeGroup declaration, like this:
...
The following example defines an attribute group named "personattrgroup":
After you have defined an attribute group, you can reference it in another group or complex type definition, like this:
The element enables us to extend the XML document with elements not specified by the schema!
The Element
The element enables us to extend the XML document with elements not specified by the schema.
The following example is a fragment from an XML schema called "family.xsd". It shows a declaration for the "person" element. By using the element we can extend (after ) the content of "person" with any element:
Now we want to extend the "person" element with a "children" element. In this case we can do so, even if the author of the schema above never declared any "children" element!
Look at this schema file, called "children.xsd":
The XML file below (called "Myfamily.xml"), uses components from two different schemas; "family.xsd" and "children.xsd":
Hege
Refsnes
Cecilie
Stale
Refsnes
The XML file above is valid because the schema "family.xsd" allows us to extend the "person" element with an optional element after the "lastname" element!
The and elements are used to make EXTENSIBLE documents! They allow documents to contain additional elements that are not declared in the main XML schema!
The element enables us to extend the XML document with attributes not specified by the schema!
The Element
The element enables us to extend the XML document with attributes not specified by the schema.
The following example is a fragment from an XML schema called "family.xsd". It shows a declaration for the "person" element. By using the element we can add any number of attributes to the "person" element:
Now we want to extend the "person" element with a "gender" attribute. In this case we can do so, even if the author of the schema above never declared any "gender" attribute!
Look at this schema file, called "attribute.xsd":
The XML file below (called "Myfamily.xml"), uses components from two different schemas; "family.xsd" and "attribute.xsd":
Hege
Refsnes
Stale
Refsnes
The XML file above is valid because the schema "family.xsd" allows us to add an attribute to the "person" element!
The and elements are used to make EXTENSIBLE documents! They allow documents to contain additional elements that are not declared in the main XML schema!
With XML Schemas one element can substitute another element.
Element Substitution
Let's say that we have users from two different countries: England and Norway. We would like the ability to let the user choose whether he or she would like to use the Norwegian element names or the English element names in the XML document.
To solve this problem, we could define a substitutionGroup in the XML schema. First, we declare a head element and then we declare the other elements which state that they are substitutable for the head element.
In the example above, the "name" element is the head element and the "navn" element is substitutable for "name".
Look at this fragment of an XML schema:
A valid XML document (according to the schema above) could look like this:
John Smith
or like this:
John Smith
Blocking Element Substitution
To prevent other elements from substituting with a specified element, use the block attribute:
Look at this fragment of an XML schema:
A valid XML document (according to the schema above) looks like this:
John Smith
BUT THIS IS NO LONGER VALID:
John Smith
Using substitutionGroup
The type of the substitutable elements must be the same as, or derived from, the type of the head element. If the type of the substitutable element is the same as the type of the head element you will not have to specify the type of the substitutable element.
Note that all elements in the substitutionGroup (the head element and the substitutable elements) must be declared as global elements, otherwise it will not work!
What are Global Elements?
Global elements are elements that are immediate children of the "schema" element! Local elements are elements nested within other elements!
This chapter will demonstrate how to write an XML Schema. You will also learn that a schema can be written in different ways.
An XML Document
Let's have a look at this XML document called "shiporder.xml":
John Smith
Ola Nordmann
Langgt 23
4000 Stavanger
Norway
-
Empire Burlesque
Special Edition
1
10.90
-
Hide your heart
1
9.90
The XML document above consists of a root element, "shiporder", that contains a required attribute called "orderid". The "shiporder" element contains three different child elements: "orderperson", "shipto" and "item". The "item" element appears twice, and it contains a "title", an optional "note" element, a "quantity", and a "price" element.
The line above: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" tells the XML parser that this document should be validated against a schema. The line: xsi:noNamespaceSchemaLocation="shiporder.xsd" specifies WHERE the schema resides (here it is in the same folder as "shiporder.xml").
Create an XML Schema
Now we are going to create a schema for the XML document above!
We start by opening a new file that we will call "shiporder.xsd". To create the schema we could simply follow the structure in the XML document and define each element as we find it. We will start with the standard XML declaration followed by the xs:schema element that defines a schema:
...
...
In the schema above we use the standard namespace (xs), and the URI associated with this namespace is the Schema language definition, which has the standard value of http://www.w3.org/2001/XMLSchema.
Next, we have to define the "shiporder" element. This element has an attribute and it contains other elements, therefore we consider it as a complex type. The child elements of the "shiporder" element is surrounded by a xs:sequence element that defines an ordered sequence of sub elements:
...
...
...
Then we have to define the "orderperson" element as a simple type (because it does not contain any attributes or other elements). The type (xs:string) is prefixed with the namespace prefix associated with XML Schema that indicates a predefined schema data type:
Next, we have to define two elements that are of the complex type: "shipto" and "item". We start by defining the "shipto" element:
With schemas we can define the number of possible occurrences for an element with the maxOccurs and minOccurs attributes. maxOccurs specifies the maximum number of occurrences for an element and minOccurs specifies the minimum number of occurrences for an element. The default value for both maxOccurs and minOccurs is 1!!!!!
Now we can define the "item" element. This element can appear multiple times inside a "shiporder" element. This is specified by setting the maxOccurs attribute of the "item" element to "unbounded" which means that there can be as many occurrences of the "item" element as the author wishes. Notice that the "note" element is optional. We have specified this by setting the minOccurs attribute to zero:
We can now declare the attribute of the "shiporder" element. Since this is a required attribute we specify use="required".
Note: The attribute declarations must always come last:
Here is the complete listing of the schema file called "shiporder.xsd":
Divide the Schema
The previous design method is very simple but can be very difficult to read and maintain when documents are complex!
The next design method is based on defining all elements and attributes first, and then refering to them using the ref attribute.
Here is the new look of the schema file ("shiporder.xsd"):
Using Named Types
The third design method defines classes or types, that enables us to reuse element definitions. This is done by naming the simpleTypes and complexTypes elements, and then point to them through the type attribute of the element.
Here is the new look of the schema file ("shiporder.xsd"):
The restriction element indicates that the datatype is derived from a W3C XML Schema namespace datatype. So, this fragment:
means that the value of the element or attribute must be a string value.
The restriction element is more often used to apply restrictions on elements. Look at the following lines from the schema above:
This indicates that the value of the element or attribute must be a string and it must be exactly six characters in a row and those characters must be a number from 0 to 9.
String data types are used for values that contains character strings.
String Data Type
The string data type can contain characters, line feeds, carriage returns, and tab characters.
The following is an example of a string declaration in a schema:
An element in your document might look like this:
John SmithOr it might look like this:
John Smith Note: The XML processor will not modify the value if you use the string data type.
NormalizedString Data Type
The normalizedString data type is derived from the String data type.
The normalizedString data type also contains characters, but the XML processor will remove line feeds, carriage returns, and tab characters.
The following is an example of a normalizedString declaration in a schema:
An element in your document might look like this:
John SmithOr it might look like this:
John Smith Note: In the example above the XML processor will replace the tabs with spaces.
Token Data Type
The token data type is also derived from the String data type.
The token data type also contains characters, but the XML processor will remove line feeds, carriage returns, tabs, leading and trailing spaces, and multiple spaces.
The following is an example of a token declaration in a schema:
An element in your document might look like this:
John SmithOr it might look like this:
John Smith Note: In the example above the XML processor will remove the tabs.
String Data Types
Note that all of the data types below derive from the String data type (except for string itself)!
NameDescriptionENTITIESENTITYIDA string that represents the ID attribute in XML (only used with schema attributes)IDREFA string that represents the IDREF attribute in XML (only used with schema attributes)IDREFSlanguageA string that contains a valid language idNameA string that contains a valid XML nameNCNameNMTOKENA string that represents the NMTOKEN attribute in XML (only used with schema attributes)NMTOKENSnormalizedStringA string that does not contain line feeds, carriage returns, or tabsQNamestringA stringtokenA string that does not contain line feeds, carriage returns, tabs, leading or trailing spaces, or multiple spaces
Restrictions on String Data Types
Restrictions that can be used with String data types:
enumeration
length
maxLength
minLength
pattern (NMTOKENS, IDREFS, and ENTITIES can NOT use this constraint)
whiteSpace
Date and time data types are used for values that contains date and time.
Date Data Type
The date data type is used to specify a date.
The date is specified in the following form "CCYY-MM-DD" where:
CC indicates the century
YY 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:
An element in your document might look like this:
2002-09-24Time Zones
To specify a time zone, you can either enter a date in UTC time by adding a "Z" behind the date - like this:
2002-09-24Zor you can specify an offset from the UTC time by adding a positive or negative time behind the date - like this:
2002-09-24-06:00
or
2002-09-24+06:00
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 time declaration in a schema:
An element in your document might look like this:
09:00:00Or it might look like this:
09:30:10.5Time Zones
To specify a time zone, you can either enter a time in UTC time by adding a "Z" behind the time - like this:
09:30:10Zor you can specify an offset from the UTC time by adding a positive or negative time behind the time - like this:
09:30:10-06:00
or
09:30:10+06:00
DateTime Data Type
The dateTime data type is used to specify a date and a time.
The dateTime is specified in the following form "CCYY-MM-DDThh:mm.ss" where:
CC indicates the century
YY indicates the year
MM indicates the month
DD indicates the day
T indicates the start of the required time section
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:
An element in your document might look like this:
2002-05-30T09:00:00Or it might look like this:
2002-05-30T09:30:10.5Time Zones
To specify a time zone, you can either enter a dateTime in UTC time by adding a "Z" behind the time - like this:
2002-05-30T09:30:10Zor you can specify an offset from the UTC time by adding a positive or negative time behind the time - like this:
2002-05-30T09:30:10-06:00
or
2002-05-30T09:30:10+06:00
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
nM indicates the number of minutes
nS indicates the number of seconds
The following is an example of a duration declaration in a schema:
An element in your document might look like this:
P5YThe example above indicates a period of five years.
Or it might look like this:
P5Y2M10DThe example above indicates a period of five years, two months, and 10 days.
Or it might look like this:
P5Y2M10DT15HThe example above indicates a period of five years, two months, 10 days, and 15 hours.
Or it might look like this:
PT15HThe example above indicates a period of 15 hours.
Negative Duration
To specify a negative duration, enter a minus sign before the P:
-P10DThe example above indicates a period of minus 10 days.
Date and Time Data Types
NameDescriptiondateDefines a date valuedateTimeDefines a date and time valuedurationDefines a time intervalgDayDefines a part of a date - the day (DD)gMonthDefines a part of a date - the month (MM)gMonthDayDefines a part of a date - the month and day (MM-DD)gYearDefines a part of a date - the year (CCYY)gYearMonthDefines a part of a date - the year and month (CCYY-MM)timeDefines a time value
Restrictions on Date Data Types
Restrictions that can be used with Date data types:
enumeration
maxExclusive
maxInclusive
minExclusive
minInclusive
pattern
whiteSpace
Decimal data types are used for numeric values.
Decimal Data Type
The decimal data type is used to specify a numeric value.
The following is an example of a decimal declaration in a schema:
An element in your document might look like this:
999.50Or it might look like this:
+999.5450Or it might look like this:
-999.5230Or it might look like this:
0Or it might look like this:
14Note: The maximum number of decimal digits you can specify is 18!
Integer Data Type
The integer data type is used to specify a numeric value without a fractional component.
The following is an example of an integer declaration in a schema:
An element in your document might look like this:
999Or it might look like this:
+999Or it might look like this:
-999Or it might look like this:
0
Numeric Data Types
Note that all of the data types below derive from the Decimal data type (except for decimal itself)!
NameDescriptionbyteA signed 8-bit integerdecimalA decimal valueintA signed 32-bit integerintegerAn integer valuelongA signed 64-bit integernegativeIntegerAn integer containing only negative values ( .., -2, -1.)nonNegativeIntegerAn integer containing only non-negative values (0, 1, 2, ..)nonPositiveIntegerAn integer containing only non-positive values (.., -2, -1, 0)positiveIntegerAn integer containing only positive values (1, 2, ..)shortA signed 16-bit integerunsignedLongAn unsigned 64-bit integerunsignedIntAn unsigned 32-bit integerunsignedShortAn unsigned 16-bit integerunsignedByteAn unsigned 8-bit integer
Restrictions on Numeric Data Types
Restrictions that can be used with Numeric data types:
enumeration
fractionDigits
maxExclusive
maxInclusive
minExclusive
minInclusive
pattern
totalDigits
whiteSpace
Other miscellaneous data types are boolean, base64Binary, hexBinary, float, double, anyURI, QName, and NOTATION.
Boolean Data Type
The boolean data type is used to specify a true or false value.
The following is an example of a boolean declaration in a schema:
An element in your document might look like this:
999Note: Legal values for boolean are true, false, 1 (which indicates true), and 0 (which indicates false).
Binary Data Types
Binary data types are used to express binary-formatted data.
We have two binary data types:
base64Binary (Base64-encoded binary data)
hexBinary (hexadecimal-encoded binary data)
The following is an example of a hexBinary declaration in a schema:
AnyURI Data Type
The anyURI data type is used to specify a URI.
The following is an example of an anyURI declaration in a schema:
An element in your document might look like this:
Note: If a URI has spaces, replace them with %20.
Miscellaneous Data Types
NameDescriptionanyURIbase64BinarybooleandoublefloathexBinaryNOTATIONQName
Restrictions on Miscellaneous Data Types
Restrictions that can be used with the other data types:
enumeration (a Boolean data type can NOT use this constraint)
length (a Boolean data type can NOT use this constraint)
maxLength (a Boolean data type can NOT use this constraint)
minLength (a Boolean data type can NOT use this constraint)
pattern
whiteSpace
XSD Elements
ElementExplanation HYPERLINK "http://www.w3schools.com/schema/el_all.asp" allSpecifies that the child elements can appear in any order. Each child element can occur 0 or 1 time HYPERLINK "http://www.w3schools.com/schema/el_annotation.asp" annotationSpecifies the top-level element for schema comments HYPERLINK "http://www.w3schools.com/schema/el_any.asp" anyEnables the author to extend the XML document with elements not specified by the schema HYPERLINK "http://www.w3schools.com/schema/el_anyattribute.asp" anyAttributeEnables the author to extend the XML document with attributes not specified by the schema HYPERLINK "http://www.w3schools.com/schema/el_appinfo.asp" appInfoSpecifies information to be used by the application (must go inside annotation) HYPERLINK "http://www.w3schools.com/schema/el_attribute.asp" attributeDefines an attribute HYPERLINK "http://www.w3schools.com/schema/el_attributegroup.asp" attributeGroupDefines an attribute group to be used in complex type definitions HYPERLINK "http://www.w3schools.com/schema/el_choice.asp" choiceAllows only one of the elements contained in the declaration to be present within the containing element HYPERLINK "http://www.w3schools.com/schema/el_complexcontent.asp" complexContentDefines extensions or restrictions on a complex type that contains mixed content or elements only HYPERLINK "http://www.w3schools.com/schema/el_complextype.asp" complexTypeDefines a complex type element HYPERLINK "http://www.w3schools.com/schema/el_documentation.asp" documentationDefines text comments in a schema (must go inside annotation) HYPERLINK "http://www.w3schools.com/schema/el_element.asp" elementDefines an element HYPERLINK "http://www.w3schools.com/schema/el_extension.asp" extensionExtends an existing simpleType or complexType element HYPERLINK "http://www.w3schools.com/schema/el_field.asp" fieldSpecifies an XPath expression that specifies the value used to define an identity constraint HYPERLINK "http://www.w3schools.com/schema/el_group.asp" groupDefines a group of elements to be used in complex type definitions HYPERLINK "http://www.w3schools.com/schema/el_import.asp" importAdds multiple schemas with different target namespace to a document HYPERLINK "http://www.w3schools.com/schema/el_include.asp" includeAdds multiple schemas with the same target namespace to a document HYPERLINK "http://www.w3schools.com/schema/el_key.asp" keySpecifies an attribute or element value as a key (unique, non-nullable, and always present) within the containing element in an instance document HYPERLINK "http://www.w3schools.com/schema/el_keyref.asp" keyrefSpecifies that an attribute or element value correspond to those of the specified key or unique element HYPERLINK "http://www.w3schools.com/schema/el_list.asp" listDefines a simple type element as a list of values HYPERLINK "http://www.w3schools.com/schema/el_notation.asp" notationDescribes the format of non-XML data within an XML document HYPERLINK "http://www.w3schools.com/schema/el_redefine.asp" redefineRedefines simple and complex types, groups, and attribute groups from an external schema HYPERLINK "http://www.w3schools.com/schema/el_restriction.asp" restrictionDefines restrictions on a simpleType, simpleContent, or a complexContent HYPERLINK "http://www.w3schools.com/schema/el_schema.asp" schemaDefines the root element of a schema HYPERLINK "http://www.w3schools.com/schema/el_selector.asp" selectorSpecifies an XPath expression that selects a set of elements for an identity constraint HYPERLINK "http://www.w3schools.com/schema/el_sequence.asp" sequenceSpecifies that the child elements must appear in a sequence. Each child element can occur from 0 to any number of times HYPERLINK "http://www.w3schools.com/schema/el_simpleContent.asp" simpleContentContains extensions or restrictions on a text-only complex type or on a simple type as content and contains no elements HYPERLINK "http://www.w3schools.com/schema/el_simpletype.asp" simpleTypeDefines a simple type and specifies the constraints and information about the values of attributes or text-only elements HYPERLINK "http://www.w3schools.com/schema/el_union.asp" unionDefines a simple type as a collection (union) of values from specified simple data types HYPERLINK "http://www.w3schools.com/schema/el_unique.asp" uniqueDefines that an element or an attribute value must be unique within the scope
XSD Restrictions/Facets for Datatypes
HYPERLINK "http://www.w3schools.com/schema/schema_facets.asp" Look at XSD Restrictions!
ConstraintDescriptionenumerationDefines a list of acceptable valuesfractionDigitsSpecifies the maximum number of decimal places allowed. Must be equal to or greater than zerolengthSpecifies the exact number of characters or list items allowed. Must be equal to or greater than zeromaxExclusiveSpecifies the upper bounds for numeric values (the value must be less than this value)maxInclusiveSpecifies the upper bounds for numeric values (the value must be less than or equal to this value)maxLengthSpecifies the maximum number of characters or list items allowed. Must be equal to or greater than zerominExclusiveSpecifies the lower bounds for numeric values (the value must be greater than this value)minInclusiveSpecifies the lower bounds for numeric values (the value must be greater than or equal to this value)minLengthSpecifies the minimum number of characters or list items allowed. Must be equal to or greater than zeropatternDefines the exact sequence of characters that are acceptable totalDigitsSpecifies the exact number of digits allowed. Must be greater than zerowhiteSpaceSpecifies how white space (line feeds, tabs, spaces, and carriage returns) are handled
> ? @ V ' (
)
<
@
s
t
x
|
˿Ӟ˿˿z˿˿e˿ (j, h89 B*CJ OJ QJ UaJ ph (j h89 B*CJ OJ QJ UaJ ph h89 5B*CJ \aJ ph (jd h89 B*CJ OJ QJ UaJ ph h89 B*CJ aJ ph h89 B*CJ aJ ph h89 CJ aJ h89 B*CJ OJ QJ aJ ph (j h89 B*CJ OJ QJ UaJ ph h89 CJ aJ ' > @ V p# { p# { p# Z p# { p# p# p# p# p# p# p# l , y kd $$If #
0 6 3 4 `
a bp
$If gd89 gd89 gd89 gd89 '
(
)
p# { p# { p# Z p# { p# p# p# p# p# q im
p# { p# { p# Z y kd $$If #
0 6 3 4 `
a bp
$If gd89 gd89 gd89
: S h |
-
B
X
f
s
p# { p# p# p# p# p# p# p# p# p# p# p# p# p# p# p# p# $If gd89 s
t
$ + 9 K g ^! p# { | p# { w p# Z p# { q p# q p# q p# q p# q p# q p# q p# q p# $If gd89 gd89 gd89 y kdd $$If #
0 6 3 4 `
a bp
, b v p# { p# { { p# { v p# Z q p# { p# p# p# p# p# p# p# gd89 gd89 gd89 y kd $$If #
0 6 3 4 `
a bp
$If
Q R S h F - Y Z x
= > < r s 3 j k ǻǻǻǻǻǻǻljǻǻǻǻ (j@ h89 B*CJ OJ QJ UaJ ph (j h89 B*CJ OJ QJ UaJ ph h89 CJ aJ h89 B*CJ aJ ph h89 B*CJ aJ ph (j h89 B*CJ OJ QJ UaJ ph h89 B*CJ OJ QJ aJ ph h89 CJ aJ /
Q S h p# p# p# p# p# { v p# { q p# { l p# Z g p# { p# gd89 gd89 gd89 gd89 ~ kd, $$If #
0 6 3 4 `
a bp
$If F \ p# p# p# p# im
{ p# { p# p# p# p# p# p# p# p# gd89 y kd $$If #
0 6 3 4 `
a bp
$If - Y p# { { p# $If gd89 y kdX $$If #
0 6 3 4 `
a bp
Y Z p# { p# { { p# $If gd89 y kd $$If #
0 6 3 4 `
a bp
i x p# { p# { { p# $If gd89 y kd $$If #
0 6 3 4 `
a bp
p# { p# { { p# $If gd89 y kd
$$If #
0 6 3 4 `
a bp
& \ p# { | p# { w p# Z p# { q p# q p# q p# q p# q p# q p# q p# q p# $If gd89 gd89 gd89 y kdx $$If #
0 6 3 4 `
a bp
= p# { p# { p# gd89 y kd $$If #
0 6 3 4 `
a bp
$If = > < r p# { p# { { p# $If gd89 y kd
$$If #
0 6 3 4 `
a bp
r s 3 j p# { { p# $If gd89 y kdl $$If #
0 6 3 4 `
a bp
j k - 3 5 T | p# { | p# { w p# { r p# Z m p# { m p# { m p# { w p# { r p# Z m p# { gd89 gd89 gd89 gd89 ~ kd4 $$If #
0 6 3 4 `
a bp
- 3 4 5 T ^ _ " # $ % & C ! ! x! ! ! ! >" DzǦǦǦۑ|ǦkǦc h89 CJ aJ !h89 0J 5B*CJ \aJ ph (j( h89 B*CJ OJ QJ UaJ ph (j h89 B*CJ OJ QJ UaJ ph h89 B*CJ aJ ph (j h89 B*CJ OJ QJ UaJ ph h89 B*CJ aJ ph h89 CJ aJ h89 B*CJ OJ QJ aJ ph (j h89 B*CJ OJ QJ UaJ ph " 0 > ^ p# { p# { v p# { p# p# p# gd89 gd89 y kd4 $$If #
0 6 3 4 `
a bp
$If ^ _ " A p# { { p# { p# { p# $If gd89 y kd $$If #
0 6 3 4 `
a bp
" # $ & C A p# { p# { | p# Z w p# { h p# } h p# } h p# } h p# } h p# } h p# } p# {
&