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