Default and fixed element values

The concept of default and fixed values has traditionally been applicable only to attribute values, and can still be applied to attributes using a schema model (as explained in the previous chapter). However, it is now possible to apply these concepts to element content as well.

Default element content

An element type can be assigned a default content value that is to be inserted automatically when the element is present, but empty. The same Default attribute used in attribute definitions can also be used in element definitions for this purpose:

<element name="companyName" type="string"
                            default="ACME Corp" />

The following examples are then equivalent:

<companyName/>
<companyName></companyName>
<companyName>ACME Corp</companyName>

But there is a subtle difference between the way element defaulting works and the way attribute defaulting works. With attributes, the default only applies when the attribute is not present. With elements, the default only applies when the element is empty, but present. When the element is omitted, the default does not apply.

Caution

While the standard makes it clear in the text that an omitted element has no default value, elsewhere in the specification it is implied that it does, but only when the element is allowed to occur at most once.


Fixed element content

The content of an element can be 'fixed in stone', meaning that a document instance author cannot change its value. While it may be possible for the element to be absent, when it is present it must hold the value specified. The Fixed attribute, used to create fixed values for attributes, is again used in element definitions for this purpose:

<element name="companyName" type="string"
                            fixed="ACME Corp" />

The following examples show some legal and illegal instances:

<companyName/>                          <!-- ERROR -->
<companyName></companyName>             <!-- ERROR -->
<companyName>ACME Corp</companyName>    <!-- OK -->
<companyName>My Corp</companyName>      <!-- ERROR -->

Caution

Just as for default values, while the standard makes it clear in the text that an omitted element has no fixed value, elsewhere in the specification it is implied that it does, but only when the element is allowed to occur at most once.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.136.154.103