WebReference.com - Part 2 of Chapter 1: Professional XML Schemas, from Wrox Press Ltd (6/6) | WebReference

WebReference.com - Part 2 of Chapter 1: Professional XML Schemas, from Wrox Press Ltd (6/6)

To page 1To page 2To page 3To page 4To page 5current page
[previous]

Professional XML Schemas

Value Constraints on Attributes

As we would expect from working with attributes in DTDs, we can supply default and fixed content for an attribute's value in the XML Schema. This works rather like the value constraints on the element declarations.

If an attribute is not included in an element in an instance document, we can use the schema to tell the processor: "When processing the document, treat the element as if it had this attribute with the value given in the schema". We give an attribute a default value by adding the default attribute to the attribute declaration, like this:

<attribute name = "currency" default = "US$" />

If you have a default value for an attribute, then the use value must be set to optional.

Imagine that we wanted to be able to validate an XML document in the following format:

<CreditAccount currency = "US$">
   <AccountName>Ray Bayliss</AccountName>
   <AccountNumber>27012</AccountNumber>
   <Amount>200.00</Amount>
</CreditAccount>

In this example, we want to ensure that if the CreditAccount element does not have the currency attribute in the instance document, the processor acts as though the attribute is there, and that its value is US$. Here is an extract from a schema that will ensure this behavior:

<element name = "CreditAccount">
   <complexType>
      <sequence>
         <element name = "AccountName" type = "string" />
         <element name = "AccountNumber" type = "integer" />
         <element name = "Amount" type = "string" />
      </sequence>
      <attribute name = "currency" default = "US$" />
   </complexType>
</element>

If we want to indicate that the value of an attribute is the same as the value we prescribe in the schema, whether or not the attribute is present in the instance document, we can use the fixed attribute on the element declaration, like so:

<attribute name = "currency" fixed = "US$" />

If the attribute does not appear in the document, the value of fixed would act as the default attribute, and the processor would treat the document as though the attribute were there and had the value specified.

For example, if the CreditAccount element was declared to have the following attribute declaration:

<element name = "CreditAccount">
   <complexType>
      <sequence>
         <element name = "AccountName" type = "string" />
         <element name = "AccountNumber" type = "integer" />
         <element name = "Amount" type = "string" />
      </sequence>
      <attribute name = "currency" fixed = "US$" />
   </complexType>
</element>

Then the following document instance would not be valid because the currency attribute has a value of AUS$ not US$:

<CreditAccount currency = "AUS$">
   <AccountName>Ray Bayliss</AccountName>
   <AccountNumber>2701 2202</AccountNumber>
   <Amount>200.00</Amount>
</CreditAccount>

If the attribute were missing, the schema processor would treat the CreditAccount element as though it was carrying a currency attribute whose value is US$.

Note that you could not add both a default and a fixed attribute to the same attribute declaration.

Together the default and fixed attributes are known as value constraints, because they constrain the value of the attribute.


To page 1To page 2To page 3To page 4To page 5current page
[previous]

Created: October 22, 2001
Revised: October 22, 2001


URL: http://webreference.com/authoring/languages/xml/schemas/chap1/2/6.html