在使用新属性扩展时限制来自父级的属性
Restrict attribute from parent while extending with new attributes
我有复杂类型 History
,它应该扩展我的复杂类型 Section
,同时禁止一个继承属性 (Title
)。我怎样才能做到这一点?
例子
<xs:complexType name="Section">
<xs:sequence minOccurs="1" maxOccurs="1">
<xs:choice minOccurs="1" maxOccurs="unbounded">
<!-- ... -->
</xs:choice>
<xs:element name="Section" type="Section" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="Key" type="xs:string"/>
<xs:attribute name="Title" type="xs:string"/>
</xs:complexType>
<xs:complexType name="History">
<xs:complexContent>
<xs:extension base="Section">
<!-- Prohibit/remove "Title" attribute from parent. -->
<xs:attribute name="Title" use="prohibited"/>
<!-- Add more attributes. -->
<xs:attribute name="StartDate" type="xs:date" use="required"/>
<xs:attribute name="EndDate" type="xs:date"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
执行此操作的正确方法是什么?
已编辑:答案完全从头开始写,因为第一个不正确(见评论)。
我知道两种方法:
选项1:同时使用xs:restriction(为了禁止属性)和xs:extension(在为了添加更多内容)
<xs:group name="baseGroup">
<xs:sequence>
<xs:choice minOccurs="1" maxOccurs="unbounded">
<!-- ... -->
</xs:choice>
<xs:element name="Section" type="Section" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:group>
<xs:complexType name="Section">
<xs:group ref="baseGroup"></xs:group>
<xs:attribute name="Key" type="xs:string"/>
<xs:attribute name="Title" type="xs:string"/>
</xs:complexType>
<xs:complexType name="HistoryWithoutTitle">
<xs:complexContent>
<xs:restriction base="Section">
<!-- Inherit the group (only attributes are inherited) -->
<xs:group ref="baseGroup"></xs:group>
<!-- Prohibit/remove "Title" attribute from parent. -->
<xs:attribute name="Title" type="xs:string" use="prohibited"/>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="History">
<xs:complexContent>
<xs:extension base="HistoryWithoutTitle">
<!-- Add more attributes. -->
<xs:attribute name="StartDate" type="xs:date" use="required"/>
<xs:attribute name="EndDate" type="xs:date"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
方案2:使用具有共同内容和属性的"interface",然后制作两种类型(Section and History)从该界面扩展并添加新内容。这是我更喜欢的选项,因为您可以轻松地向两个元素或仅向一个元素添加新属性。
<!-- This is what both Section and History have in common (similar to an interface) -->
<xs:complexType name="common">
<!-- Content in both Section and History -->
<xs:sequence>
<xs:choice minOccurs="1" maxOccurs="unbounded">
<!-- ... -->
</xs:choice>
<xs:element name="Section" type="Section" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<!-- Attributes in both Section and History -->
<xs:attribute name="Key" type="xs:string"/>
</xs:complexType>
<xs:complexType name="Section">
<xs:complexContent>
<xs:extension base="common">
<!-- New attributes -->
<xs:attribute name="Title" type="xs:string"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="History">
<xs:complexContent>
<xs:extension base="common">
<!-- New attributes -->
<xs:attribute name="StartDate" type="xs:date" use="required"/>
<xs:attribute name="EndDate" type="xs:date"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
我有复杂类型 History
,它应该扩展我的复杂类型 Section
,同时禁止一个继承属性 (Title
)。我怎样才能做到这一点?
例子
<xs:complexType name="Section">
<xs:sequence minOccurs="1" maxOccurs="1">
<xs:choice minOccurs="1" maxOccurs="unbounded">
<!-- ... -->
</xs:choice>
<xs:element name="Section" type="Section" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="Key" type="xs:string"/>
<xs:attribute name="Title" type="xs:string"/>
</xs:complexType>
<xs:complexType name="History">
<xs:complexContent>
<xs:extension base="Section">
<!-- Prohibit/remove "Title" attribute from parent. -->
<xs:attribute name="Title" use="prohibited"/>
<!-- Add more attributes. -->
<xs:attribute name="StartDate" type="xs:date" use="required"/>
<xs:attribute name="EndDate" type="xs:date"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
执行此操作的正确方法是什么?
已编辑:答案完全从头开始写,因为第一个不正确(见评论)。
我知道两种方法:
选项1:同时使用xs:restriction(为了禁止属性)和xs:extension(在为了添加更多内容)
<xs:group name="baseGroup">
<xs:sequence>
<xs:choice minOccurs="1" maxOccurs="unbounded">
<!-- ... -->
</xs:choice>
<xs:element name="Section" type="Section" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:group>
<xs:complexType name="Section">
<xs:group ref="baseGroup"></xs:group>
<xs:attribute name="Key" type="xs:string"/>
<xs:attribute name="Title" type="xs:string"/>
</xs:complexType>
<xs:complexType name="HistoryWithoutTitle">
<xs:complexContent>
<xs:restriction base="Section">
<!-- Inherit the group (only attributes are inherited) -->
<xs:group ref="baseGroup"></xs:group>
<!-- Prohibit/remove "Title" attribute from parent. -->
<xs:attribute name="Title" type="xs:string" use="prohibited"/>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="History">
<xs:complexContent>
<xs:extension base="HistoryWithoutTitle">
<!-- Add more attributes. -->
<xs:attribute name="StartDate" type="xs:date" use="required"/>
<xs:attribute name="EndDate" type="xs:date"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
方案2:使用具有共同内容和属性的"interface",然后制作两种类型(Section and History)从该界面扩展并添加新内容。这是我更喜欢的选项,因为您可以轻松地向两个元素或仅向一个元素添加新属性。
<!-- This is what both Section and History have in common (similar to an interface) -->
<xs:complexType name="common">
<!-- Content in both Section and History -->
<xs:sequence>
<xs:choice minOccurs="1" maxOccurs="unbounded">
<!-- ... -->
</xs:choice>
<xs:element name="Section" type="Section" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<!-- Attributes in both Section and History -->
<xs:attribute name="Key" type="xs:string"/>
</xs:complexType>
<xs:complexType name="Section">
<xs:complexContent>
<xs:extension base="common">
<!-- New attributes -->
<xs:attribute name="Title" type="xs:string"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="History">
<xs:complexContent>
<xs:extension base="common">
<!-- New attributes -->
<xs:attribute name="StartDate" type="xs:date" use="required"/>
<xs:attribute name="EndDate" type="xs:date"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>