XML Schema quiz on default values

Hello Folks,

Recall that when you declare an element (or attribute) you can give it a default value.

For example, I give the Altitude element a default value of 100:

	<xs:element name="Altitude" type="xs:integer" default="100" />

In an instance document, if you wish for Altitude to have the default value, then you can simply create it as an empty element:

	<Altitude></Altitude>

The value of Altitude is 100.

Let's take another example. Here I declare the Title element to be of type string and give it a default value, "Hello World":

	<xs:element name="Title" type="xs:string" default="Hello World" />

Now in my instance document I create an empty Title element:

	<Title></Title>

Quiz: What is the value of Title?

Scroll down to see the answer ...

























Answer: the value of Title is the empty string, not the default value. The reason is that the empty string is a valid value of the string data type. If you want Title to have the default value then you must explicitly enter the default value:

	<Title>Hello World</Title>

/Roger

Received on Thursday, 23 August 2012 21:47:32 UTC