XML(10) XML Schemas in general

来源:互联网 发布:软件评测师证书 编辑:程序博客网 时间:2024/05/05 04:17

XML Schemas are more powerful than DTD.

Uses XML syntax

Unlike DTD, XML schemas supports Namespace

How to include Schema in XML File

To include a schema, you must first define xsi namespace. xsi namespace contain value "http://www.w3.org/2001/XMLSchema-instance". So the definition is


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

The schema files are declared in xsi:schemaLocation attribute

This attribute can contain many values in pairs, separated by a space. The first value in the pair is the namespace to use. The second value in the pair is the path of the XML schema to use for that namespace. The path is always the URL to the schema file. However It is possible to use local file path. The first value and the second value are separated by a space too. Example of a schema declaration:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xmlns:aop="http://www.springframework.org/schema/aop"       xmlns:tx="http://www.springframework.org/schema/tx"       xsi:schemaLocation="       http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd       http://www.springframework.org/schema/tx        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd       http://www.springframework.org/schema/aop        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">


The XML <Schema> Namespace

The <schema> element is the root element of every XML Schema.

"http://www.w3.org/2001/XMLSchema" should be the default namespace of XML Schema

There are three choces, the xs namespace is recommended, the first is also very common:

<schema xmlns="http://www.w3.org/2001/XMLSchema">

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

or

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">


Target Namespace

Elements defined by this schema come from this specific namespace


elementFormDefault="qualified"

indicates that any elements used by the XML instance document which were declared in this schemamustbe namespace qualified.



<element> Declarations

In element declaration, you specify element name and its content
element can be 
<element
name="name of the element"
type="global type"
ref="global element declaration"
form="qualified or unqualified"
minOccurs="non negative number"
maxOccurs="non negative number or 'unbounded'"
default="default value"
fixed="fixed value">

may contain global 

Global and Local declarations

Global declarations are declarations that appear as direct children of the <schema> element.

Local declaration are NOT direct children of the <schema> element


Simple Types

Element

A simple element is an XML element that contains only text. It cannot contain any other elements or attributes.

Simple Element Format:

<xs:element name="xxx" type="yyy"/>

<xs:element name="lastname" type="xs:string"/><xs:element name="age" type="xs:integer"/><xs:element name="dateborn" type="xs:date"/>

Attribute Format:
<xs:attribute name="xxx" type="yyy"/>
<xs:attribute name="lang" type="xs:string"/>


The type must be under the namespace "http://www.w3.org/2001/XMLSchema", which is the default namespace

  • string
  • decimal
  • integer
  • boolean
  • date
  • time


Restriction
Restriction can be applied to value, or series of values, length, whitespace.
<xs:element name="password">  <xs:simpleType>    <xs:restriction base="xs:string">      <xs:minLength value="5"/>      <xs:maxLength value="8"/>    </xs:restriction>  </xs:simpleType></xs:element>

Complex Types

Four kinds of complex Types:
  • empty elements
  • elements that contain only other elements
  • elements that contain only text
  • elements that contain both other elements and text

declaration example:

declare inside the element

<xs:element name="employee">  <xs:complexType>    <xs:sequence>      <xs:element name="firstname" type="xs:string"/>      <xs:element name="lastname" type="xs:string"/>    </xs:sequence>  </xs:complexType></xs:element>

<sequence> tag means the child element must be in the exact order as they are declared.

declare outside of the element:

<xs:element name="employee" type="personinfo"/><xs:complexType name="personinfo">  <xs:sequence>    <xs:element name="firstname" type="xs:string"/>    <xs:element name="lastname" type="xs:string"/>  </xs:sequence></xs:complexType>

declare by including the other complex type:

<xs:element name="employee" type="fullpersoninfo"/><xs:complexType name="personinfo">  <xs:sequence>    <xs:element name="firstname" type="xs:string"/>    <xs:element name="lastname" type="xs:string"/>  </xs:sequence></xs:complexType><xs:complexType name="fullpersoninfo">  <xs:complexContent>    <xs:extension base="personinfo">      <xs:sequence>        <xs:element name="address" type="xs:string"/>        <xs:element name="city" type="xs:string"/>        <xs:element name="country" type="xs:string"/>      </xs:sequence>    </xs:extension>  </xs:complexContent></xs:complexType>
<complexContent> means we are going to extend or restrict the content model.


Mixed Type

xml:
<letter>  Dear Mr.<name>John Smith</name>.  Your order <orderid>1032</orderid>  will be shipped on <shipdate>2001-07-13</shipdate>.</letter>
xsd:

<xs:element name="letter" type="lettertype"/><xs:complexType name="lettertype" mixed="true">  <xs:sequence>    <xs:element name="name" type="xs:string"/>    <xs:element name="orderid" type="xs:positiveInteger"/>    <xs:element name="shipdate" type="xs:date"/>  </xs:sequence></xs:complexType>



Indicators

We can control HOW elements are to be used in documents with indicators.
Order indicators:
  • All
  • Choice
  • Sequence
Occurrence indicators:
  • maxOccurs
  • minOccurs
Group indicators:
  • Group name
  • attributeGroup name

<any> and <anyAttribute>

The <any> and <anyAttribute> elements are used to make EXTENSIBLE documents! They allow documents to contain additional elements that are not declared in the main XML schema.

<any>:

<xs:element name="person">  <xs:complexType>    <xs:sequence>      <xs:element name="firstname" type="xs:string"/>      <xs:element name="lastname" type="xs:string"/>      <xs:any minOccurs="0"/>    </xs:sequence>  </xs:complexType></xs:element>
<anyAttribute>

<xs:element name="person">  <xs:complexType>    <xs:sequence>      <xs:element name="firstname" type="xs:string"/>      <xs:element name="lastname" type="xs:string"/>    </xs:sequence>    <xs:anyAttribute/>  </xs:complexType></xs:element>



Element Substitution

example: substitutionGroup

you can use either of them, <name> or <navn>

<span style="font-family: 'courier new'; font-size: 13.333333015441895px; "><xs:element name="name" type="xs:string"/></span><br style="font-family: 'courier new'; font-size: 13.333333015441895px; " /><span style="font-family: 'courier new'; font-size: 13.333333015441895px; "><xs:element name="navn" substitutionGroup="name"/></span><br style="font-family: 'courier new'; font-size: 13.333333015441895px; " /><br style="font-family: 'courier new'; font-size: 13.333333015441895px; " /><span style="font-family: 'courier new'; font-size: 13.333333015441895px; "><xs:complexType name="custinfo"></span><br style="font-family: 'courier new'; font-size: 13.333333015441895px; " /><span style="font-family: 'courier new'; font-size: 13.333333015441895px; ">  <xs:sequence></span><br style="font-family: 'courier new'; font-size: 13.333333015441895px; " /><span style="font-family: 'courier new'; font-size: 13.333333015441895px; ">    <xs:element ref="name"/></span><br style="font-family: 'courier new'; font-size: 13.333333015441895px; " /><span style="font-family: 'courier new'; font-size: 13.333333015441895px; ">  </xs:sequence></span><br style="font-family: 'courier new'; font-size: 13.333333015441895px; " /><span style="font-family: 'courier new'; font-size: 13.333333015441895px; "></xs:complexType></span><br style="font-family: 'courier new'; font-size: 13.333333015441895px; " /><br style="font-family: 'courier new'; font-size: 13.333333015441895px; " /><span style="font-family: 'courier new'; font-size: 13.333333015441895px; "><xs:element name="customer" type="custinfo"/></span><br style="font-family: 'courier new'; font-size: 13.333333015441895px; " /><span style="font-family: 'courier new'; font-size: 13.333333015441895px; "><xs:element name="kunde" substitutionGroup="customer"/></span>


In this example, you can't use <navn>

block="substitution" prevent you from using substitution

<xs:element name="name" type="xs:string" block="substitution"/><xs:element name="navn" substitutionGroup="name"/><xs:complexType name="custinfo">  <xs:sequence>    <xs:element ref="name"/>  </xs:sequence></xs:complexType><xs:element name="customer" type="custinfo" block="substitution"/><xs:element name="kunde" substitutionGroup="customer"/>

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!






原创粉丝点击