4

Person要素の定義があり、実行内容に応じて異なる要素が必要になります。たとえば、Personを追加する場合は、Personを更新するのとは異なる要素を送信する必要があります。以下の例では、Personタイプが現在複製されていますが、これはもちろん間違っています。これをxsdで表現して、Personタイプを再利用できるようにする良い方法はありますか?

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="Person">
        <xs:annotation>
            <xs:documentation>This is the definition when changing a person</xs:documentation>
        </xs:annotation>
        <xs:sequence>
            <xs:element name="PartyName" type="xs:string" minOccurs="0" maxOccurs="1"/>
            <xs:element name="GenderCode" type="GenderCode_Type" minOccurs="0" maxOccurs="1"/>
            <xs:element name="BirthDate" type="xs:date" minOccurs="0" maxOccurs="1"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="Person">
        <xs:annotation>
            <xs:documentation>This is the definition when adding a person</xs:documentation>
        </xs:annotation>
        <xs:sequence>
            <xs:element name="PartyName" type="xs:string" minOccurs="1" maxOccurs="1"/>
            <xs:element name="GenderCode" type="GenderCode_Type" minOccurs="1" maxOccurs="1"/>
            <xs:element name="BirthDate" type="xs:date" minOccurs="0" maxOccurs="1"/>
        </xs:sequence>
    </xs:complexType>   
</xs:schema>
4

1 に答える 1

4

Person要素に2つの異なるタイプを設定する最も簡単な方法は、念頭に置いている2つの異なるコンテキストでPersonのローカル宣言を使用することです。たとえば、次のように言うことができます。

<xs:element name="Add">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="Person" type="AddPerson"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

<xs:element name="Update">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="Person" type="ChangePerson"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

この例では、2つの複合型をAddPersonとChangePersonとして再定義したことを前提としています。

さらに、2つの複合型を明示的に関連付けたい場合は、一般的なPerson型からの制限によって両方を導出できます。

<xs:complexType name="Person">
  <xs:annotation>
    <xs:documentation>This is the generic 
      definition for persons</xs:documentation>
  </xs:annotation>
  <xs:sequence>
    <xs:element name="PartyName" type="xs:string" 
                minOccurs="0" maxOccurs="1"/>
    <xs:element name="GenderCode" type="GenderCode_Type" 
                minOccurs="0" maxOccurs="1"/>
    <xs:element name="BirthDate" type="xs:date" 
                minOccurs="0" maxOccurs="1"/>
  </xs:sequence>
</xs:complexType>

<xs:complexType name="ChangePerson">
  <xs:annotation>
    <xs:documentation>This is the definition 
      when changing a person</xs:documentation>
  </xs:annotation>
  <xs:complexContent>
    <xs:restriction base="Person">
      <xs:sequence>
        <xs:element name="PartyName" type="xs:string" 
                    minOccurs="0" maxOccurs="1"/>
        <xs:element name="GenderCode" type="GenderCode_Type" 
                    minOccurs="0" maxOccurs="1"/>
        <xs:element name="BirthDate" type="xs:date" 
                    minOccurs="0" maxOccurs="1"/>
      </xs:sequence>        
    </xs:restriction>
  </xs:complexContent>
</xs:complexType>

<xs:complexType name="AddPerson">
  <xs:annotation>
    <xs:documentation>This is the definition 
      when adding a person</xs:documentation>
  </xs:annotation>
  <xs:complexContent>
    <xs:restriction base="Person">
      <xs:sequence>
        <xs:element name="PartyName" type="xs:string" 
                    minOccurs="1" maxOccurs="1"/>
        <xs:element name="GenderCode" type="GenderCode_Type" 
                    minOccurs="1" maxOccurs="1"/>
        <xs:element name="BirthDate" type="xs:date" 
                    minOccurs="0" maxOccurs="1"/>
      </xs:sequence>        
    </xs:restriction>
  </xs:complexContent>
</xs:complexType>  

ここで、ジェネリック型のPersonはAddPerson型と同じです。ジェネリック型から操作固有の型の両方を導出する対称性のために、空の制限を使用してAddPersonを定義しました。

タイプ間にこのような明示的な関係があることが実際に目標の達成に役立つかどうかは、もちろん、システムがスキーマタイプ定義をどのように使用するかに一部依存します。

于 2012-10-25T00:23:21.873 に答える