Restrizioni/sfaccettature XSD
Le restrizioni vengono utilizzate per definire valori accettabili per elementi o attributi XML. Le restrizioni sugli elementi XML sono chiamate facet.
Restrizioni sui valori
L'esempio seguente definisce un elemento chiamato "età" con una restrizione. Il valore dell'età non può essere inferiore a 0 o superiore a 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>
Restrizioni su un insieme di valori
Per limitare il contenuto di un elemento XML a un insieme di valori accettabili, utilizzeremo il vincolo di enumerazione.
L'esempio seguente definisce un elemento chiamato "auto" con una restrizione. Gli unici valori accettabili sono: 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>
L'esempio sopra potrebbe anche essere scritto in questo modo:
<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>
Nota: in questo caso il tipo "carType" può essere utilizzato da altri elementi perché non fa parte dell'elemento "car".
Restrizioni su una serie di valori
Per limitare il contenuto di un elemento XML per definire una serie di numeri o lettere che possono essere utilizzati, utilizzeremo il vincolo del modello.
L'esempio seguente definisce un elemento chiamato "lettera" con una restrizione. L'unico valore accettabile è UNA delle lettere Minuscole dalla a alla z:
<xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
L'esempio successivo definisce un elemento chiamato "initials" con una restrizione. L'unico valore accettabile è TRE delle lettere MAIUSCOLE dalla a alla 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>
L'esempio successivo definisce anche un elemento chiamato "initials" con una restrizione. L'unico valore accettabile è TRE delle lettere MAIUSCOLE O MAIUSCOLE dalla a alla 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>
L'esempio successivo definisce un elemento chiamato "scelta" con una restrizione. L'unico valore accettabile è UNA delle seguenti lettere: 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>
L'esempio successivo definisce un elemento chiamato "prodid" con una restrizione. L'unico valore accettabile è CINQUE cifre in una sequenza e ciascuna cifra deve essere compresa tra 0 e 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>
Altre restrizioni su una serie di valori
L'esempio seguente definisce un elemento chiamato "lettera" con una restrizione. Il valore accettabile è zero o più occorrenze di lettere minuscole dalla a alla z:
<xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="([a-z])*"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
L'esempio successivo definisce anche un elemento chiamato "lettera" con una restrizione. Il valore accettabile è una o più coppie di lettere, ciascuna composta da una lettera minuscola seguita da una lettera maiuscola. Ad esempio, "sToP" verrà convalidato da questo modello, ma non "Stop" o "STOP" o "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>
L'esempio successivo definisce un elemento chiamato "genere" con una restrizione. L'unico valore accettabile è maschio O femmina:
<xs:element name="gender">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="male|female"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
L'esempio successivo definisce un elemento chiamato "password" con una restrizione. Devono esserci esattamente otto caratteri in una riga e quei caratteri devono essere lettere minuscole o maiuscole dalla a alla z o un numero da 0 a 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>
Restrizioni sui caratteri degli spazi bianchi
Per specificare come devono essere gestiti i caratteri degli spazi bianchi, utilizzeremo il vincolo whiteSpace.
Questo esempio definisce un elemento chiamato "indirizzo" con una restrizione. Il vincolo whiteSpace è impostato su "preserve", il che significa che il processore XML NON rimuoverà alcuno spazio vuoto:
<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="preserve"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Questo esempio definisce anche un elemento chiamato "indirizzo" con una restrizione. Il vincolo whiteSpace è impostato su "replace", il che significa che il processore XML SOSTITUIRÀ tutti i caratteri di spazio vuoto (avanzamento riga, tabulazioni, spazi e ritorni a capo) con spazi:
<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="replace"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Questo esempio definisce anche un elemento chiamato "indirizzo" con una restrizione. Il vincolo whiteSpace è impostato su "compressa", il che significa che il processore XML RIMUOVERÀ tutti i caratteri degli spazi vuoti (avanzamenti di riga, tabulazioni, spazi, ritorni a capo vengono sostituiti con spazi, gli spazi iniziali e finali vengono rimossi e più spazi vengono ridotti a un unico spazio):
<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="collapse"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Restrizioni sulla lunghezza
Per limitare la lunghezza di un valore in un elemento, utilizzeremo i vincoli length, maxLength e minLength.
Questo esempio definisce un elemento chiamato "password" con una restrizione. Il valore deve essere esattamente di otto caratteri:
<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Questo esempio definisce un altro elemento chiamato "password" con una restrizione. Il valore deve essere minimo cinque caratteri e massimo otto caratteri:
<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>
Restrizioni per i tipi di dati
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 |