XSD 복합 타입 지시자
지시자를 통해 문서에서 요소 사용 방식을 제어할 수 있습니다.
지시자
칠가지 지시자가 있습니다:
순서지시자:
- 모두
- 선택
- 순서
발생지시자:
- maxOccurs
- minOccurs
Group 지시자:
- Group name
- attributeGroup name
Order 지시자
Order 지시자는 요소의 순서를 정의합니다.
All 지시자
<all> 지시자는 자식 요소가 어떤 순서로도 등장할 수 있으며, 각 자식 요소는 한 번만 등장해야 한다는 것을 정의합니다:
<xs:element name="person"> <xs:complexType> <xs:all> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:all> </xs:complexType> </xs:element>
주의사항:<all> 지시자를 사용할 때, <minOccurs>을 0 또는 1로 설정할 수 있으며, <maxOccurs> 지시자를 1로 설정할 수 있습니다 (minOccurs 및 maxOccurs에 대해 아래에서 설명할 것입니다).
Choice 지시자
<choice> 지시자는 어떤 자식 요소가 등장할 수 있거나 또 다른 자식 요소가 등장할 수 있다는 것을 정의합니다 (또는 그것이 아니면 안 됩니다):
<xs:element name="person"> <xs:complexType> <xs:choice> <xs:element name="employee" type="employee"/> <xs:element name="member" type="member"/> </xs:choice> </xs:complexType> </xs:element>
힌트:자식 요소가 무한히 많이 등장할 수 있도록 설정하려면 <maxOccurs> (아래에서 설명할 것입니다)을 unbounded (무한 횟수)으로 설정할 수 있습니다.
Sequence 지시자
<sequence>은 자식 요소가 특정 순서로 등장해야 한다는 것을 정의합니다:
<xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element>
Occurrence 지시자
Occurrence 지시자는 어떤 요소가 등장할 수 있는 빈도를 정의합니다.
주의사항:모든 "Order"와 "Group" 지시자 (any, all, choice, sequence, group name 및 group reference)의 maxOccurs 및 minOccurs의 기본 값은 모두 1입니다.
maxOccurs 지시자
<maxOccurs> 지시자는 어떤 요소가 등장할 수 있는 최대 횟수를 정의할 수 있습니다:
<xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="full_name" type="xs:string"/> <xs:element name="child_name" type="xs:string"> maxOccurs="10"/> </xs:sequence> </xs:complexType> </xs:element>
위의 예시는 "child_name" 자식 요소가 "person" 요소에 최소 1 번 등장할 수 있으며 (minOccurs의 기본 값은 1입니다), 최대 10 번 등장할 수 있다는 것을 보여줍니다.
minOccurs 지시자
<minOccurs> 지시자는 어떤 요소가 등장할 수 있는 최소 횟수를 정의할 수 있습니다:
<xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="full_name" type="xs:string"/> <xs:element name="child_name" type="xs:string"> maxOccurs="10" minOccurs="0"/> </xs:sequence> </xs:complexType> </xs:element>
위의 예시는 "child_name" 자식 요소가 "person" 요소에 최소 0 번, 최대 10 번 등장할 수 있다는 것을 보여줍니다.
힌트:요소가 무제한으로 횟수로 나타나도록 하려면 maxOccurs="unbounded" 선언을 사용하십시오:
실제 예제:
이 "Myfamily.xml" XML 파일입니다:
<?xml version="1.0" encoding="ISO-8859-1"?> <persons xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> xsi:noNamespaceSchemaLocation="family.xsd"> <person> <full_name>Tony Smith</full_name> <child_name>Cecilie</child_name> </person> <person> <full_name>David Smith</full_name> <child_name>Jogn</child_name> <child_name>mike</child_name> <child_name>kyle</child_name> <child_name>mary</child_name> </person> <person> <full_name>Michael Smith</full_name> </person> </persons>
위의 XML 파일은 "persons"라는 루트 요소를 포함하고 있습니다. 이 루트 요소 내에서는 세 개의 "person" 요소를 정의했습니다. 각 "person" 요소는 "full_name" 요소를 포함해야하며, 최대 5개의 "child_name" 요소를 포함할 수 있습니다.
이 스키마 파일 "family.xsd"입니다:
<?xml version="1.0" encoding="ISO-8859-1"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> elementFormDefault="qualified"> <xs:element name="persons"> <xs:complexType> <xs:sequence> <xs:element name="person" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="full_name" type="xs:string"/> <xs:element name="child_name" type="xs:string"> minOccurs="0" maxOccurs="5"/> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
Group 지시자
Group 지시자는 관련 요소批次를 정의하는 데 사용됩니다.
요소 그룹
요소 그룹은 group 선언을 통해 정의됩니다:
<xs:group name="组名称"> ... </xs:group>
group 선언 내부에서 all, choice 또는 sequence 요소를 정의해야 합니다. 다음 예제는 "persongroup"라는 이름의 group을 정의합니다. 이 group은 정확한 순서로 반드시 등장해야 하는 요소 그룹을 정의합니다:
<xs:group name="persongroup"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> <xs:element name="birthday" type="xs:date"/> </xs:sequence> </xs:group>
group 정의를 완료한 후, 다른 정의에서 참조할 수 있습니다:
<xs:group name="persongroup"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> <xs:element name="birthday" type="xs:date"/> </xs:sequence> </xs:group> <xs:element name="person" type="personinfo"/> <xs:complexType name="personinfo"> <xs:sequence> <xs:group ref="persongroup"/> <xs:element name="country" type="xs:string"/> </xs:sequence> </xs:complexType>
속성 그룹
속성 그룹은 attributeGroup 선언을 통해 정의됩니다:
<xs:attributeGroup name="组名称"> ... </xs:attributeGroup>
다음 예제는 "personattrgroup"라는 이름의 하나의 속성 그룹을 정의합니다:
<xs:attributeGroup name="personattrgroup"> <xs:attribute name="firstname" type="xs:string"/> <xs:attribute name="lastname" type="xs:string"/> <xs:attribute name="birthday" type="xs:date"/> </xs:attributeGroup>
이미 속성 그룹을 정의한 후, 다른 정의에서 그것을 참조할 수 있습니다. 예를 들어:
<xs:attributeGroup name="personattrgroup"> <xs:attribute name="firstname" type="xs:string"/> <xs:attribute name="lastname" type="xs:string"/> <xs:attribute name="birthday" type="xs:date"/> </xs:attributeGroup> <xs:element name="person"> <xs:complexType> <xs:attributeGroup ref="personattrgroup"/> </xs:complexType> </xs:element>