XML Tutorial/XML Schema/Introduction

Материал из Web эксперт
Перейти к: навигация, поиск

Create global complex types

<?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="supportedLanguages">
    <xs:restriction base="xs:language">
      <xs:enumeration value="en" />
      <xs:enumeration value="es" />
    </xs:restriction>
  </xs:simpleType>
  
  <xs:attribute name="lang" type="supportedLanguages" />
  <xs:complexType name="tokenWithLang">
    <xs:simpleContent>
      <xs:extension base="xs:token">
        <xs:attribute ref="lang" />
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:element name="title" type="tokenWithLang" />
</xs:schema>


Derivation by extension

<?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="supportedLanguages">
    <xs:restriction base="xs:language">
      <xs:enumeration value="en" />
      <xs:enumeration value="es" />
    </xs:restriction>
  </xs:simpleType>
  
  <xs:attribute name="lang" type="supportedLanguages" />
  <xs:complexType name="tokenWithLang">
    <xs:simpleContent>
      <xs:extension base="xs:token">
        <xs:attribute ref="lang" />
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  
  <xs:element name="title">
    <xs:complexType>
      <xs:simpleContent>
        <xs:extension base="tokenWithLang">
          <xs:attribute name="note" type="xs:token" />
        </xs:extension>
      </xs:simpleContent>
    </xs:complexType>
  </xs:element>
</xs:schema>


Derivation by restriction

<?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:complexType name="tokenWithLangAndNote">
    <xs:simpleContent>
      <xs:extension base="xs:token">
        <xs:attribute name="lang" type="xs:language" />
        <xs:attribute name="note" type="xs:token" />
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
</xs:schema>


Derived Datatypes

Type Name          Type                             Derivation
integer            Built-in derived                 decimal
token              Built-in derived                 normalizedString
nonPositiveInteger Built-in derived                 integer
long               Built-in derived                 integer
nonNegativeInteger Built-in derived                 integer
language           Built-in derived                 token
Name               Built-in derived                 token
NMTOKEN            Built-in derived                 token
negativeInteger    Built-in derived                 nonPositiveInteger
int                Built-in derived                 long
unsignedLong       Built-in derived                 nonNegativeInteger
positiveInteger    Built-in derived                 nonNegativeInteger
NCName             Built-in derived                 Name
NMTOKENS           Built-in derived                 NMTOKEN
short              Built-in derived                 int
unsignedInt        Built-in derived                 unsignedLong
ID                 Built-in derived                 NCName
IDREF              Built-in derived                 NCName
ENTITY             Built-in derived                 NCName
byte               Built-in derived                 short
unsignedShort      Built-in derived                 unsignedInt
IDREFS             Built-in derived                 IDREF
ENTITIES           Built-in derived                 ENTITY
unsignedByte       Built-in derived                 unsignedShort


Getting Started with XML Schemas

File: Data.xml
<?xml version = "1.0" ?>
<Customer>
   <FirstName>First</FirstName>
   <MiddleInitial>Middle</MiddleInitial>
   <LastName>Last</LastName>
</Customer>
File: Schema.xsd
<?xml version = "1.0" ?>
<schema xmlns = "http://www.w3.org/2001/XMLSchema">
   <element name = "Customer">
      <complexType>
         <sequence>
            <element name = "FirstName" type = "string" />
            <element name = "MiddleInitial" type = "string" />
            <element name = "LastName" type = "string" />
         </sequence>
      </complexType>
   </element>
</schema>


Primitive Datatypes

Type Name          Type                             Derivation
duration           Built-in primitive               anySimpleType
dateTime           Built-in primitive               anySimpleType
time               Built-in primitive               anySimpleType
date               Built-in primitive               anySimpleType
gYearMonth         Built-in primitive               anySimpleType
gYear              Built-in primitive               anySimpleType
gMonthDay          Built-in primitive               anySimpleType
gDay               Built-in primitive               anySimpleType
gMonth             Built-in primitive               anySimpleType
boolean            Built-in primitive               anySimpleType
base64Binary       Built-in primitive               anySimpleType
hexBinary          Built-in primitive               anySimpleType
float              Built-in primitive               anySimpleType
double             Built-in primitive               anySimpleType
anyURI             Built-in primitive               anySimpleType
QName              Built-in primitive               anySimpleType
NOTATION           Built-in primitive               anySimpleType
string             Built-in primitive               anySimpleType
decimal            Built-in primitive               anySimpleType


<schema> Declarations

To connect the two documents, you include a reference to the XML Schema within your instance document. 
You begin XML schema with the XML declaration, <?xml version="1.0"?>.
The root element within your XML Schema is the <schema> element.
Within the <schema> element, you have the namespace declaration.
The namespace of the <schema> element is http://www.w3.org/2001/XMLSchema.
targetNamespace attribute indicates that the vocabulary is for the namespace http://www.wbex.ru/name. 
You can declare a namespace that matches your targetNamespace with the prefix target to refer to any declarations within your XML Schema. 
The attribute elementFormDefault controls the way namespaces are used within your corresponding XML document. 
<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" 
        xmlns:target="http://www.wbex.ru/name" 
        targetNamespace="http://www.wbex.ru/name" 
        elementFormDefault="qualified"> 

</schema>


<schema> element is the root element within an XML Schema.

<schema targetNamespace="URI" 
        attributeFormDefault="qualified or unqualified" 
        elementFormDefault="qualified or unqualified" 
        version="version number"> 
You could use any of the following for the XML schema namespace : 
<schema xmlns="http://www.w3.org/2001/XMLSchema">              or 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">        or 
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 

Within the <schema> element you can specify how elements should be qualified: elementFormDefault and attributeFormDefault 
The elementFormDefault and attributeFormDefault attributes enable you to control the default qualification form for elements and attributes.
The default value is unqualified.


Simple and Complex Types

Elements that can contain only text are simple type. 
Elements that contain other elements or that contain attributes are a complex type. 
Attributes are simple type.
There are several built-in simple types, like date, integer, and string. 
You can build custom simple types.
Elements of complex type describe the structure of a document, rather than its content. 
There are four basic kinds of complex types: 
    elements that contain just other elements,
    elements that contain both elements and text, 
    elements that contain only text
    elements that are empty. 
Each of these may contain attributes as well. 
Both simple and custom types may be named, which can be reused in other places.
Both simple and custom types may be anonymous, which are used only within the element in which the definition appears.


Target Namespaces

The primary purpose of XML Schemas is to declare vocabularies. 
These vocabularies can be identified by a namespace that is specified in the targetNamespace attribute.
Not all XML Schemas will have a targetNamespace.
Many XML Schemas define vocabularies that are reused in another XML Schema.
Possible targetNamespace declarations include the following: 
<schema xmlns="http://www.w3.org/2001/XMLSchema" 
        targetNamespace="http://www.wbex.ru/name" 
        xmlns:target="http://www.wbex.ru/name"> 
or 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
           targetNamespace="http://www.wbex.ru/name" 
           xmlns="http://www.wbex.ru/name"> 

In the first declaration, the <schema> element uses the default namespace. 
Because of this the target namespace http://www.wbex.ru/name requires the use of a prefix. 
In the second declaration, the <schema> element requires the use of a prefix.
Because the target namespace http://www.wbex.ru/nameis using a default namespace declaration.


Useful built-in simple types

Built-in type           Description                                                Example(s) 
xsd:string              A sequence of any of the legal XML characters              This is a string. 
xsd:boolean             The value true or false, or 1(true) or 0(false)            true, 
                                                                                   false
                                                                                   1
                                                                                   0
xsd:decimal             A number that may contain a decimal component              -5.2
xsd:integer             A whole number                                             -389
xsd:positiveInteger     A positive whole number (not including 0)                  5
xsd:negativeInteger     A negative whole number (not including 0)                  -389
xsd:date                A calendar date, represented as CCYY-MM-DD                 2008-05-21
xsd:time                A time of day, represented as hh:mm:ss.ss                  11:30:00.00 (11:30 A.M.)
xsd:dateTime            A date and time of day, CCYY-MM-DD Thh:mm:ss.ss            2008-05-21T17:28:00.00
xsd:gMonth              A Gregorian calendar month, represented as --MM--          --05-- (May)
xsd:gYear               A Gregorian calendar year, represented as CCYY             2008
xsd:gDay                A day of a Gregorian calendar month, represented as ---DD  ---05
xsd:gYearMonth          A Gregorian calendar year and month, as CCYY-MM            2008-05 (May, 2008)
xsd:anyURI              A URI                                                      http://www.wbex.ru


XML Schema defines the name vocabulary.

It shows how to refer to the XML Schema from the instance document.
You used the XML Schema to determine whether your instance document was schema valid.
File: Schema.xsd
<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
  xmlns:target="http://www.wbex.ru/name"
  targetNamespace="http://www.wbex.ru/name"
  elementFormDefault="qualified">
  <element name="name">
    <complexType>
      <sequence>
        <element name="first" type="string" />
        <element name="middle" type="string" />
        <element name="last" type="string" />
      </sequence>
      <attribute name="title" type="string" />
    </complexType>
  </element>
</schema>

File: Data.xsl
<?xml version="1.0"?>
<name xmlns="http://www.wbex.ru/name"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.wbex.ru/name name5.xsd"
  title="Mr.">
  <first>John</first>
  <middle>Joh</middle>
  <last>Doe</last>
</name>