schema教程
XML Schema是以XML语言为基础的,它用于可替代DTD。
一份XML schema文件描写叙述了XML文档的结构
XML Schema语言也被称为XML Schema Definition (XSD)(XML Schema定义).
在本章教程你将学习怎样读取和建立XML Schemas,以及为什么XML Schemas比DTDs的功能更为强大,还有怎样在你的程序中使用XML Schema语言
XML Schema 參考资料
你能够找到完整的有效的XML Schema元素
XML Schema 參考
Schema 介绍
w3pop.com / 2006-09-19
为何使用 XML Schemas?
XML Schema is an XML-based alternative to DTD.
XML Schema是以XML语言为基础的,它用于可替代DTD。
An XML schema describes the structure of an XML document.
一份XML schema文件描写叙述了XML文档的结构。
The XML Schema language is also referred to as XML Schema Definition (XSD).
XML Schema语言也被称为XML Schema Definition (XSD)(XML Schema定义).
What You Should Already Know
你应该已经掌握的知识
Before you continue you should have a basic understanding of the following:
在继续之前你应对以下内容有基本了解:
* HTML / XHTML
* XML and XML Namespaces
XML 和 XML 名称空间
* A basic understanding of DTD
对DTD有主要的了解
If you want to study these subjects first, find the tutorials on our Home page.
假设你想先学这些内容,请看我们主页上的的学习教程
What is an XML Schema?
什么是XML Schema?
The purpose of an XML Schema is to define the legal building blocks of an XML document, just like a DTD.
XML Schema的作用是定义一份XML文档的合法组件群,就像DTD的作用一样
An XML Schema:
一份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
定义了元素和属性的默认值和固定值
XML Schemas are the Successors of DTDs
XML Schema 是DTD的“接任者”
We think that very soon XML Schemas will be used in most Web applications as a replacement for DTDs. Here are some reasons:
我们觉得XML Schema非常快就会替代DTD在大多数的网络应用程序中被广泛使用。
* XML Schemas are extensible to future additions
XML Schema针对将来的额外内容是可扩展的
* XML Schemas are richer and more powerful than DTDs
XML Schema内容比DTD丰富,作用也更大
* XML Schemas are written in XML
XML Schema是以XML语言编写而成的
* XML Schemas support data types
XML Schema支持数据类型
* XML Schemas support namespaces
XML Schema支持名称空间(namespaces)
XML Schema is a W3C Standard
XML Schema是W3C定义的标准
XML Schema became a W3C Recommendation 02. May 2001.
XML Schema于2001年5月2日成为W3C推荐使用的标准
You can read more about the XML Schema standard in our W3C tutorial.
在W3C 教程中你能阅读到很多其它关于XML Schema标准。
为何使用 XML Schemas?
w3pop.com / 2006-09-21
Schema 介绍 怎样制定 XSD
XML Schemas are much more powerful than DTDs.
XML Schema比DTD作用更加强大。
XML Schemas Support Data Types
XML Schema支持数据类型
One of the greatest strength of XML Schemas is the support for data types.
XML Schema的最好用的地方之中的一个就是它支持数据类型
With support for data types:
支持数据类型所带来的优点:
* It is easier to describe allowable document content
更易于描写叙述被同意的文档内容
* It is easier to validate the correctness of data
更易于检验数据的正确性
* It is easier to work with data from a database
更易于与数据库中的数据一起协同工作
* It is easier to define data facets (restrictions on data)
更易于定义数据的使用面(关于数据的限制)
* It is easier to define data patterns (data formats)
更易于定义数据样式(数据格式)
* It is easier to convert data between different data types
更易于把数据转换成不同的数据类型
XML Schemas use XML Syntax
XML Schema使用XML的语法
Another great strength about XML Schemas is that they are written in XML.
XML Schema还有一个优点就是他们是用XML编写成的。
Some benefits of that XML Schemas are written in XML:
用XML编写的优点是:
* You don't have to learn a new language
能够不须要再学一种新语言
* You can use your XML editor to edit your Schema files
能够用XML编辑器来编辑Schema文件
* You can use your XML parser to parse your Schema files
能够用XML解析器解析Schema文件
* You can manipulate your Schema with the XML DOM
能够用XML DOM 处理Schema
* You can transform your Schema with XSLT
能够用XSLT转换Schema
XML Schemas Secure Data Communication
XML Schema安全数据通讯
When sending data from a sender to a receiver, it is essential that both parts have the same "expectations" about the content.
当数据由发送者被传向接受者时,两方对“数据内容理解的一致性”就显得非常重要了
With XML Schemas, the sender can describe the data in a way that the receiver will understand.
在XML Schema里,发送者能够用接受者能够理解的方式描写叙述数据。
A date like: "03-11-2004" will, in some countries, be interpreted as 3.November and in other countries as 11.March.
像这样格式的日期:"03-11-2004",一些国家会解释成11月3日,而别的国家会解释成3月11日
However, an XML element with a data type like this:
<date type="date">2004-03-11</date>
ensures a mutual understanding of the content, because the XML data type "date" requires the format "YYYY-MM-DD".
然而,一个有着像这种数据种类的XML元素:<date type="date">2004-03-11</date>,就确保了两方都能理解其内容,由于XML数据类型的"date"要求规定为"YYYY-MM-DD"的格式。
XML Schemas are Extensible
XML Schema是可扩展的
XML Schemas are extensible, because they are written in XML.
由于XML Schema文件是由XML编写的,所以它们是可扩展的。
With an extensible Schema definition you can:
Schema可扩展意味着你能够:
* Reuse your Schema in other Schemas
在别的Schema文件中再次用到你的Schema
* Create your own data types derived from the standard types
从标准的数据类型中派生出你自己的数据类型
* Reference multiple schemas in the same document
在同样的文档中參考多种Schema。
Well-Formed is not Enough
光有“规范”仍然是不够的
A well-formed XML document is a document that conforms to the XML syntax rules, like:
规范的XML文档是符合XML语法规则的文档,就像这样:
* it must begin with the XML declaration
? 开头必须有XML声明
* it must have one unique root element
? 必须有一个独特的根文件夹元素
* start-tags must have matching end-tags
? 開始标签(start-tag)必须和结束标签(end-tag)匹配
* elements are case sensitive
? 元素是有大写和小写区分的
* all elements must be closed
? 元素必须有结束
* all elements must be properly nested
? 全部的元素必须被合理嵌套
* all attribute values must be quoted
? 全部属性值必须写在引號里
* entities must be used for special characters
? 用特殊字符时必须用到实体
Even if documents are well-formed they can still contain errors, and those errors can have serious consequences.
即使文档是规范的,它们也可能包括错误。这些错误可能会引起严重的后果。
Think of the following situation: you order 5 gross of laser printers, instead of 5 laser printers. With XML Schemas, most of these errors can be caught by your validating software.
试想以下的情况:你预定了 5罗(1罗=12打=144台)激光打印机,而不是5台打印机。在XML Schema文件中,大多数像这种错误会由检验软件产生。
怎样制定 XSD
w3pop.com / 2006-09-21
为何使用 XML Schemas? XSD - <schema> 元素
XML documents can have a reference to a DTD or to an XML Schema.
XML文档能和一份DTD或XML Schema文件相关联。
A Simple XML Document
一份简单的XML文档
Look at this simple XML document called "note.xml":
请看以下名为"note.xml"的一份简易的XML文档
<?xml version="1.0"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
A DTD File
一份DTD文件
The following example is a DTD file called "note.dtd" that defines the elements of the XML document above ("note.xml"):
以下的样例是名为"note.dtd"的DTD文件,它定义了上述XML文档("note.xml")的元素。
<!ELEMENT note (to, from, heading, body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
The first line defines the note element to have four child elements: "to, from, heading, body".
第一行定义了含有"to, from, heading, body".四个子元素的“note”元素。
Line 2-5 defines the to, from, heading, body elements to be of type "#PCDATA".
第2到5行定义了"#PCDATA". 类型的“to, from, heading, body”四个元素
An XML Schema
一份XML Schema
The following example is an XML Schema file called "note.xsd" that defines the elements of the XML document above ("note.xml"):
以下的样例其中,一个名为"note.xsd"的XML Schema文件定义了上述XML文档("note.xml")中的元素
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
The note element is a complex type because it contains other elements. The other elements (to, from, heading, body) are simple types because they do not contain other elements. You will learn more about simple and complex types in the following chapters.
Note元素是复合类型(complex type)由于它包括了其它元素,其它元素(to, from, heading, body)属于简单类型(simple type),由于它不包括其它元素。在以下几章里你会学到很多其它关于简单类型和复合类型的相关知识。
A Reference to a DTD
和DTD相关
This XML document has a reference to a DTD:
XML文档和DTD相关:
<?xml version="1.0"?>
<!DOCTYPE note SYSTEM
"http://www.w3schools.com/dtd/note.dtd">
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
A Reference to an XML Schema
和XML Schema相关
This XML document has a reference to an XML Schema:
以下的XML文档和XML Schema相关:
<?xml version="1.0"?>
<note
xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3schools.com note.xsd">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
XSD - <schema> 元素
w3pop.com / 2006-09-21
怎样制定 XSD XSD Simple 元素
The <schema> element is the root element of every XML Schema.
<schema>元素是每一个XML Schema文件的根元素。
The <schema> Element
<schema>元素
The <schema> element is the root element of every XML Schema:
<schema>元素是每份XML Schema文件的根元素。
<?xml version="1.0"?>
<xs:schema>
...
...
</xs:schema>
The <schema> element may contain some attributes. A schema declaration often looks something like this:
<schema>元素也能够含有一些属性,一个schema声明常常写成这样:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
...
...
</xs:schema>
The following fragment:
看以下的片段:
xmlns:xs="http://www.w3.org/2001/XMLSchema"
indicates that the elements and data types used in the schema 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:
指明了在schema中使用的元素和数据种类来自http://www.w3.org/2001/XMLSchema名称空间(namespace)。它也指定了来自"http://www.w3.org/2001/XMLSchema"名称空间(namespace)的元素和数据种类必须带前缀“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.
暗示了由这份schema(note, to, from, heading, body.)定义的元素来自"http://www.w3schools.com"名称空间(namespace)
This fragment:
这个片段:
xmlns="http://www.w3schools.com"
indicates that the default namespace is http://www.w3schools.com.
指明了默认名称空间(namespace)是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.
指明了由这份schema声明的XML实例文档里用到的不论什么元素,都必须是有效的名称空间(namespace qualified)。
Referencing a Schema in an XML Document
在一份XML文档里提到Schema
This XML document has a reference to an XML Schema:
一份XML文档里提提到XML Schiema
<?xml version="1.0"?>
<note xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3schools.com note.xsd">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
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.
指定了默认的名称空间(default namespace)声明。这个声明告诉schema-检验器:这份XML文档里用到的全部元素都在http://www.w3schools.com的名称空间(namespace)中声明过。
Once you have the XML Schema Instance namespace available:
一旦你有了能够利用的XML Schema Instance的名称空间(namespace):
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:
你能够用到SchemaLocation属性。这个属性有两个值。第一个值是要用到的名称空间(namespace)。第二个值是为名称空间(namespace)指定了须要使用的XML schema的位置。
xsi:schemaLocation="http://www.w3schools.com note.xsd"
XSD Simple 元素
w3pop.com / 2006-09-21
XSD - <schema> 元素 XSD 属性
XML Schemas define the elements of your XML files.
XML Schemas(XML公式)定义了XML文件的元素
A simple element is an XML element that contains only text. It cannot contain any other elements or attributes.
简单元素是仅仅含有文本的XML元素。它不能够含有其它元素或属性
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.
简单元素是仅仅含有文本的XML元素。它不能够含有其它元件或属性
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.
可是,“仅仅含文本”这个限定条件是非常easy引起误解的,文本能够有非常多的不同类型。它能够是XML Schema定义里的文本类型之中的一个(逻辑值,字符串,日期,等等),也能够是自己定义文本类型。
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.
你能够通过给数据类型加入限定条件来限制它的内容,或者你能够要求数据与指定的式样相匹配。
Defining a Simple Element
定义一个简明元素
The syntax for defining a simple element is:
定义简明元素的语法为:
<xs:element name="xxx" type="yyy"/>
where xxx is the name of the element and yyy is the data type of the element.
在这里, xxx是元素名称,yyy是元素的数据类型
XML Schema has a lot of built-in data types. The most common types are:
XML Schema本身有非常多数据种类。最常见的种类有:
* xs:string
xs:字符
* xs:decimal
xs: 小数
* xs:integer
xs:整数
* xs:boolean
xs:逻辑值
* xs:date
xs:日期
* xs:time
xs:时间
Example
样例
Here are some XML elements:
这里是一些XML元素
<lastname>Refsnes</lastname>
<age>36</age>
<dateborn>1970-03-27</dateborn>
And here are the corresponding simple element definitions:
以下是对应的简单元素定义
<xs:element name="lastname" type="xs:string"/>
<xs:element name="age" type="xs:integer"/>
<xs:element name="dateborn" type="xs:date"/>
Default and Fixed Values for Simple Elements
简单元素的默认值和固定值
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.
值没有被指定时,属性自己主动会设置成默认值
In the following example the default value is "red":
以下样例里默认值是"red":
<xs:element name="color" type="xs:string" default="red"/>
A fixed value is also automatically assigned to the element, and you cannot specify another value.
固定值是也是自己主动分派给属性的,而且,一旦有了固定值,你就不能指定其它值了。
In the following example the fixed value is "red":
以下样例里固定值是"red":
<xs:element name="color" type="xs:string" fixed="red"/>
XSD 属性
w3pop.com / 2006-09-21
XSD Simple 元素 XSD Restrictions/Facets
All attributes are declared as simple types.
全部属性都是以简单类型来声明的。
What is an Attribute?
属性是什么?
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.
其中,xxx是属性的名称,yyy指定了属性的数据类型
XML Schema has a lot of built-in data types. The most common types are:
XML Schema本身有非常多数据类型。最常见的数据类型有:
* xs:string
xs:字符串
* xs:decimal
xs:小数
* xs:integer
xs:整数
* xs:boolean
xs:逻辑值
* xs:date
xs:日期
* xs:time
xs:时间
Example
样例
Here is an XML element with an attribute:
这是一个有属性的XML元素:
<lastname lang="EN">Smith</lastname>
And here is the corresponding attribute definition:
而这个对应的属性定义:
<xs:attribute name="lang" type="xs:string"/>
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 "EN":
以下样例里默认值是"EN":
<xs:attribute name="lang" type="xs:string" default="EN"/>
A fixed value is also automatically assigned to the attribute, and you cannot specify another value.
固定值是也是自己主动分派给属性的,而且,一旦有了固定值,你就不能指定其它值了。
In the following example the fixed value is "EN":
以下样例里固定值是"EN":
<xs:attribute name="lang" type="xs:string" fixed="EN"/>
Optional and Required Attributes
随意属性和必需属性
Attributes are optional by default. To specify that the attribute is required, use the "use" attribute:
默认时属性是随意的,要指明属性是必需的,须用到"use"属性
<xs:attribute name="lang" type="xs:string" use="required"/>
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.
当XML元素或属性有了已定义的数据类型,元素或属性的内容会有约束
If an XML element is of type "xs:date" and contains a string like "Hello World", the element will not validate.
假设一个XML元素是"xs:date"类型,并包括着象"Hello World"的字符串,元素就不会进行检验
With XML Schemas, you can also 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.
你也能够用XML Schema给XML元素和属性加入约束限制。这些约束称为“面(facet)”。在下章里你能够读到很多其它关于“面”的内容
XSD Restrictions/Facets
w3pop.com / 2006-09-21
XSD 属性 XSD Complex 元素
Restrictions are used to define acceptable values for XML elements or attributes. Restrictions on XML elements are called facets.
约束用于给XML元素或属性定义可接受的值,关于对XML元素的约束称之为“面(facet)”
Restrictions on Values
对单个值的约束
The following example defines an element called "age" with a restriction. The value of age cannot be lower than 0 or greater than 120:
以下的样例给叫做"age"的元件定义了一个“约束(restriction)”。“age”的值要大等于0,小等于120:
<xs:element name="age">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="120"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
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.
为了限制XML元素的内容得到一组符合条件的值,我们会用到“列举约束(enumeration constraint)”。
The example below defines an element called "car" with a restriction. The only acceptable values are: Audi, Golf, BMW:
以下的样例给叫做"car"的元素定义了约束条件,符合条件的值有:Audi, Golf, BMW:
<xs:element name="car">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Audi"/>
<xs:enumeration value="Golf"/>
<xs:enumeration value="BMW"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
The example above could also have been written like this:
上面的样例也能够写成这样:
<xs:element name="car" type="carType"/>
<xs:simpleType name="carType">
<xs:restriction base="xs:string">
<xs:enumeration value="Audi"/>
<xs:enumeration value="Golf"/>
<xs:enumeration value="BMW"/>
</xs:restriction>
</xs:simpleType>
Note: In this case the type "carType" can be used by other elements because it is not a part of the "car" element.
注意:在这种情况下"carType"类型能够被其它元件所使用,由于它不是"car"元素的一部分
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.
为了限制XML元件的内容以定义一系列可被使用的数字或字母,我们能够用“式样约束(pattern constraints)”。
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:
以下的样例给叫做"letter"的元素定义可约束。唯一符合条件的值是 a到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 an element called "initials" with a restriction. The only acceptable value is THREE of the UPPERCASE letters from a to z:
接下来的样例给叫做"initials"的元素定义了一个约束。唯一符合条件的值是a到z之间的3个大写字母
<xs:element name="initials">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[A-Z][A-Z][A-Z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
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:
以下的样例给叫做"initials"的元素定义了一个约束。唯一符合条件的值是 a到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:
以下的样例给叫做"choice"的元素定义了一个约束,唯一符合条件的值是x,y,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 "prodid" 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:
以下的样例给叫做"prodid"的元素定义了一个约束,唯一符合条件的值是0到9的5个阿拉伯数字的排列,
<xs:element name="prodid">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:pattern value="[0-9][0-9][0-9][0-9][0-9]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Other Restrictions on a Series of Values
对一系列值的其它约束
The example below defines an element called "letter" with a restriction. The acceptable value is zero or more occurrences of lowercase letters from a to z:
以下的样例给叫做"letter"的元素定义了一个约束。唯一符合条件的值是a 到z的小写字母(能够有多个)或0
<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 also defines an element called "letter" with a restriction. The acceptable value is one or more pairs of letters, each pair consisting of a lower case letter followed by an upper case letter. For example, "sToP" will be validated by this pattern, but not "Stop" or "STOP" or "stop":
以下的样例也给叫做"letter"的元素定义了一个约束。唯一符合条件的值是一对或多对字母,每对都是一个小写字母后跟一个大写字母组成。举个样例,"sToP"在这种式样里是有效正确的,但"Stop" ,"STOP" 或 "stop"就都不是了。
<xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="([a-z][A-Z])+"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
The next example defines an element called "gender" with a restriction. The only acceptable value is male OR female:
以下的样例也给叫做"gender"的元素定义了一个约束。唯一符合的值是male (男性)或female(女性):
<xs:element name="gender">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="male|female"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
The next example defines an element called "password" 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:
以下的样例也给叫做"password"的元素定义了一个约束。一行里必须有8个字符,字符必须是a到z大或小写字母,或者是0到9的数字
<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z0-9]{8}"/>
</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:
以下的样例给叫做"address"的元素定义了一个约束。空白符设为"preserve"(保留),这意味着XML处理器不会删除不论什么空白字符:
<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:
以下的样例也给叫做"address"的元素定义了一个约束。空白符设为" replace "(替代),这意味着XML处理器会用空格替代全部的空白字符(换行符, 制表符, 空格符, 回车符))
<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="replace"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
This example also defines an element called "address" 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, and multiple spaces are reduced to a single space):
以下的样例也给叫做"address"的元素定义了一个约束。空白符设为"collapse"(消除),这意味着XML处理器会清除全部的空白字符(换行符, 制表符, 空格符以及回车符都被空格符代替。头尾空格会被清除,多个空格也会降低为一个)
<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="collapse"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Restrictions on Length
对长度的约束
To limit the length of a value in an element, we would use the length, maxLength, and minLength constraints.
为了限制元素的长度值,我们会用length, maxLength, 和 minLength 约束。
This example defines an element called "password" with a restriction. The value must be exactly eight characters:
以下的样例给叫做"password"的元素定义了一个约束。值必须正好有8个字符:
<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
This example defines another element called "password" with a restriction. The value must be minimum five characters and maximum eight characters:
以下的样例给叫做"password"的元素定义了一个约束。值最少要有5个字符,最多有8个字符。
<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>
Restrictions for Datatypes
对数据类型的约束
Constraint
约束 Description
说明
enumeration Defines a list of acceptable values
定义了一系列的有效值
fractionDigits Specifies the maximum number of decimal places allowed. Must be equal to or greater than zero
指定了同意的小数位数的最多位数。必须大于等于0
length Specifies the exact number of characters or list items allowed. Must be equal to or greater than zero
指定了同意的字符或列表项的个数。必须大于等于0
maxExclusive Specifies the upper bounds for numeric values (the value must be less than this value)
指定了数值的上限(数值要比这个值小)
maxInclusive Specifies the upper bounds for numeric values (the value must be less than or equal to this value)
指定了数值上限(数值必须小于等于这个值)
maxLength Specifies the maximum number of characters or list items allowed. Must be equal to or greater than zero
指定了所同意的字符或列表项的最多个数。必须大于等于0
minExclusive Specifies the lower bounds for numeric values (the value must be greater than this value)
指定了数值的下限 (数值要比这个值小)
minInclusive Specifies the lower bounds for numeric values (the value must be greater than or equal to this value)
指定了数值的下限(数值必须大于等于这个值)
minLength Specifies the minimum number of characters or list items allowed. Must be equal to or greater than zero
指定了所同意的字符或列表的最少个数。必须等于大于0个
pattern Defines the exact sequence of characters that are acceptable
定义了符合要求的字符的确切排列顺序
totalDigits Specifies the exact number of digits allowed. Must be greater than zero
指定了所同意的字符的确切个数。必须大于0
whiteSpace Specifies how white space (line feeds, tabs, spaces, and carriage returns) is handled
指定了空白该怎样被处理(换行符,制表符,空格符和回车符)
XSD Complex 元素
w3pop.com / 2006-09-21
XSD Restrictions/Facets XSD Complex 空元素
A complex element contains other elements and/or attributes.
一个复合元素(Complex Elements)包括其它元素和/或属性
What is a Complex Element?
什么是复合元素(Complex Elements)?
A complex element is an XML element that contains other elements and/or attributes.
复合元素(Complex Elements)是含有其它元素和/或属性的XML元素
There are four kinds of complex elements:
有四种复合元素(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 Elements
复合元素(Complex Elements)的样例
A complex XML element, "product", which is empty:
一个空的复合XML元素"product":
<product p />
Note: If a URI has spaces, replace them with %20.
注意:假设URI包括空格,能够用“%20”替代它们。
Miscellaneous Data Types
混合数据类型
Name
名称 Description
描写叙述
anyURI
base64Binary
boolean
double
float
hexBinary
NOTATION
QName
Restrictions on Miscellaneous Data Types
对混合数据类型的约束
Restrictions that can be used with the other data types:
可用于约束的其它数据类型:
o enumeration (a Boolean data type cannot use this constraint)
列举(不能用此约束逻辑值数据类型)
o length (a Boolean data type cannot use this constraint)
长度(不能用此约束逻辑值数据类型)
o maxLength (a Boolean data type cannot use this constraint)
最大长度(不能用此约束逻辑值数据类型)
o minLength (a Boolean data type cannot use this constraint)
最短长度(不能用此约束逻辑值数据类型)
o pattern
式样
o whiteSpace
空白
XML Schema总结
w3pop.com / 2006-09-21
XSD 逻辑值数据类型 XML Schema 參考
XML Schema Summary
XML Schema总结
This tutorial has taught you how to describe the structure of an XML document.
这篇教程教你怎样描写叙述XML文档的结构
You have learned how to use an XML Schema is to define the legal elements of an XML document, just like a DTD. We think that very soon XML Schemas will be used in most Web applications as a replacement for DTDs.
你已经学习了怎样使用XML Schema(XML 公式),就是象DTD一样给XML文档定义合法元素。我们觉得非常快XML Schema(XML 公式)就会代替DTD而被大多数的网络应用程序所使用。
You have also learned that the XML Schema language is very rich. Unlike DTDs, it supports data types and namespaces.
你已经知道了 XML公式语言是非常丰富的。和DTD不同,它支持数据类型和名称空间。
For more information on XML Schema, please look at our XML Schema reference.
想很多其它地了解XML Schema 的信息,请看我们的XML Schema 參考。
Now You Know XML Schema, What's Next?
如今你知道了XML Schema,那接着学什么呢?
The next step is to learn about WSDL.
下步是学习有关WSDL.
WSDL is a schema-based language for describing Web services and how to access them.
WSDL 是描写叙述网络服务以及描写叙述怎样訪问它们的一种以schema为基础的语言
WSDL describes a web service, along with the message format and protocol details for the web service.
WSDL描写叙述网络服务,以及与网络服务有关的信息格式和协议细节。
If you want to learn more about WSDL, please visit our WSDL tutorial.
想学习很多其它与WSDL有关的内容,请浏览我们的WSDL 教程.
XML Schema 參考
w3pop.com / 2006-09-21
XML Schema总结
XSD Elements
XSD元素
Element
元素 Explanation
解释
all Specifies that the child elements can appear in any order. Each child element can occur 0 or 1 time
指明了子元素能够以随意顺序出现,每一个子元素出现0到1次
annotation Specifies the top-level element for schema comments
为schema凝视指定了最高阶层的元素
any Enables the author to extend the XML document with elements not specified by the schema
让编者能够用没有被schema指定的元素来扩展XML文件
anyAttribute Enables the author to extend the XML document with attributes not specified by the schema
让编者能够用没有被schema指定的属性来扩展XML文件
appInfo Specifies information to be used by the application (must go inside annotation)
指定了将被应用程序所用到的信息(必须在annotation里)
attribute Defines an attribute
定义属性
attributeGroup Defines an attribute group to be used in complex type definitions
定义了用于复合种类定义的属性组
choice Allows only one of the elements contained in the <choice> declaration to be present within the containing element
仅仅同意<choice>声明里的元素里的一个元素出如今所含的元素中。
complexContent Defines extensions or restrictions on a complex type that contains mixed content or elements only
在包括混合内容和元素的复合类型(complex type)上定义扩展或限制
complexType Defines a complex type element
定义了复合类型元素(complex type element)
documentation Defines text comments in a schema (must go inside annotation)
定义了schema里的文本凝视(必须在annotation里)
element Defines an element
定义元素
extension Extends an existing simpleType or complexType element
扩展一个现存的简单类型或复合类型元素
field Specifies an XPath expression that specifies the value used to define an identity constraint
指明一个XPath表达式,以指明用于定义身份约束的值
group Defines a group of elements to be used in complex type definitions
定义了将用于复合类型说明的一组元素
import Adds multiple schemas with different target namespace to a document
用不同的目标名称空间把多项schema加入到一份文件
include Adds multiple schemas with the same target namespace to a document
用同一目标名称空间把多项schema加入到文件
key Specifies an attribute or element value as a key (unique, non-nullable, and always present) within the containing element in an instance document
在实例文档包括的元素里指明某属性或元素值,使它们成为“钥匙”(特殊的,非零的,常常是存在的)
keyref Specifies that an attribute or element value correspond to those of the specified key or unique element
定义用于回应已指明关键或特殊元素的属性或元素值
list Defines a simple type element as a list of values
一张值的列表的形式定义一个简单类型元素
notation Describes the format of non-XML data within an XML document
描写叙述在XML文档里的非XML形式数据的格式
redefine Redefines simple and complex types, groups, and attribute groups from an external schema
从外部的schema中又一次定义简单和复合类型,群,以及属性群
restriction Defines restrictions on a simpleType, simpleContent, or a complexContent
在简单类型,简单文本或复合文本上定义约束
schema Defines the root element of a schema
定义schema的根元素
selector Specifies an XPath expression that selects a set of elements for an identity constraint
指明一段可为身份约束选择一组节点的XPath表达
sequence Specifies that the child elements must appear in a sequence. Each child element can occur from 0 to any number of times
指明了子元素必须以一定顺序出现。每一个子元素能够出现随意次
simpleContent Contains extensions or restrictions on a text-only complex type or on a simple type as content and contains no elements
包括着作为仅仅有文本的复合类型或简单类型的文本的扩展和约束,不包括元素
simpleType Defines a simple type and specifies the constraints and information about the values of attributes or text-only elements
定义了简单类型,指定了关于属性值或仅仅有文本的元素的信息
union Defines a simple type as a collection (union) of values from specified simple data types
从已指定的简单数据类型里定义简单种类,作为值的集合
unique Defines that an element or an attribute value must be unique within the scope
定义了在所属范围内一个元素或属性值必须是独特(没有反复的)
XSD Restrictions/Facets for Datatypes
XSD对各种数据类型的约束
Look at XSD Restrictions!
以下看一看关于XSD的约束
Constraint
约束 Description
描写叙述
enumeration Defines a list of acceptable values
定义了一系列可接受的属性的值
fractionDigits Specifies the maximum number of decimal places allowed. Must be equal to or greater than zero
指明了所同意的最多小数位数。必须等于大于0
length Specifies the exact number of characters or list items allowed. Must be equal to or greater than zero
指明了所同意的字符或列表项的确切个数。必须等于大于零
maxExclusive Specifies the upper bounds for numeric values (the value must be less than this value)
指明了最大数值(数值必须小于最大数值)
maxInclusive Specifies the upper bounds for numeric values (the value must be less than or equal to this value)
指明了数值上限(数值必须小于等于数值上限)
maxLength Specifies the maximum number of characters or list items allowed. Must be equal to or greater than zero
指明了所同意的字符或列表项的最多个数。必须大于等于0
minExclusive Specifies the lower bounds for numeric values (the value must be greater than this value)
指明了数值的下限(数值必须比这个值大)
minInclusive Specifies the lower bounds for numeric values (the value must be greater than or equal to this value)
指明了数值的下限(数值要大于等于这个值)
minLength Specifies the minimum number of characters or list items allowed. Must be equal to or greater than zero
指明了所同意的字符和列表项的最少个数。必须大于等于0个
pattern Defines the exact sequence of characters that are acceptable
定义了可接受字符的确切顺序
totalDigits Specifies the exact number of digits allowed. Must be greater than zero
定义了所同意的阿拉伯数字的确切个数。必须比0个多
whiteSpace Specifies how white space (line feeds, tabs, spaces, and carriage returns) is handled
定义了各种空白区域(white space) (换行符、制表符、空格、回车符)是怎样被处理的。
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=2215545
[收藏到我的网摘] [发送Trackback] MicroヾLi发表于 2008年03月25日 09:26:42
特别推荐:
想在这里投放广告?点击查看详情
关闭
* 免费体验张孝祥Java基础课程
夯实基础往前跑 1万元的线下课程如今几百元就能够得到 xml
* XML 2007 年度回想
例数XML精品文章 xml
* 主机全然DIY,域名免费试用
时代互联100M主机 216元/年 xml
* Ajax 和 XML
Ajax 和 XML: 五种 Ajax 反模式 xml
* Oracle XML 开发者工具包
XML开发者工具包下载 XML开发捷径,最新技术指导 xml
上一篇: ASP.NET 和 IIS 配置自己定义扩展名网页 | 下一篇: 关于clustered 和 noclustered 索引,以及查询优化【MS-SQL】
评论
没有评论。
发表评论
大名:
请输入尊姓大名
网址:
校验码: 检验码无效! 看不清,换一张
评论
请输入评论
当前用户设置仅仅有注冊用户才干发表评论。假设你没有登录,请点击登录
Powered by:
Copyright ? MicroヾLi