当前位置: 首页 > 网络学院 > XML相关教程 > Schema (XSD) > XSD 复合元素
A complex element contains other elements and/or attributes.
一个复合元素包含其它元素和 / 或属性。
A complex element is an XML element that contains other elements and/or attributes.
复合元素是包含其它元素和 / 或属性的XML元素:
There are four kinds of 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> |
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> |
Look at this complex XML element, "employee", which contains only other elements:
先请看看下面这个只包含其它元素的复合XML元素"employee":
<employee> |
We can define a complex element in an XML Schema two different ways:
我们可以使用两种不同的方法在一份XML Schema文档里定义一个复合元素:
1. The "employee" element can be declared directly by naming the element, like this:
1. "employee"元素可以直接通过为元素命名的方式被声明,如下所示:
<xs:element name="employee"> |
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"> |
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:complexType name="personinfo"> |
You can also base a complex element on an existing complex element and add some elements, like this:
你也可以在现有的复合元素的基础上再加上一个复合元素,并添加一些其它元素,如下所示:
<xs:element name="employee" type="fullpersoninfo"/> <xs:complexType name="personinfo"> <xs:complexType name="fullpersoninfo"> |