2014-12-07 93 views
0

我已阅读How to extend a choice complexType without sequencing the choice?XML Schema: Extend xsd:choice so that union (not sequence) of choices is created。我的问题是相关的,但稍有不同。我有许多不同的complexTypes的XML模式,所有看起来像这样:在XSD中扩展选项

<xs:complexType name="Type_NNN"> 
    <xs:choice> 
     <xs:element name="indiv_NNN" type="Indiv_NNN" minOccurs="0"/> 
     <xs:element name="common-element" type="CommmonElement" minOccurs="0"/> 
    </xs:choice> 
    <xs:attribute name="commonAtt-001" type="xs:string" use="required"/> 
    <xs:attribute name="commonAtt-002" type="xs:string" use="optional"/> 
    <xs:attribute name="commonAtt-003" type="xs:string" use="optional"/> 
    <xs:attribute name="commonAtt-004" type="xs:string" use="optional"/> 
    <xs:attribute name="commonAtt-005" type="xs:string" use="optional"/> 
    <xs:attribute name="commonAtt-006" type="xs:string" use="optional"/> 
    <xs:attribute name="commonAtt-007" type="xs:nonNegativeInteger" 
        use="optional" default="0"/> 
    <xs:attribute name="commonAtt-008" type="xs:string" use="optional"/> 
    <xs:attribute name="indivAtt-NNN" type="xs:string" use="optional"/> 
</xs:complexType> 

也就是说,大部分内容是共同的,除了项目标记为“NNN”。有没有办法使用继承,以便共同的元素只能被设置一次?很显然,我可以为属性做到这一点,但<xs:choice>正在让我为这些元素绊倒。我的唯一选择是将

 <xs:element name="indiv_NNN" type="Indiv_NNN" minOccurs="0"/> 
     <xs:element name="common-element" type="CommmonElement" minOccurs="0"/> 

纳入<xs:extension>?一定会有更好的办法!

回答

0

如果你的主要目标是避免重复自己,最简单的方法可能是使用一个名为模型组:

<group name="basic-choice"> 
    <choice> 
    <element name="common-element" 
      type="tns:CommonElement" 
      minOccurs="0"/> 
    </ 
</ 

所有类型的共享TNS:常见的元素是指这个群体:

<complexType name="Type_NNN"> 
    <choice> 
    <element name="indiv_NNN" 
      type="tns:Indiv_NNN" 
      minOccurs="0"/> 
    <group ref="tns:basic-choice"/> 
    </ 
</ 
0

xs:choice不可扩展。扩展将进入派生类型,扩展内容将进入原始内容之后的独立块。

有很好的策略来构建基类型,以便它们具有很强的扩展性。如果您可以修改基本类型,那么使其可扩展的一个好方法是让它包含一个旨在成为替换组头部的元素。那么任何扩展都可以提供一个可替代该头元素的元素。

例如,你可以用一个扩展点头脑中建立的基本类型:

<complexType name="base_type"> 
    <sequence> 
    <element ref="base_ns:base_extension_point_head" minOccurs="0" maxOccurs="unbounded"> 
    </sequence> 
</complexType> 

<element name="base_extension_point_head" abstract="true"/> 

然后扩展会替代的磁头组件:

<element name="extension" type="extension_ns:type" substitutionGroup="base_ns:base_extension_point_head"/> 

NIEM使用此策略,以确保参考模式中的类型是可扩展的。 NIEM称他们为“增强点”。例如,请参阅the NIEM naming and design rules