w3pop.com :: ÍøÂçѧԺ :: Schema (XSD) :: XSD Ô¼ÊøÃæ
Restrictions are used to define acceptable values for XML elements or attributes. Restrictions on XML elements are called facets.
Ô¼ÊøÊÇÓÃÓÚΪXMLÔªËØ»òÊôÐÔ¶¨Òå¿É½ÓÊܵÄÖµ¡£¶ÔÓÚXMLÔªËØµÄÔ¼Êø³ÆÖ®Îª“Ô¼ÊøÃæ”¡£
The following example defines an element called "age" with a restriction. The value of age cannot be lower than 0 or greater than 120:
ÏÂÊö°¸Àý¸øÃûΪ"age"µÄÔªËØ¶¨ÒåÁËÒ»¸ö“Ô¼Êø”¡£“age”µÄÖµÒª´óµÈÓÚ0£¬Ð¡µÈÓÚ120£º
<xs:element name="age"> <xs:simpleType> </xs:element> |
To limit the content of an XML element to a set of acceptable values, we would use the enumeration constraint.
ΪÁËÏÞÖÆXMLÔªËØµÄÄÚÈÝÖ»½ÓÊÜÒ»×é·ûºÏÌõ¼þµÄÖµ£¬ÎÒÃÇ»áʹÓÓÁоÙÔ¼Êø”¡£
The example below defines an element called "car" with a restriction. The only acceptable values are: Audi, Golf, BMW:
ÏÂÊö°¸Àý¸øÃûΪ"car"µÄÔªËØ¶¨ÒåÁËÔ¼ÊøÌõ¼þ£¬·ûºÏÌõ¼þµÄÖµÓУºAudi¡¢Golf¡¢BMW£º
<xs:element name="car"> <xs:simpleType> </xs:element> |
The example above could also have been written like this:
ÉÏÊö°¸ÀýÒ²¿ÉÒÔ°´ÕÕÏÂÃæµÄ·½Ê½Êéд£º
<xs:element name="car" type="carType"/> <xs:simpleType name="carType"> |
Note: In this case the type "carType" can be used by other elements because it is not a part of the "car" element.
×¢Ò⣺ÔÚÕâÖÖÇé¿öÏ£¬"carType"ÀàÐÍ¿ÉÒÔ±»ÆäËüÔªËØËùʹÓã¬ÒòΪËü²»ÊÇ"car"ÔªËØµÄÒ»²¿·Ö¡£
To limit the content of an XML element to define a series of numbers or letters that can be used, we would use the pattern constraint.
ΪÁËÏÞÖÆXMLÔªËØµÄÄÚÈÝ£¬ÎÒÃÇÖ»¶¨ÒåÁËһϵÁпɱ»XMLÄÚÈÝʹÓõÄÊý×Ö»ò×Öĸ¡£ÎªÁËʵÏÖÉÏÊöÄ¿±ê£¬ÎÒÃÇ¿ÉÒÔÓÓʽÑùÔ¼Êø”¡£
The example below defines an element called "letter" with a restriction. The only acceptable value is ONE of the LOWERCASE letters from a to z:
ÏÂÊö°¸Àý¸øÃûΪ"letter"µÄÔªËØ¶¨ÒåÁËÔ¼ÊøÌõ¼þ¡£Î¨Ò»·ûºÏÌõ¼þµÄÖµÊÇ´Ó a µ½ z Ö®¼äµÄÒ»¸öСд×Öĸ£º
<xs:element name="letter"> <xs:simpleType> </xs:element> |
The next example defines an element called "initials" with a restriction. The only acceptable value is THREE of the UPPERCASE letters from a to z:
ÏÂÊö°¸Àý¸øÃûΪ"initials"µÄÔªËØ¶¨ÒåÁËÒ»¸öÔ¼ÊøÌõ¼þ¡£Î¨Ò»·ûºÏÌõ¼þµÄÖµÊÇ´Ó a µ½ z Ö®¼äµÄ3¸ö´óд×Öĸ£º
<xs:element name="initials"> <xs:simpleType> </xs:element> |
The next example also defines an element called "initials" with a restriction. The only acceptable value is THREE of the LOWERCASE OR UPPERCASE letters from a to z:
ÏÂÊö°¸Àý¸øÃûΪ"initials"µÄÔªËØ¶¨ÒåÁËÒ»¸öÔ¼Êø¡£Î¨Ò»·ûºÏÌõ¼þµÄÖµÊÇ´Ó a µ½ z Ö®¼äµÄÈý¸ö´óд»òСд×Öĸ£º
<xs:element name="initials"> <xs:simpleType> </xs:element> |
The next example defines an element called "choice" with a restriction. The only acceptable value is ONE of the following letters: x, y, OR z:
ÏÂÊö°¸Àý¸øÃûΪ"choice"µÄÔªËØ¶¨ÒåÁËÒ»¸öÔ¼Êø£¬Î¨Ò»·ûºÏÌõ¼þµÄÖµÊÇx¡¢y¡¢zÈý¸ö×ÖĸÖеÄÈÎÒâÒ»¸ö£º
<xs:element name="choice"> <xs:simpleType> </xs:element> |
The next example defines an element called "prodid" with a restriction. The only acceptable value is FIVE digits in a sequence, and each digit must be in a range from 0 to 9:
ÏÂÊö°¸Àý¸øÃûΪ"prodid"µÄÔªËØ¶¨ÒåÁËÒ»¸öÔ¼Êø£¬Î¨Ò»·ûºÏÌõ¼þµÄÖµÊÇ´Ó0µ½9µÄ5¸ö°¢À²®Êý×ÖµÄÐòÁУº
<xs:element name="prodid"> <xs:simpleType> </xs:element> |
The example below defines an element called "letter" with a restriction. The acceptable value is zero or more occurrences of lowercase letters from a to z:
ÏÂÊö°¸Àý¸øÃûΪ"letter"µÄÔªËØ¶¨ÒåÁËÒ»¸öÔ¼ÊøÌõ¼þ¡£Î¨Ò»·ûºÏÌõ¼þµÄÖµÊÇ´Ó a µ½ z µÄСд×Öĸ£¨¿ÉÒÔ²»°üº¬ÈκÎ×Öĸ»ò°üº¬¶à¸ö×Öĸ£©£º
<xs:element name="letter"> <xs:simpleType> </xs:element> |
The next example also defines an element called "letter" with a restriction. The acceptable value is one or more pairs of letters, each pair consisting of a lower case letter followed by an upper case letter. For example, "sToP" will be validated by this pattern, but not "Stop" or "STOP" or "stop":
ÏÂÊö°¸Àý¸øÃûΪ"letter"µÄÔªËØ¶¨ÒåÁËÒ»¸öÔ¼ÊøÌõ¼þ¡£Î¨Ò»·ûºÏÌõ¼þµÄÖµÊÇÒ»¶Ô»ò¶à¶Ô×Öĸ£¬Ã¿¶Ô¶¼ÊÇÒ»¸öСд×Öĸºó¸úÒ»¸ö´óд×Öĸ×é³É¡£¾Ù¸öÀý×Ó£º"sToP"ÔÚÕâÖÖʽÑùÊǺϷ¨µÄ£¬µ«"Stop" ¡¢"STOP" »ò "stop"¶¼ÊDz»ºÏ·¨µÄ£º
<xs:element name="letter"> <xs:simpleType> </xs:element> |
The next example defines an element called "gender" with a restriction. The only acceptable value is male OR female:
ÏÂÊö°¸Àý¸øÃûΪ"gender"µÄÔªËØ¶¨ÒåÁËÒ»¸öÔ¼ÊøÌõ¼þ¡£Î¨Ò»·ûºÏµÄÖµÊÇmale £¨ÄÐÐÔ£©»òfemale£¨Å®ÐÔ£©£º
<xs:element name="gender"> <xs:simpleType> </xs:element> |
The next example defines an element called "password" with a restriction. There must be exactly eight characters in a row and those characters must be lowercase or uppercase letters from a to z, or a number from 0 to 9:
ÏÂÊö°¸Àý¸øÃûΪ"password"µÄÔªËØ¶¨ÒåÁËÒ»¸öÔ¼ÊøÌõ¼þ¡£Ò»ÐÐÀï±ØÐë°üº¬8¸ö×Ö·û£¬×Ö·û±ØÐëÊÇ´Ó a µ½ z µÄ´óд×Öĸ»òСд×Öĸ£¬»òÕßÊÇ´Ó 0 µ½ 9 µÄÊý×Ö£º
<xs:element name="password"> <xs:simpleType> </xs:element> |
To specify how whitespace characters should be handled, we would use the whiteSpace constraint.
ΪÁËÖ¸¶¨¿Õ°×·ûµÄ´¦Àí·½Ê½£¬ÎÒÃÇ¿ÉÒÔʹÓÿհ׷ûÔ¼Êø¡£
This example defines an element called "address" with a restriction. The whiteSpace constraint is set to "preserve", which means that the XML processor WILL NOT remove any white space characters:
ÏÂÊö°¸Àý¸øÃûΪ"address"µÄÔªËØ¶¨ÒåÁËÒ»¸öÔ¼ÊøÌõ¼þ¡£¿Õ°×·ûÉèÖÃΪ"preserve"£¨±£Áô£©£¬ÕâÒâζ×ÅXML´¦ÀíÆ÷²»»áɾ³ýÈκοհ׷û£º
<xs:element name="address"> <xs:simpleType> </xs:element> |
This example also defines an element called "address" with a restriction. The whiteSpace constraint is set to "replace", which means that the XML processor WILL REPLACE all white space characters (line feeds, tabs, spaces, and carriage returns) with spaces:
ÏÂÊö°¸Àý¸øÃûΪ"address"µÄÔªËØ¶¨ÒåÁËÒ»¸öÔ¼ÊøÌõ¼þ¡£¿Õ°×·ûÉèÖÃΪ" replace "£¨Ìæ´ú£©£¬ÕâÒâζ×ÅXML´¦ÀíÆ÷»áÓÿոñÌæ´úËùÓеĿհ××Ö·û£¨ÆäÖаüÀ¨£º»»Ðзû¡¢ÖƱí·û¡¢¿Õ¸ñ·û¡¢»Ø³µ·û£©£º
<xs:element name="address"> <xs:simpleType> </xs:element> |
This example also defines an element called "address" with a restriction. The whiteSpace constraint is set to "collapse", which means that the XML processor WILL REMOVE all white space characters (line feeds, tabs, spaces, carriage returns are replaced with spaces, leading and trailing spaces are removed, and multiple spaces are reduced to a single space):
ÏÂÊö°¸Àý¸øÃûΪ"address"µÄÔªËØ¶¨ÒåÁËÒ»¸öÔ¼ÊøÌõ¼þ¡£¿Õ°×·ûÉèÖÃΪ"collapse"£¨Çå³ý£©£¬ÕâÒâζ×ÅXML´¦ÀíÆ÷»áÇå³ýËùÓеĿհ××Ö·û£¨»»Ðзû¡¢ÖƱí·û¡¢¿Õ¸ñ·ûÒÔ¼°»Ø³µ·û¶¼±»¿Õ¸ñ·ûÌæ´ú¡£Í·²¿¡¢Î²²¿µÄ¿Õ¸ñ»á±»Çå³ý£¬¶à¸ö¿Õ¸ñÒ²»á×Ô¶¯¼õÉÙΪһ¸ö£©£º
<xs:element name="address"> <xs:simpleType> </xs:element> |
To limit the length of a value in an element, we would use the length, maxLength, and minLength constraints.
ΪÁËÏÞÖÆÔªËØµÄ³¤¶ÈÖµ£¬ÎÒÃÇʹÓÃlength [ ³¤¶È ]¡¢maxLength [ ×î´ó³¤¶È ] ºÍ minLength [ ×îС³¤¶È ] Ô¼ÊøÌõ¼þ¡£
This example defines an element called "password" with a restriction. The value must be exactly eight characters:
ÏÂÊö°¸Àý¸øÃûΪ"password"µÄÔªËØ¶¨ÒåÁËÒ»¸öÔ¼ÊøÌõ¼þ¡£Öµ±ØÐëÖ»°üº¬8¸ö×Ö·û£º
<xs:element name="password"> <xs:simpleType> </xs:element> |
This example defines another element called "password" with a restriction. The value must be minimum five characters and maximum eight characters:
ÏÂÊö°¸Àý¸øÃûΪ"password"µÄÔªËØ¶¨ÒåÁËÒ»¸öÔ¼ÊøÌõ¼þ¡£Öµ×îÉÙÐèÒª°üº¬5¸ö×Ö·û£¬×î¶àÖ»Äܰüº¬8¸ö×Ö·û£º
<xs:element name="password"> <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 Ö¸¶¨ÁËÔÊÐí³öÏÖµÄСÊýλÊýµÄ×î´óλÊý¡£Öµ±ØÐë´óÓÚµÈÓÚ0 |
| length | Specifies the exact number of characters or list items allowed. Must be equal to or greater than zero Ö¸¶¨ÁËÔÊÐí³öÏÖµÄ×Ö·û»òÁбíÏîµÄ¸öÊý¡£Öµ±ØÐë´óÓÚµÈÓÚ0 |
| 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 Ö¸¶¨ÁËÔÊÐí³öÏÖµÄ×Ö·û»òÁбíÏîµÄ×î´ó¸öÊý¡£Öµ±ØÐë´óÓÚµÈÓÚ0 |
| 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 Ö¸¶¨ÁËÔÊÐí³öÏÖµÄ×Ö·û»òÁбíµÄ×îС¸öÊý¡£Öµ±ØÐë´óÓÚµÈÓÚ0 |
| pattern | Defines the exact sequence of characters that are acceptable ¶¨ÒåÁË·ûºÏÒªÇóµÄ×Ö·ûµÄ¾«È·ÅÅÁÐ˳Ðò |
| totalDigits | Specifies the exact number of digits allowed. Must be greater than zero Ö¸¶¨ÁËÔÊÐí³öÏÖµÄ×Ö·ûµÄ¾«È·¸öÊý¡£Öµ±ØÐë´óÓÚ0 |
| whiteSpace | Specifies how white space (line feeds, tabs, spaces, and carriage returns) is handled Ö¸¶¨Á˿հ׷ûµÄ´¦Àí·½Ê½£¨ÆäÖаüÀ¨£º»»Ðзû¡¢ÖƱí·û¡¢¿Õ¸ñ·ûºÍ»Ø³µ·û£© |
ÆÀÂÛ (0)
All