| home / authoring / languages / xml / schemas / chap1 / 2 | [previous] [next] |
|
With DTDs we could supply a default attribute value for an attribute that was left empty in an instance document, but there was no equivalent mechanism for elements. With XML Schema, we can supply a default value for text-only element content.
If we specify a default value for an element, and that element is empty in the instance document, an XML Schema aware processor would treat the document as though it had the default value when it parses the document. In the following example we have a fragment of an XML instance document, which is used to profile a member's subscription to a web site:
<MailOut>
<Subscribe></Subscribe>
</MailOut>
We want the default content of the Subscribe element to be yes, so
we add a default attribute to the element declaration, whose value is the simple
element content we want:
<element name = "Subscribe" type = "string" default = "yes" />
Once parsed, if the Subscribe element were empty in the instance document,
the schema processor would treat the Subscribe element as if it had contained the string
yes.
There is another attribute that we can add to an element declaration, called
fixed. When fixed is used on an element declaration, the element's
content must either be empty (in which case it behaves like default), or the element
content must match the value of the fixed attribute. If the document contained a value
other than that expressed by the fixed attribute it would not be valid.
For example, if we wanted a SecurityCleared element to either contain
the boolean value of true, or if empty to be treated as if it contains
true, we would use the fixed attributes like this:
<element name = "SecurityCleared" type = "boolean" fixed = "true" />
Therefore, the following would be valid:
<SecurityCleared>true</SecurityCleared>
As would either of these:
<SecurityCleared></SecurityCleared>
<SecurityCleared />
In either of the above cases, the processor would treat the element as if it had the content
true. However, the three examples below would not be valid:
<SecurityCleared>false</SecurityCleared>
<SecurityCleared>no</SecurityCleared>
<SecurityCleared><UserID>001</UserID></SecurityCleared>
It should be noted that the value of the element is measured against the permitted values
for the datatype. We will look at datatypes in more detail in the next chapter, but the examples here
are not valid because the only allowed values for a boolean whose value is true,
are the string true or the value 1. The following would be a valid example,
because 1 is an allowed value for the datatype:
<SecurityCleared>1</SecurityCleared>
This would be helpful in preventing any documents being validated if they explicitly
contained any content other than the string true.
Note that we could not add both a default and a fixed attribute
to the same element declaration.
Together the
defaultandfixedattributes are known as value constraints, because they constrain the values allowed in element content.
| home / authoring / languages / xml / schemas / chap1 / 2 | [previous] [next] |
Created: October 22, 2001
Revised: October 22, 2001
URL: http://webreference.com/authoring/languages/xml/schemas/chap1/2/4.html