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):
Note: Each of these elements may contain attributes as well!
注意:这些元素中的每一个也许还含有属性!
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> |
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> |
An empty complex element cannot have contents, only attributes.
一个空的复合元素不能含有内容,只能含有属性。
An empty XML element:
一个空的XML元素:
<product prodid="1345" /> |
The "product" element above has no content at all. To define a type with no content, we must define a type that allows only elements in its content, but we do not actually declare any elements, like this:
上述"product"元素完全不含内容。为定义不含内容的类型,我们必须定义一个内容中只允许出现元素的类型,但我们不需要声明任何元素,就像这样:
<xs:element name="product"> <xs:complexType> <xs:complexContent> <xs:restriction base="xs:integer"> <xs:attribute name="prodid" type="xs:positiveInteger"/> </xs:restriction> </xs:complexContent> </xs:complexType> </xs:element> |
In the example above, we define a complex type with a complex content. The complexContent element signals that we intend to restrict or extend the content model of a complex type, and the restriction of integer declares one attribute but does not introduce any element content.
上述例子中,我们定义了一个有复合内容的复合类型。复合内容的元素表示了我们想要约束或扩充的复合类型的内容模式。对整数的约束声明了一个属性,但并没有介绍任何元素内容。
However, it is possible to declare the "product" element more compactly, like this:
但是,可以更加简洁地声明"product"元素,就像这样:
<xs:element name="product"> <xs:complexType> <xs:attribute name="prodid" type="xs:positiveInteger"/> </xs:complexType> </xs:element> |
Or you can give the complexType element a name, and let the "product" element have a type attribute that refers to the name of the complexType (if you use this method, several elements can refer to the same complex type):
或者你可以给complexType元素起个名称,并让"product"元素有个类型属性,而且类型属性引用的是complexType的名称(如果你用这个方法,几个元素可以引用相同的复合类型):
<xs:element name="product" type="prodtype"/> <xs:complexType name="prodtype"> <xs:attribute name="prodid" type="xs:positiveInteger"/> </xs:complexType> |
An "elements-only" complex type contains an element that contains only other elements.
“只有元素(Elements-only)”复合类型含有一个只包含其他元素的元素
An XML element, "person", that contains only other elements:
一个XML元素, "person",只含有其他元素:
<person> <firstname>John</firstname> <lastname>Smith</lastname> </person> |
You can define the "person" element in a schema, like this:
你可以在一篇schema里定义"person"元素,就像这样
<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:complexType> </xs:element> |
Notice the <xs:sequence> tag. It means that the elements defined ("firstname" and "lastname") must appear in that order inside a "person" element.
注意<xs:sequence>标签。这表示所定义的元素("firstname" 和 "lastname")必须在"person"元素里以那样的次序出现。
Or you can give the complexType element a name, and let the "person" element have a type attribute that refers to the name of the complexType (if you use this method, several elements can refer to the same complex type):
或者你可以给complexType元素取个名字,让"person"元素有个类型属性,这个类型属性的名字可以参考使用complexType元素的名字(如果你用这个方法,几个元素可以同时参考使用相同的复合类型)。
<xs:element name="person" type="persontype"/> <xs:complexType name="persontype"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> </xs:complexType> |
A complex text-only element can contain text and attributes.
一个复合只含文本元素(Complex Text-Only Elements)可以含有文本和属性。
This type contains only simple content (text and attributes), therefore we add a simpleContent element around the content. When using simple content, you must define an extension OR a restriction within the simpleContent element, like this:
这种类型只含有简单内容(文本和属性),因此我们在内容周围添加一个simpleContent元素,当用到简单内容时,你必须在simpleContent元素里定义一个扩展或约束,就像这样:
<xs:element name="somename"> <xs:complexType> <xs:simpleContent> <xs:extension base="basetype"> .... .... </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> OR <xs:element name="somename"> <xs:complexType> <xs:simpleContent> <xs:restriction base="basetype"> .... .... </xs:restriction> </xs:simpleContent> </xs:complexType> </xs:element> |
Tip: Use the extension/restriction element to expand or to limit the base simple type for the element.
提示:用extension/restriction元素扩展或限制元素的基本简单类型(base simple type)。
Here is an example of an XML element, "shoesize", that contains text-only:
这儿是只含有文本的一个XML元素, "shoesize":
<shoesize country="france">35</shoesize> |
The following example declares a complexType, "shoesize". The content is defined as an integer value, and the "shoesize" element also contains an attribute named "country":
下面的例子声明了一个复合类型,"shoesize"元素。内容定义为整数值,"shoesize"元件含有名为"country"的属性。
<xs:element name="shoesize"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:integer"> <xs:attribute name="country" type="xs:string" /> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> |
We could also give the complexType element a name, and let the "shoesize" element have a type attribute that refers to the name of the complexType (if you use this method, several elements can refer to the same complex type):
我们可以给complexType元件起个名字,让"shoesize"元件有种类属性,种类属性的名字就是complexType元件的名字。(如果你用这种方法,几个元素指的可以是相同的复合类型)
<xs:element name="shoesize" type="shoetype"/> <xs:complexType name="shoetype"> <xs:simpleContent> <xs:extension base="xs:integer"> <xs:attribute name="country" type="xs:string" /> </xs:extension> </xs:simpleContent> </xs:complexType> |
A mixed complex type element can contain attributes, elements, and text.
混合内容的复合类型元素(XSD Complex Types Element With Mixed Content )可以含有属性,元素,和文本。
An XML element, "letter", that contains both text and other elements:
一个XML元素,"letter",既含有文本又含有其他元素:
<letter> Dear Mr.<name>John Smith</name>. Your order <orderid>1032</orderid> will be shipped on <shipdate>2001-07-13</shipdate>. </letter> |
The following schema declares the "letter" element:
下面的XML公式声明了"letter"元素:
<xs:element name="letter"> <xs:complexType 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> </xs:element> |
Note: To enable character data to appear between the child-elements of "letter", the mixed attribute must be set to "true". The <xs:sequence> tag means that the elements defined (name, orderid and shipdate) must appear in that order inside a "letter" element.
注意:为了使字符数据能出现在"letter"子元件之间,mixed属性必须设置为"true"。<xs:sequence>标签指出了已定义的元素(name, orderid 和shipdate)在"letter"元素里必须以指定的顺序出现
We could also give the complexType element a name, and let the "letter" element have a type attribute that refers to the name of the complexType (if you use this method, several elements can refer to the same complex type):
我们可以给这个complexType元素一个名称,并且让"letter"元素有一个引用了complexType的名称的种类属性(如果你用了这个方法,几个元素可以同时使用相同的复合类型):
<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> |