覆盖父xsd中元素的minoccurs



是否可以覆盖父xsd中元素的minOccurs/maxOccurs属性?我们可以很容易地扩展父xsd并创建一个具有不同名称空间的新xsd,但我正在尝试使用相同的父名称空间进行覆盖。

假设我们有一个xsd

    <?xml version="1.0" encoding="UTF-8"?>
<xs:schema
    xmlns:cust="http://test.com/schema/cust" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://test.com/schema/cust"  elementFormDefault="qualified"  attributeFormDefault="unqualified">
<xs:complexType name="customer-type">
  <xs:sequence>
    <xs:element name="firstname" type="xs:string"/>
    <xs:element name="lastname" type="xs:string"/>
    <xs:element name="hobby" type="xs:string" minOccurs="0" maxOccurs="unbounded" />
  </xs:sequence>
</xs:complexType>
<xs:element name="customer" type="cust:customer-type"/>
</xs:schema>

我可以用相同的命名空间创建上述xsd的扩展/限制吗?这将限制/更改<cust:hobby> minOccurs为1?

好吧,我把解决方案复杂化了。在此之前,我尝试在xml 1.0中使用override函数,该函数只能从xml 1.1中获得。

一个简单的重新定义就可以了:

如果基础xsd的名称是cust.xd,那么下面的重新定义将"hobby"元素的minOccurs重写为1。

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:cust="http://test.com/schema/cust" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://test.com/schema/cust"  elementFormDefault="qualified"  attributeFormDefault="unqualified">
<xs:redefine schemaLocation="cust.xsd">
<xs:complexType name="customer-type">
<xs:complexContent>
 <xs:restriction base="cust:customer-type">
  <xs:sequence>
    <xs:element name="firstname" type="xs:string"/>
    <xs:element name="lastname" type="xs:string"/>
    <xs:element name="hobby" type="xs:string" minOccurs="1" maxOccurs="unbounded" />
  </xs:sequence>
 </xs:restriction>
</xs:complexContent>
</xs:complexType>
</xs:redefine>
</xs:schema>

最新更新