Ograniczenia/aspekty XSD
Ograniczenia służą do definiowania dopuszczalnych wartości elementów lub atrybutów XML. Ograniczenia dotyczące elementów XML są nazywane aspektami.
Ograniczenia wartości
Poniższy przykład definiuje element o nazwie „wiek” z ograniczeniem. Wartość wieku nie może być mniejsza niż 0 ani większa niż 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>
Ograniczenia zbioru wartości
Aby ograniczyć zawartość elementu XML do zestawu akceptowalnych wartości, użyjemy ograniczenia wyliczenia.
Poniższy przykład definiuje element o nazwie „samochód” z ograniczeniem. Jedyne dopuszczalne wartości to: 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>
Powyższy przykład mógłby również zostać napisany w ten sposób:
<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>
Uwaga: W tym przypadku typ „carType” może być używany przez inne elementy, ponieważ nie jest on częścią elementu „car”.
Ograniczenia szeregu wartości
Aby ograniczyć zawartość elementu XML w celu zdefiniowania serii cyfr lub liter, których można użyć, użyjemy ograniczenia wzorca.
Poniższy przykład definiuje element o nazwie „letter” z ograniczeniem. Jedyną dopuszczalną wartością jest JEDNA z WIELKICH liter od a do z:
<xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Następny przykład definiuje element o nazwie „inicjały” z ograniczeniem. Jedyną dopuszczalną wartością są TRZY WIELKIE LITERY od a do z:
<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>
W następnym przykładzie zdefiniowano również element o nazwie „inicjały” z ograniczeniem. Jedyną dopuszczalną wartością są TRZY WIELKIE LUB WIELKIE litery od a do z:
<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>
Następny przykład definiuje element o nazwie „wybór” z ograniczeniem. Jedyną dopuszczalną wartością jest JEDNA z następujących liter: x, y, OR z:
<xs:element name="choice">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[xyz]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Następny przykład definiuje element o nazwie „prodid” z ograniczeniem. Jedyną dopuszczalną wartością jest PIĘĆ cyfr w sekwencji, a każda cyfra musi mieścić się w zakresie od 0 do 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>
Inne ograniczenia szeregu wartości
Poniższy przykład definiuje element o nazwie „letter” z ograniczeniem. Dopuszczalna wartość to zero lub więcej wystąpień małych liter od a do z:
<xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="([a-z])*"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
W następnym przykładzie zdefiniowano również element o nazwie „letter” z ograniczeniem. Dopuszczalna wartość to jedna lub więcej par liter, każda para składa się z małej litery, po której następuje duża litera. Na przykład „sToP” zostanie zweryfikowane przez ten wzorzec, ale nie „Stop” lub „STOP” lub „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>
Następny przykład definiuje element o nazwie „płeć” z ograniczeniem. Jedyną dopuszczalną wartością jest mężczyzna LUB kobieta:
<xs:element name="gender">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="male|female"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Następny przykład definiuje element o nazwie „hasło” z ograniczeniem. Musi być dokładnie osiem znaków w rzędzie, a te znaki muszą być małymi lub dużymi literami od a do z lub liczbą od 0 do 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>
Ograniczenia dotyczące znaków odstępu
Aby określić, jak powinny być obsługiwane znaki odstępu, użyjemy ograniczenia whiteSpace.
Ten przykład definiuje element o nazwie „adres” z ograniczeniem. Ograniczenie whiteSpace jest ustawione na „zachowaj”, co oznacza, że procesor XML NIE USUNIE żadnych białych znaków:
<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="preserve"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Ten przykład definiuje również element o nazwie „adres” z ograniczeniem. Ograniczenie whiteSpace jest ustawione na „zamień”, co oznacza, że procesor XML ZAMIENI wszystkie znaki odstępu (znaki nowego wiersza, tabulatory, spacje i powrót karetki) spacjami:
<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="replace"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Ten przykład definiuje również element o nazwie „adres” z ograniczeniem. Ograniczenie whiteSpace jest ustawione na „collapse”, co oznacza, że procesor XML USUNIE wszystkie białe znaki (znaki nowego wiersza, tabulatory, spacje, powrót karetki są zastępowane spacjami, spacje wiodące i końcowe są usuwane, a wiele spacji jest zmniejszanych do pojedyncza spacja):
<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="collapse"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Ograniczenia dotyczące długości
Aby ograniczyć długość wartości w elemencie, użyjemy ograniczeń length, maxLength i minLength.
Ten przykład definiuje element o nazwie „hasło” z ograniczeniem. Wartość musi mieć dokładnie osiem znaków:
<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Ten przykład definiuje inny element o nazwie „hasło” z ograniczeniem. Wartość musi składać się z co najmniej pięciu znaków i maksymalnie ośmiu znaków:
<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>
Ograniczenia dotyczące typów danych
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 |