Your original模式应该声明sequence
在 - 的里面complexType
:
<xs:complexType name="Book">
<xs:sequence>
<xs:element name="Author" type="String32"
minOccurs="1" maxOccurs="1" />
<xs:element name="Title" type="String32"
minOccurs="1" maxOccurs="1" />
</xs:sequence>
</xs:complexType>
它还应该声明targetNamespace
等于默认值xmlns
命名空间,并包含属性elementFormDefault="qualified"
因此您可以在实例中使用不合格的元素。下面的架构(我添加了一个element
声明和simpleType
声明)验证您的原始 XML:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://bookshop.com"
targetNamespace="http://bookshop.com"
elementFormDefault="qualified">
<xs:complexType name="Book">
<xs:sequence>
<xs:element name="Author" type="String32"
minOccurs="1" maxOccurs="1" />
<xs:element name="Title" type="String32"
minOccurs="1" maxOccurs="1" />
</xs:sequence>
</xs:complexType>
<xs:element name="Book" type="Book"/>
<xs:simpleType name="String32">
<xs:restriction base="xs:string"></xs:restriction>
</xs:simpleType>
</xs:schema>
假设上面的架构是您的original架构,你可以redefine the Book
新模式中的复杂类型具有相同的目标命名空间作为您的原始架构。如果它需要包含一个Publisher
来自另一个命名空间的元素,您必须在第三个模式中声明它。在与原始模式具有相同目标命名空间的模式中,您可以重新定义Book
像这样输入(假设您的原始架构位于bookshop.xsd
:
<xs:redefine schemaLocation="bookshop.xsd">
<xs:complexType name="Book">
<xs:complexContent>
<xs:extension base="Book">
<xs:sequence>
<xs:element ref="cs:Publisher" minOccurs="1" maxOccurs="1" />
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:redefine>
为此,您必须导入架构,其中Publisher
元素被声明。假设它是在此模式中声明的,其中http://custombookshop.com
命名空间:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
xmlns="http://custombookshop.com"
xmlns:bs="http://bookshop.com"
targetNamespace="http://custombookshop.com">
<xs:import namespace="http://bookshop.com" schemaLocation="bookshop.xsd"/>
<xs:element name="Publisher" type="bs:String32" />
</xs:schema>
然后您可以将其导入到重新定义的架构中:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
xmlns="http://bookshop.com"
targetNamespace="http://bookshop.com"
xmlns:cs="http://custombookshop.com">
<xs:import schemaLocation="custombookshop.xsd" namespace="http://custombookshop.com"/>
<xs:redefine schemaLocation="bookshop.xsd">
...
</xs:redefine>
</xs:schema>
现在,您可以使用重新定义的架构来验证第二个 XML 文件。