XML Schema restriction 요소
정의와 사용법
restriction 요소는 simpleType, simpleContent 또는 complexContent를 정의한 제한을 정의합니다.
요소 정보
출현 횟수 | 한 번 |
부모 요소 | complexContent |
내용 | group, all, choice, sequence, attribute, attributeGroup, anyAttribute |
문법
<restriction id=ID base=QName any attributes > simpleType의 내용: (annotation?,(simpleType?,(minExclusive|minInclusive| maxExclusive|maxInclusive|totalDigits|fractionDigits| length|minLength|maxLength|enumeration|whiteSpace|pattern)*)) simpleContent의 내용: (annotation?,(simpleType?,(minExclusive |minInclusive| maxExclusive|maxInclusive|totalDigits|fractionDigits| (length|minLength|maxLength|enumeration|whiteSpace|pattern)*)?, ((attribute|attributeGroup)*,anyAttribute?)) complexContent의 내용: (annotation?,(group|all|choice|sequence)?, ((attribute|attributeGroup)*,anyAttribute?)) </restriction>
(? 기호는 restriction 요소에서 이 요소가 0회나 1회만 등장할 수 있음을 나타냅니다.)
속성 | 설명 |
---|---|
id | 선택 가능합니다. 이 요소의 독특한 ID를 정의합니다. |
base | 필수입니다. 이 schema(또는 지정된 이름 공간을 통해 지정된 다른 schema)에서 정의된 내장 데이터 타입, simpleType 또는 complexType 요소의 이름을 정의합니다. |
any attributes | 선택 가능합니다. non-schema 이름 공간을 가진 다른 어떤 속성도 정의합니다. |
예제
예제 1
아래 예제는 제약 조건을 가진 이름이 "age"인 요소를 정의합니다. age의 값은 0보다 작거나 100보다 크지 않아야 합니다:
<xs:element name="age"> <xs:simpleType> <xs:restriction base="xs:integer"> <xs:minInclusive value="0"/> <xs:maxInclusive value="100"/> </xs:restriction> </xs:simpleType> </xs:element>
예제 2
이 예제는 "initials"라는 이름의 요소를 정의합니다. "initials" 요소는 제약 조건을 가진 간단한 타입입니다. 허용된 값은 a부터 z까지의 대문자나 소문자 3자리입니다:
<xs:element name="initials"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[a-zA-Z][a-zA-Z][a-zA-Z]"/> </xs:restriction> </xs:simpleType> </xs:element>
예제 3
이 예제는 "password"라는 이름의 요소를 정의합니다. "password" 요소는 제약 조건을 가진 간단한 타입입니다. 값은 최소 5자리에서 최대 8자리여야 합니다:
<xs:element name="password"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:minLength value="5"/> <xs:maxLength value="8"/> </xs:restriction> </xs:simpleType> </xs:element>
예제 4
이 예제는 제약 조건을 사용한 복잡한 타입 정의를 보여줍니다. 복잡한 타입 "Chinese_customer"는 일반 customer 복잡한 타입에서 파생되었으며, country 요소의 고정 값은 "China"입니다:
<xs:complexType name="customer"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> <xs:element name="country" type="xs:string"/> </xs:sequence> </xs:complexType> <xs:complexType name="Chinese_customer"> <xs:complexContent> <xs:restriction base="customer"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> <xs:element name="country" type="xs:string" fixed="China"/> </xs:sequence> </xs:restriction> </xs:complexContent> </xs:complexType>