by Eric van der Vlist is published by O'Reilly & Associates (ISBN: 0596004214)
What if, instead of giving a list of allowed values, you want to give a list of values that are forbidden? The except pattern serves precisely this purpose.
To exclude the value 0836217462 from the possible ISBN numbers, write:
<element name="isbn"> <data type="token"> <except> <value>0836217462</value> </except> </data> </element> |
or, using the compact syntax:
element isbn {token - "0836217462"} |
Although this statement looks simple, note that the type can be defined at two different levels here: it must be defined in the data pattern and may also be defined in the value pattern; these two definitions have a different meaning. The type attached to the data pattern defines a validation performed on the text node, while the type attached to the value pattern defines how the value should be interpreted and which whitespace processing should be performed.
In this example, both are token types, and values such as " 0836217462 " are excluded as well as "0836217462". The token type, as noted previously, normalizes whitespace before making comparisons. The two datatypes can also be mixed, as in:
<attribute name="available"> <data type="token"> <except> <choice> <value type="string">available</value> <value type="string">checked out</value> <value type="string">on hold</value> </choice> </except> </data> </attribute> |
or, using the compact syntax:
attribute available {token -(string "available"|string "checked out"|string "on hold")} |
In this case, the first control is done on the datatype token, and the comparison uses the datatype string. To push this a little further, let's examine what happens when you use it with datatypes (which are shown in Chapter 8):
<data type="integer"> <except> <choice> <value type="integer">1</value> </choice> </except> </data> |
or, using the compact syntax:
integer -(integer "1") |
In this case, both controls are performed on integers. This statement accepts any integer except values representing "1" as an integer. ("1" and also "01" or "001" are forbidden.)
Now, consider:
<data type="integer"> <except> <choice> <value>1</value> </choice> </except> </data> |
or, using the compact syntax
integer -("1") |
The value has a default type of token so that "01" is normalized, then compared to "1" as a token. The two aren't equal (as tokens) so the except isn't triggered; hence "01" is passed up to the next level. Next, the data has a type of integer so that "01" (normalized by the previous step) is tested to see if it's an integer. It is, so it's accepted as valid.
This text is released under the Free Software Foundation GFDL.