XSD 제한 사항/측면
제한 사항은 XML 요소 또는 속성에 대해 허용되는 값을 정의하는 데 사용됩니다. XML 요소에 대한 제한을 패싯이라고 합니다.
값에 대한 제한
다음 예제는 제한이 있는 "age"라는 요소를 정의합니다. age 값은 0보다 작거나 120보다 클 수 없습니다.
<xs:element name="age">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="120"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
값 집합에 대한 제한 사항
XML 요소의 내용을 허용 가능한 값 집합으로 제한하려면 열거 제약 조건을 사용합니다.
아래 예제는 제한이 있는 "car"라는 요소를 정의합니다. 허용되는 값은 Audi, Golf, BMW뿐입니다.
<xs:element name="car">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Audi"/>
<xs:enumeration value="Golf"/>
<xs:enumeration value="BMW"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
위의 예는 다음과 같이 작성할 수도 있습니다.
<xs:element name="car" type="carType"/>
<xs:simpleType name="carType">
<xs:restriction base="xs:string">
<xs:enumeration value="Audi"/>
<xs:enumeration value="Golf"/>
<xs:enumeration value="BMW"/>
</xs:restriction>
</xs:simpleType>
참고: 이 경우 "carType" 유형은 "car" 요소의 일부가 아니기 때문에 다른 요소에서 사용할 수 있습니다.
일련의 값에 대한 제한 사항
사용할 수 있는 일련의 숫자 또는 문자를 정의하기 위해 XML 요소의 내용을 제한하려면 패턴 제약 조건을 사용합니다.
아래 예는 제한이 있는 "문자"라는 요소를 정의합니다. 허용되는 유일한 값은 a에서 z까지의 소문자 중 하나입니다.
<xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
다음 예제는 제한이 있는 "initials"라는 요소를 정의합니다. 허용되는 유일한 값은 a에서 z까지의 대문자 중 3개입니다.
<xs:element name="initials">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[A-Z][A-Z][A-Z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
다음 예제에서는 제한이 있는 "이니셜"이라는 요소도 정의합니다. 허용되는 유일한 값은 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>
다음 예는 제한이 있는 "choice"라는 요소를 정의합니다. 허용되는 유일한 값은 x, y, 또는 z 문자 중 하나입니다.
<xs:element name="choice">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[xyz]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
다음 예는 제한이 있는 "prodid"라는 요소를 정의합니다. 유일하게 허용되는 값은 시퀀스의 5자리 숫자이며 각 숫자는 0에서 9 사이의 범위에 있어야 합니다.
<xs:element name="prodid">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:pattern value="[0-9][0-9][0-9][0-9][0-9]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
일련의 값에 대한 기타 제한 사항
아래 예는 제한이 있는 "문자"라는 요소를 정의합니다. 허용되는 값은 a에서 z까지의 소문자가 0개 이상 발생하는 것입니다.
<xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="([a-z])*"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
다음 예제에서는 제한이 있는 "문자"라는 요소도 정의합니다. 허용되는 값은 하나 이상의 문자 쌍이며, 각 쌍은 소문자와 대문자로 구성됩니다. 예를 들어 "sToP"는 이 패턴으로 검증되지만 "Stop", "STOP" 또는 "stop"은 검증되지 않습니다.
<xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="([a-z][A-Z])+"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
다음 예는 제한이 있는 "gender"라는 요소를 정의합니다. 허용되는 유일한 값은 남성 또는 여성입니다.
<xs:element name="gender">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="male|female"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
다음 예는 제한이 있는 "password"라는 요소를 정의합니다. 한 행에 정확히 8개의 문자가 있어야 하며 해당 문자는 a에서 z까지의 소문자 또는 대문자이거나 0에서 9까지의 숫자여야 합니다.
<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z0-9]{8}"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
공백 문자에 대한 제한 사항
공백 문자를 처리하는 방법을 지정하려면 공백 제약 조건을 사용합니다.
이 예제는 제한이 있는 "address"라는 요소를 정의합니다. whiteSpace 제약 조건은 "preserve"로 설정되며, 이는 XML 프로세서가 공백 문자를 제거하지 않음을 의미합니다.
<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="preserve"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
이 예제는 또한 제한이 있는 "address"라는 요소를 정의합니다. whiteSpace 제약 조건은 "replace"로 설정되며, 이는 XML 프로세서가 모든 공백 문자(줄 바꿈, 탭, 공백 및 캐리지 리턴)를 공백으로 대체함을 의미합니다.
<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="replace"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
이 예제는 또한 제한이 있는 "address"라는 요소를 정의합니다. 공백 제약 조건이 "축소"로 설정되어 있습니다. 즉, XML 프로세서가 모든 공백 문자를 제거합니다(줄 바꿈, 탭, 공백, 캐리지 리턴은 공백으로 대체되고, 선행 및 후행 공백은 제거되고, 여러 공백은 다음으로 축소됩니다. 단일 공백):
<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="collapse"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
길이 제한
요소에서 값의 길이를 제한하려면 길이, maxLength 및 minLength 제약 조건을 사용합니다.
이 예에서는 제한이 있는 "password"라는 요소를 정의합니다. 값은 정확히 8자여야 합니다.
<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
이 예에서는 제한이 있는 "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>
데이터 유형에 대한 제한 사항
Constraint | Description |
---|---|
enumeration | Defines a list of acceptable values |
fractionDigits | Specifies the maximum number of decimal places allowed. Must be equal to or greater than zero |
length | Specifies the exact number of characters or list items allowed. Must be equal to or greater than zero |
maxExclusive | Specifies the upper bounds for numeric values (the value must be less than this value) |
maxInclusive | Specifies the upper bounds for numeric values (the value must be less than or equal to this value) |
maxLength | Specifies the maximum number of characters or list items allowed. Must be equal to or greater than zero |
minExclusive | Specifies the lower bounds for numeric values (the value must be greater than this value) |
minInclusive | Specifies the lower bounds for numeric values (the value must be greater than or equal to this value) |
minLength | Specifies the minimum number of characters or list items allowed. Must be equal to or greater than zero |
pattern | Defines the exact sequence of characters that are acceptable |
totalDigits | Specifies the exact number of digits allowed. Must be greater than zero |
whiteSpace | Specifies how white space (line feeds, tabs, spaces, and carriage returns) is handled |