2014-12-07 89 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