您的位置:首页 > 其它

XML学习之Xml Schema:十一、引入其他的模式文档

2013-01-30 19:26 555 查看
在编写文档时,需要导入其他的模式文档,XML Schema提供了3种机制,方便我们导入。分别是包含(include)、重定义(redefine)、导入(import)。

包含Include

要包含一个模式文档,可以使用xs:include元素,他有个一个必须的属性schemaLocation,用于指出要包含的模式文档的位置。xs:include元素要作为xs':schema元素的子元素使用,且必须出现在模式文档的开始处。(之前只能出现xs:redefine、xs:import)。

使用包含一个很重要的条件就是:被包含组件的命名空间必须和包含方的目标名称空间一样。被包含的模式文档也可以没有命名空间,这样的话,将使用包含方的目标命名空间。如果包含方也没有目标名称空间,那么就和没有使用名称空间一样的。

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.jwt.com/Employee" targetNamespace="http://www.jwt.com/Employee">
<xs:complexType name="addressType">
<xs:sequence>
<xs:element name="city" type="xs:token"/>
<xs:element name="street" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>

XS_Include_AddressType.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.jwt.com/Employee" xmlns="http://www.jwt.com/Employee">
<xs:include schemaLocation="XS_Include_AddressType.xsd"/>
<xs:element name="employee" type="employeeType"/>
<xs:complexType name="employeeType">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="homeAddr" type="addressType"/>
</xs:sequence>
</xs:complexType>
</xs:schema>

XS_Include_Employee.xml
重定义redefine

重定义与包含类似,都可以用于引入其他的模式,且都要求被引入的模式文档与包含文档具有相同的名称空间(或者没有目标名称空间),不同的是,重定义可以对引入的模式文档中的组件进行重新定义。要注意的是:1、只有某些特定的类型的模式组件可以被重定义,他们是:简单类型、复杂类型、元素组、属性组。2、是有需要修改的组件才应该出现在xs:redefine元素中,其他的组件将被之间包含在新的模式中。3、xs:redefine元素并不要求有任何的子元素,这样的情况下,他仅仅起到xs:include元素的作用。

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.jwt.com/Employee" xmlns="http://www.jwt.com/Employee">
<xs:redefine schemaLocation="XS_Include_AddressType.xsd">
<xs:complexType name="addressType">
<xs:complexContent>
<xs:extension base="addressType">
<xs:sequence>
<xs:element name="zipCode" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:redefine>

<xs:element name="employee" type="employeeType"/>
<xs:complexType name="employeeType">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="homeAddr" type="addressType"/>
</xs:sequence>
</xs:complexType>
</xs:schema>


导入Import

导入用于在一个模式文档中引入其他名称空间中的组件。导入使用xs:import元素来表示,有两个属性,namespace和schemaLocation。namespace属性指定要导入的名称空间的URI,shemaLocation属性是必需的,用于指出模式文档的位置。同样的,xs:import元素只能作为xs:schema的子元素使用,且必须出现在模式文档的开始处。

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.jwt.com/Address" targetNamespace="http://www.jwt.com/Address">
<xs:complexType name="addressType">
<xs:sequence>
<xs:element name="city" type="xs:token"/>
<xs:element name="street" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.jwt.com/Emp" xmlns:emp="http://www.jwt.com/Emp"
xmlns:addr="http://www.jwt.com/Address">
<xs:import namespace="http://www.jwt.com/Address" schemaLocation="address.xsd" />
<xs:element name="employee" type="empType"/>
<xs:complexType name="empType">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="homeAddr" type="addr:addressType"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: