XML Tutorial/XML Schema/Simple Type

Материал из Web эксперт
Версия от 18:22, 25 мая 2010; (обсуждение)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

A ISO date without a time zone

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  targetNamespace="http://www.wbex.ru" xmlns="http://www.wbex.ru"
  elementFormDefault="qualified">
  <xs:simpleType name="dateISO">
    <xs:restriction base="xs:date">
      <xs:pattern value="[^:Z]*" />
    </xs:restriction>
  </xs:simpleType>
</xs:schema>


An anonymous simple string datatype for which any one of three literal string patterns (Small, Medium, or Large) are acceptable

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  targetNamespace="http://www.wbex.ru" xmlns="http://www.wbex.ru"
  elementFormDefault="qualified">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:pattern value="Small|Medium|Large" />
    </xs:restriction>
  </xs:simpleType>
</xs:schema>


Declaring an Element with a Simple Type

An element has a simple type if it"s only allowed to contain other elements or attributes. 
There are many different built-in simple types.
You can create your own, based on one of the built-in types.
File: Schema.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  targetNamespace="http://www.wbex.ru" 
  xmlns="http://www.wbex.ru"
  elementFormDefault="qualified">
  <xs:element name="weight" type="xs:string" />
</xs:schema>

File: Data.xml 
<?xml version="1.0"?>
<weight>3 points</weight>
File: Schema.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  targetNamespace="http://www.wbex.ru" xmlns="http://www.wbex.ru"
  elementFormDefault="qualified">
  <xs:element name="population" type="xs:integer" />
</xs:schema>

File: Data.xml
<?xml version="1.0"?>
<population>28</population>


Simple Type Definitions

<simpleType final="#all | (list | union | restriction)"
      id="ID"
      name="NCName"
      {any attributes with non-schema namespace}
>
  Content: (annotation?, (restriction | list | union))
</simpleType>


The simple type may be defined globally, as a child of an attribute declaration, or within an element content model

<xsd:simpleType name="deptType">
  <xsd:restriction base="xsd:string">
   <xsd:enumeration value="management"/>
   <xsd:enumeration value="programming"/>
   <xsd:enumeration value="design"/>
   <xsd:enumeration value="training"/>
  </xsd:restriction>
</xsd:simpleType>


User-Defined Data types

You can create custom user-defined data types using the <simpleType> definition: 
<simpleType name="name of the simpleType" 
            final="#all or list or union or restriction"> 
To declare a <simpleType>, you must always base your declaration on an existing data type. 
The existing data type may be a built-in XML Schema data type or a custom datatype. 
<simpleType>definitions are often called derived types. 
There are three primary derived types: 
Restriction types 
List types 
Union types


XML Schemas provide a number of built-in simple types.

Type               Description 
string             Any character data 
normalizedString   A whitespace-normalized string
token              A string that does not contain sequences of two or more spaces, tabs, carriage returns, or linefeed characters 
byte               A numeric value from -128 to 127 
unsignedByte       A numeric value from 0 to 255 
base64Binary       Base64 encoded binary information 
hexBinary          Hexadecimal encoded binary information 
integer            A numeric value representing a whole number 
positiveInteger    An integer whose value is greater than 0 
negativeInteger    An integer whose value is less than 0 
nonNegativeInteger An integer whose value is 0 or greater 
nonPositiveInteger An integer whose value is less than or equal to 0 
int                A numeric value from ?47483648 to 2147483647 
unsignedInt        A numeric value from 0 to 4294967295 
long               A numeric value from ?23372036854775808 to 9223372036854775807 
unsignedLong       A numeric value from 0to 18446744073709551615 
short              A numeric value from ?768 to 32767 
unsignedShort      A numeric value from 0 to 65535 
decimal            A numeric value that may or may not include a fractional part 
float              32-bit floating-point type, INF, -INF, and NaN are also valid values. 
double             64-bit floating-point type. -0, INF, -INF, and NaN are also valid values. 
boolean            true, false, 0, and 1 time. 
dateTime           An instant of time. For example, 2008-07-12T16:30:00.000. 
duration           A span of time. For example, P30D is 30 days. 
date               A date of the Gregorian calendar. For example, 2008-05-25 is a valid date value. 
gMonth             A month of Gregorian calendar. For example, --07 is a valid gMonth value. 
gYear              A year of Gregorian calendar. For example, 2008 is a valid gYear value. 
gYearMonth         A specific month and year. For example, 1998-07is a valid gYearMonth value. 
gDay               A recurring day of the month. For example, ---12 is a valid gDay value. 
gMonthDay          A recurring day of a specific month. For example, --07-12 is a valid gMonthDay value. 
name               An XML name according to the Namespace Recommendation. 
QName              A qualified XML name as defined in the Namespaces Recommendation. 
NCName             A noncolonized XML name that does not include a namespace prefix or colon 
anyURI             A valid Uniform Resource Identifier (URI) 
language           A language constant as defined in RFC 1766 

In addition to the types listed, the XML Schema Recommendation also allows the types defined within 
the XML Recommendation. These types include ID, IDREF, IDREFS, ENTITY, ENTITIES, NOTATION, 
NMTOKEN, and NMTOKENS.