37장. element Substitution

초록

XML 스키마 대체 그룹을 사용하면 최상위 수준 또는 헤드 요소를 대체할 수 있는 요소 그룹을 정의할 수 있습니다.XML Schema substitution groups allow you to define a group of elements that can replace a top level, or head, element. 이는 공통 기본 형식을 공유하거나 교환이 가능해야 하는 요소를 공유하는 여러 요소가 있는 경우에 유용합니다.

37.1. XML 스키마의 그룹 대체

37.1.1. 개요

대체 그룹은 해당 스키마에서 생성된 문서에서 다른 요소를 교체할 수 있는 요소를 지정할 수 있는 XML 스키마의 기능입니다. replaceable 요소는 head 요소라고 하며 스키마의 전역 범위에 정의해야 합니다. 대체 그룹의 요소는 head 요소 또는 head 요소의 형식에서 파생되는 형식과 동일한 형식이어야 합니다.

대체 그룹을 사용하면 제네릭 요소를 사용하여 지정할 수 있는 요소 컬렉션을 빌드할 수 있습니다.A substitution group allows you to build a collection of elements that can be specified using a generic element. 예를 들어 3가지 유형의 위젯을 판매하는 회사의 주문 시스템을 구축하는 경우 세 가지 위젯 유형에 대한 공통 데이터 세트가 포함된 일반 위젯 요소를 정의할 수 있습니다. 그런 다음 각 위젯 유형에 대한 보다 구체적인 데이터 세트가 포함된 대체 그룹을 정의할 수 있습니다. 그러면 각 위젯 유형에 대한 특정 순서 작업을 정의하는 대신 일반 위젯 요소를 메시지 파트로 지정할 수 있습니다. 실제 메시지가 작성되면 메시지에 대체 그룹의 요소가 포함될 수 있습니다.

37.1.2. 구문

대체 그룹은 XML Schema 요소 요소substitutionGroup 특성을 사용하여 정의됩니다. substitutionGroup 특성의 값은 정의되는 요소가 대체되는 요소의 이름입니다. 예를 들어, 헤드 요소 위젯 이면, 목재Group="widget"이라는 요소에 특성 substitutionGroup=" widget "을 추가 하면 위젯 요소가 사용되는 모든 곳에서는 목차를 대체할 수 있습니다. 이는 예 37.1. “대체 그룹 사용” 에 표시됩니다.

예 37.1. 대체 그룹 사용

<element name="widget" type="xsd:string" />
<element name="woodWidget" type="xsd:string"
         substitutionGroup="widget" />

37.1.3. 유형 제한 사항

대체 그룹의 요소는 head 요소 또는 head 요소의 형식에서 파생된 형식의 형식과 동일해야 합니다. 예를 들어 head 요소가 xsd:int인 경우 대체 그룹의 모든 멤버가 xsd:int 형식 또는 xsd:int에서 파생되는 형식이어야 합니다.For example, if the head element is of type xsd:int all members of the substitution group must be of type xsd:int or of a type derived from xsd:int. 대체 그룹의 요소가 head 요소의 유형에서 파생되는 유형인 예 37.2. “Complex Types을 사용하는 대체 그룹” 에 표시된 대체 그룹을 정의할 수도 있습니다.

예 37.2. Complex Types을 사용하는 대체 그룹

<complexType name="widgetType">
  <sequence>
    <element name="shape" type="xsd:string" />
    <element name="color" type="xsd:string" />
  </sequence>
</complexType>
<complexType name="woodWidgetType">
  <complexContent>
    <extension base="widgetType">
      <sequence>
        <element name="woodType" type="xsd:string" />
      </sequence>
    </extension>
  </complexContent>
</complexType>
<complexType name="plasticWidgetType">
  <complexContent>
    <extension base="widgetType">
      <sequence>
        <element name="moldProcess" type="xsd:string" />
      </sequence>
    </extension>
  </complexContent>
</complexType>
<element name="widget" type="widgetType" />
<element name="woodWidget" type="woodWidgetType"
         substitutionGroup="widget" />
<element name="plasticWidget" type="plasticWidgetType"
         substitutionGroup="widget" />
<complexType name="partType">
  <sequence>
    <element ref="widget" />
  </sequence>
</complexType>
<element name="part" type="partType" />

대체 그룹인 위젯 의 헤드 요소는 widget Type 유형으로 정의됩니다. 대체 그룹의 각 요소는 widgetType 을 확장하여 해당 유형의 위젯 주문과 관련된 데이터를 포함합니다.

예 37.2. “Complex Types을 사용하는 대체 그룹” 의 스키마에 따라 예 37.3. “대체 그룹을 사용하는 XML 문서”파트 요소는 유효합니다.

예 37.3. 대체 그룹을 사용하는 XML 문서

<part>
  <widget>
    <shape>round</shape>
    <color>blue</color>
  </widget>
</part>
<part>
  <plasticWidget>
    <shape>round</shape>
    <color>blue</color>
    <moldProcess>sandCast</moldProcess>
  </plasticWidget>
</part>
<part>
  <woodWidget>
    <shape>round</shape>
    <color>blue</color>
    <woodType>elm</woodType>
  </woodWidget>
</part>

37.1.4. 추상 헤드 요소

스키마를 사용하여 생성된 문서에 절대 나타날 수 없는 추상 헤드 요소를 정의할 수 있습니다. 추상 헤드 요소는 제네릭 클래스의 보다 구체적인 구현을 정의하기 위한 기반으로 사용되기 때문에 Java의 추상 클래스와 유사합니다. 추상 헤드는 최종 제품에서 일반 요소를 사용하지 않도록 합니다.

예 37.4. “추상 헤드 정의” 와 같이 요소 요소의 abstract 속성을 true 로 설정하여 추상 헤드 요소 를 선언합니다. 이 스키마를 사용하면 유효한 review 요소에 positiveComment 요소가 포함될 수 있지만 주석 요소가 포함될 수 없습니다.

예 37.4. 추상 헤드 정의

<element name="comment" type="xsd:string" abstract="true" />
<element name="positiveComment" type="xsd:string"
         substitutionGroup="comment" />
<element name="negtiveComment" type="xsd:string"
         substitutionGroup="comment" />
<element name="review">
  <complexContent>
    <all>
      <element name="custName" type="xsd:string" />
      <element name="impression" ref="comment" />
    </all>
  </complexContent>
</element>