【问题标题】:Extending a choice in XSD扩展 XSD 中的选择
【发布时间】:2014-12-07 02:14:44
【问题描述】:

我已经阅读了How to extend a choice complexType without sequencing the choice?XML Schema: Extend xsd:choice so that union (not sequence) of choices is created。我的问题是相关的,但略有不同。我有一个 XML Schema,其中包含许多不同的 complexTypes,它们看起来都像这样:

<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”的项目。有没有办法使用继承,这样公共元素只需设置一次?很明显,我可以为属性执行此操作,但 &lt;xs:choice&gt; 让我为元素而绊倒。是我唯一的选择吗?

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

进入&lt;xs:extension&gt;?一定有更好的办法!

【问题讨论】:

    标签: xml xsd


    【解决方案1】:

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

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

    所有共享 tns:common-element 的类型都指向这个组:

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

    【讨论】:

      【解决方案2】:

      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"/>
      

      然后一个扩展将替代 head 元素:

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

      NIEM 使用此策略来确保引用架构中的类型是可扩展的。 NIEM 称它们为“增强点”。示例见the NIEM naming and design rules

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-26
        • 1970-01-01
        • 1970-01-01
        • 2016-10-07
        • 1970-01-01
        相关资源
        最近更新 更多