当前位置: 首页 > 网络学院 > XML相关教程 > Schema (XSD) > XSD 复合元素

Schema (XSD)
XSD 逻辑值数据类型
XML Schema总结
XML Schema 参考
XSD 验证

Schema (XSD) 中的 XSD 复合元素


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 836 ::
收藏到网摘: n/a

A complex element contains other elements and/or attributes.
一个复合元素包含其它元素和 / 或属性。


What is a Complex Element?
什么是复合元素?

A complex element is an XML element that contains other elements and/or attributes.
复合元素是包含其它元素和 / 或属性的XML元素:

There are four kinds of 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
复合元素案例

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
复合元素的定义方法

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文档里定义一个复合元素:

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:
你也可以在现有的复合元素的基础上再加上一个复合元素,并添加一些其它元素,如下所示:

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

评论 (0) All

登陆 | 还没注册?