以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 XML基础 』  (http://bbs.xml.org.cn/list.asp?boardid=1)
----  schema教程  (http://bbs.xml.org.cn/dispbbs.asp?boardid=1&rootid=&id=46609)


--  作者:grass00
--  发布时间:5/9/2007 10:43:00 AM

--  schema教程
XSD Simple 元素

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.
但是,“只含文本”这个限定条件是非常容易引起误解的,文本可以有很多的不同类型。它可以是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:
<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 属性

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
   
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 元素

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 pid="1345"/>
A complex XML element, "employee", which contains only other elements:
只含有其他元素的复合XML元素, "employee"
<employee>
<firstname>John</firstname>
<lastname>Smith</lastname>
</employee>
A complex XML element, "food", which contains only text:
只含有文本的复合XML元素, "food":
<food type="dessert">Ice cream</food>
A complex XML element, "description", which contains both elements and text:
含有元素和文本的复合XML元素, "description":
<description>

It happened on <date lang="norwegian">03.03.99</date> ....
</description>
________________________________________
How to Define a Complex Element
怎样定义一个复合元素(Complex Elements)?
Look at this complex XML element, "employee", which contains only other elements:
看这个只含有其他元素的复合XML元素,"employee":
<employee>
<firstname>John</firstname>
<lastname>Smith</lastname>

</employee>
We can define a complex element in an XML Schema two different ways:
我们有两种方法可以在一篇XML Schema里定义一个复合元素(Complex Elements):
1. The "employee" element can be declared directly by naming the element, like this:
1. "employee"元素可以直接通过命名元素的方式被声明,像这样:
<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>
If you use the method described above, only the "employee" element can use the specified complex type. Note that the child elements, "firstname" and "lastname", are surrounded by the <sequence> indicator. This means that the child elements must appear in the same order as they are declared. You will learn more about indicators in the XSD Indicators chapter.
如果你用了上面的方法,那么只有"employee"元素才可以用指定的复合类型。注意子元素"firstname" 和 "lastname",它们是被包围在<sequence>“指示器”元素里的。这意味着子元素必须以它们被声明的顺序出现。在XSD指示器这章里你可以学到关于指示器更多内容。
2. The "employee" element can have a type attribute that refers to the name of the complex type to use:
2. "employee"元素可以有个类型属性,其所指的是要用的复合类型的名称
<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>
If you use the method described above, several elements can refer to the same complex type, like this:
如果你用上述方法,几个元素指的可以是相同的复合类型,就像这样:
<xs:element name="employee" type="personinfo"/>
<xs:element name="student" type="personinfo"/>
<xs:element name="member" 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>
You can also base a complex element on an existing complex element and add some elements, like this:
你也可以在现存的复合元素(Complex Elements)上再加上一个复合元素(Complex Elements),并添加一些元素,就像这样:
<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>



--  作者:hgpimac
--  发布时间:5/9/2007 1:11:00 PM

--  
看看
--  作者:unix229
--  发布时间:5/15/2007 4:56:00 PM

--  
研究一下。。。
--  作者:gqwen1989
--  发布时间:3/27/2008 8:54:00 AM

--  
好东东~~
得好好看看~
W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
4,812.500ms