XML Schema学习教程(二)-元素属性的定义与约束

来源:互联网 发布:apache spark 更新数据 编辑:程序博客网 时间:2024/06/07 03:18

XSD Simple Elements
[XSD
的简单元素]

翻译:linqingfeng
英语原文: http://www.w3schools.com/schema/default.asp


XML Schemas define the elements of your XML files.
[XML Schemas
定义XML文件中的元素.]

A simple element is an XML element that can contain 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 that are included in the XML Schema definition (boolean, string, date, etc.), or it can be a custom type that you can define yourself.
[
然而,”仅能包含文本这个约束非常令人误解.文本是有很多种不同的形式的.它能是XML Schema定义元素(boolean, string, date等等)中的一种,或者是你自定义类型.]

You can also add restrictions (facets) to a data type in order to limit its content, and you can require the data to match a defined pattern.
[
你能够给数据类型加上约束(facets)来限定它的内容,而且你还能要求数据匹配你定义的模式.]


How to Define 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是元素的数据类型.]

Here are some XML elements:
[
这里有一些XML的元素:]

<lastname>Refsnes</lastname>

<age>34</age>

<dateborn>1968-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"/>

 


Common XML Schema Data Types
[
普通的XML Schema数据类型]

XML Schema has a lot of built-in data types. Here is a list of the most common types:
[XML Schema
中有很多固定的数据类型.这里列举了一些普通的类型:]

  • xs:string
  • xs:decimal
  • xs:integer
  • xs:boolean
  • xs:date
  • xs:time

Declare Default and Fixed Values for Simple Elements
[
元素的默认值和固定值的声明]

Simple elements can have a default value OR a fixed value set.
[
每个元素都能设置默认值或者固定值.]

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. 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 Attributes
[XSD
属性]


All attributes are declared as simple types.
[
所有的属性都被声明为简单的类型.]

Only complex elements can have attributes!
[
只有复合的元素能有属性!!]


What is an Attribute?
[
什么是属性?]

Simple elements cannot have attributes. If an element has attributes, it is considered to be of complex type. But the attribute itself is always declared as a simple type. This means that an element with attributes always has a complex type definition.
[
简单的元素不能有属性。如果一个元素有属性,那么他就是一个复合的元素.但属性本身总是声明为简单的类型.意思就是说有属性的元素总是被定义为复合的类型.]


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 is the data type of the attribute.
[
这里的xxx是属性的名字,而yyy是属性的数据类型.]

Here is an XML element with an attribute:
[
下面是一个XML元素和它的属性:]

<lastname lang="EN">Smith</lastname>

And here is a corresponding simple attribute definition:
[
而这里是和它相对应的属性定义:]

<xs:attribute name="lang" type="xs:string"/>

 


Common XML Schema Data Types
[
普通XML Schema的数据类型]

XML Schema has a lot of built-in data types. Here is a list of the most common types:
[XML Schema
有很多固定的数据类型,这里列举其中一些:]

  • xs:string
  • xs:decimal
  • xs:integer
  • xs:boolean
  • xs:date
  • xs:time

Declare Default and Fixed Values for Attributes
[
声明属性的默认值和固定值]

Attributes can 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. You cannot specify another value. In the following example the fixed value is "EN":
[
固定的值会自动指派给属性,而你是不能给他指派其他值的. 例如下面的例子中固定值是"EN"]

<xs:attribute name="lang" type="xs:string" fixed="EN"/>

 


Creating Optional and Required Attributes
[
创建可选和必需的属性]

All attributes are optional by default. To explicitly specify that the attribute is optional, use the "use" attribute:
[
所与属性默认都是可选的.如果要明确指派属性是可选的,要定义属性"use"]

<xs:attribute name="lang" type="xs:string" use="optional"/>

To make an attribute required:
[
指定属性为必需的:]

<xs:attribute name="lang" type="xs:string" use="required"/>

 


Restrictions on Content
[
内容上的约束]

When an XML element or attribute has a type defined, it puts a restriction on the element's or attribute's content. If an XML element is of type "xs:date" and contains a string like "Hello Mother", the element will not validate.
[
XML的元素或者属性定义了类型,那么约束其输入的内容。如果XML元素类型被定义为"xs:date",并付给他一个字符串"Hello Mother",那么这个元素是不能通过校验的.]

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 Schemas,你同样能够给你的XML元素和属性加上约束.这些约束叫作facets.你能够在下一章了解到更多关于facets的知识.]

 

XSD Restrictions/Facets
[XSD
的约束/层面]


Restrictions are used to control acceptable values for XML elements or attributes. Restrictions on XML elements are called facets.
[
约束是用于控制XML元素和属性输入有效的值. 这些XML元素的约束叫作facets]


Restrictions on Values
[
数值的约束]

This example defines an element called "age" with a restriction. The value of age cannot be lower than 0 or greater than 100:
[
下面的例子对元素"age"定义了一个约束.使他的值不能低过0和高过100]

<xs:element name="age">

<xs:simpleType>

  <xs:restriction base="xs:integer">

    <xs:minInclusive value="0"/>

    <xs:maxInclusive value="100"/>

  </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元素的输入指定的几个值,我们会用到列举约束.]

This example defines an element called "car":
[
例如定义一个元素叫"car"]

<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 "car" element is a simple type with a restriction. The acceptable values are: Audi, Golf, BMW.
["car"
这个元素有一个简单的约束,就是限定输入的值只能为:Audi, Golf, BMW.]

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元素的输入连续的数字或则连续的字母,我们会用到模式约束.]

This example defines an element called "letter":
[
例如定义一个叫"letter"的元素:]

<xs:element name="letter">

<xs:simpleType>

  <xs:restriction base="xs:string">

    <xs:pattern value="[a-z]"/>

  </xs:restriction>

</xs:simpleType>

</xs:element>

The "letter" element is a simple type with a restriction. The only acceptable value is ONE of the LOWERCASE letters from a to z.
["letter"
是一个有简单约束的元素.只允许他输入的值为一个小写字母(az的任何一个).]

The next example defines an element called "initials":
[
下面的例子定义了一个叫"initials"的元素:]

<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 "initials" element is a simple type with a restriction. The only acceptable value is THREE of the UPPERCASE letters from a to z.
["initials"
这个元素有一个简单的约束.只允许他输入3个大写字母(范围从AZ).]

This example also defines an element called "initials":
[
这个例子也定义了一个"initials"元素:]

<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 "initials" element is a simple type with a restriction. The only acceptable value is THREE of the LOWERCASE OR UPPERCASE letters from a to z.
["initials"
是以一个有简单约束的元素. 只允许他输入3个大写或小写的字母(范围从AZ或者az).]

This example defines an element called "choice":
[
下面的例子定义了一个叫"choice"的元素:]

<xs:element name="choice">

<xs:simpleType>

  <xs:restriction base="xs:string">

    <xs:pattern value="[xyz]"/>

  </xs:restriction>

</xs:simpleType>

</xs:element>

The "choice" element is a simple type with a restriction. The only acceptable value is ONE of the following letters: x, y, OR z.
["choice"
元素的约束是只允许输入一个字母,是x,y,z中的任一个.]

The next example defines an element called "prodid":
[
下个例子定义了一个元素"prodid"]

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

The "prodid" element is a simple type 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"
元素的约束是只允许输入五个数字,范围从09.]


Other Restrictions on a Series of Values
[
其他连续值得约束]

Some other restrictions that can be defined by the pattern constraint:
[
一些其他的约束能定义为模式约束:]

This example defines an element called "letter":
[
下面的例子定义了一个"letter"的元素:]

<xs:element name="letter">

<xs:simpleType>

  <xs:restriction base="xs:string">

    <xs:pattern value="([a-z])*"/>

  </xs:restriction>

</xs:simpleType>

</xs:element>

The "letter" element is a simple type with a restriction. The acceptable value is zero or more occurrences of lowercase letters from a to z.
["letter"
元素的约束是只允许输入0个或者多个小写字母(az).]

This example also defines an element called "letter":
[
下面的例子定义了一个元素"letter"]

<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 "letter" element is a simple type 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"都是不行的.]

This example defines an element called "gender":
[
下面的例子定义了一个"gender"的元素:]

<xs:element name="gender">

<xs:simpleType>

  <xs:restriction base="xs:string">

    <xs:pattern value="male|female"/>

  </xs:restriction>

</xs:simpleType>

</xs:element>

The "gender" element is a simple type with a restriction. The only acceptable value is male OR female.
["gender"
元素定义的约束是只允许输入male或者female.]

This example defines an element called "password":
[
下面的例子定义一个元素"password"]

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

The "password" element is a simple type 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个字符,字符的范围是字母或者数字.]


Restrictions on White Space Characters
[
空白字符的约束]

To specify how white space characters should be handled, we would use the whiteSpace constraint.
[
为了能够输入空白字符,我们将用到空白字符约束.]

This example defines an element called "address":
[
下面的例子定义了一个元素"address"]

<xs:element name="address">

<xs:simpleType>

  <xs:restriction base="xs:string">

    <xs:whiteSpace value="preserve"/>

  </xs:restriction>

</xs:simpleType>

</xs:element>

The "address" element is a simple type 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处理程序将不会移除任何一个空白字符.]

This example also defines an element called "address":
[
下面的例子定义了一个叫"address"的元素:]

<xs:element name="address">

<xs:simpleType>

  <xs:restriction base="xs:string">

    <xs:whiteSpace value="replace"/>

  </xs:restriction>

</xs:simpleType>

</xs:element>

This "address" element is a simple type 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处理程序会用空格取代所有的空白字符(换行符,退格符,空格和回车).]

This example also defines an element called "address":
[
这个例子也定义了一个叫"address"的元素:]

<xs:element name="address">

<xs:simpleType>

  <xs:restriction base="xs:string">

    <xs:whiteSpace value="collapse"/>

  </xs:restriction>

</xs:simpleType>

</xs:element>

This "address" element is a simple type 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, multiple spaces are reduced to a single space).
["address"
元素的空白字符约束定义为"collapse",意味着XML处理程序将会移除所有的空白字符(意思是换行符,退格符,空格,回车将会由空格代替掉,而前面和后面的空白将会削掉,中间出现的多个连续的空白字符也只用一个空格代替).]


Restrictions on Length
[
长度的约束]

To limit the length of a value in an element, we would use the length, maxLength, and minLength constraints.
[
为了限制元素输入的长度,我们将会使用长度,最大长度和最小长度的约束.]

This example defines an element called "password":
[
下面的例子定义了一个"password"的元素:]

<xs:element name="password">

<xs:simpleType>

  <xs:restriction base="xs:string">

    <xs:length value="8"/>

  </xs:restriction>

</xs:simpleType>

</xs:element>

The "password" element is a simple type with a restriction. The value must be exactly eight characters.
["password"
的元素加入了长度的约束,只允许输入8个字符.]

This example defines another element called "password":
[
这个例子定义了另一个元素"password"]

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

This "password" element is a simple type with a restriction. The value must be minimum five characters and maximum eight characters.
["password"
元素加入了最大长度和最小长度的约束,只允许58个字符的输入.]


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
[
指定空白字符的处理(包括换行符,退格符,空格和回车)]