<!--schema复合元素-->
案例1
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.youkuaiyun.com"
elementFormDefault="qualified">
<xs:element name="books">
<!-- 复合元素 -->
<xs:complexType>
<xs:sequence>
<!-- 子元素 -->
<xs:element name="book">
<xs:complexType>
<!-- 定义了一个属性 -->
<xs:attribute name="isbn" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="defaultDemo" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
被约束文件
<?xml version="1.0" encoding="UTF-8"?>
<books xmlns="http://www.youkuaiyun.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.youkuaiyun.com empl.xsd"
defaultDemo="yy">
<book isbn="00xx1"></book>
</books>
案例2
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/fh"
elementFormDefault="qualified">
<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>
</xs:schema>
被约束文件
<?xml version="1.0" encoding="UTF-8"?>
<employee xmlns="http://www.example.org/fh"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.org/fh fh.xsd">
<firstname>John</firstname>
<lastname>Smith</lastname>
</employee>
案例3<定义元素扩展类型>
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/fh" xmlns="http://www.example.org/fh"
elementFormDefault="qualified">
<xs:element name="users">
<xs:complexType>
<xs:sequence>
<!-- 定义元素 -->
<xs:element name="employee" type="nameType" />
<!-- 定义元素 -->
<xs:element name="student" type="nameType" />
<!-- 定义元素 -->
<xs:element name="teacher" type="teacherType" />
</xs:sequence>
</xs:complexType>
</xs:element>
<!-- 自定义类型 -->
<xs:complexType name="nameType">
<xs:sequence>
<xs:element name="firstname" type="xs:string" />
<xs:element name="lastname" type="xs:string" />
</xs:sequence>
</xs:complexType>
<!-- 自定义类型 -->
<xs:complexType name="teacherType">
<xs:complexContent>
<!-- 扩展类型 -->
<xs:extension base="nameType">
<xs:sequence>
<xs:element name="address" type="xs:string" />
<xs:element name="age" type="xs:integer" />
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>
被约束文件
<?xml version="1.0" encoding="UTF-8"?>
<users xmlns="http://www.example.org/fh" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.org/fh fh1.xsd">
<employee>
<firstname>John</firstname>
<lastname>Smith</lastname>
</employee>
<student>
<firstname>John</firstname>
<lastname>Smith</lastname>
</student>
<teacher>
<firstname>John</firstname>
<lastname>Smith</lastname>
<address>河北石家庄</address>
<age>29</age>
</teacher>
</users>