Book HomeXML in a NutshellSearch this book

21.4. Instance Document Attributes

The W3C XML Schema Language defines four attributes in the http://www.w3.org/2001/XMLSchema-instance namespace (here mapped to the xsi prefix), which are attached to elements in the instance document rather than elements in the schema. These are as follows: xsi:nil, xsi:type, xsi:schemaLocation, and xsi:noNamespaceSchemaLocation. All four of these attributes are special in that the schemas do not need to declare them.

xsi:nil

The xsi:nil attribute indicates that a certain element does not have a value or that the value is unknown. This is not the same as having a value that is zero or the empty string. Semantically, it is equivalent to SQL's null. For example, in this full_name element, the last_name child has a nil value:

<full_name xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <first_name>Cher</first_name>
  <last_name xsi:nil="true"/>
</full_name>

It is not relevant whether an empty-element tag or a start-tag/end-tag pair is used to represent the nil element. However, a nil element may not have any content.

In order for this document to be valid, the element declaration for the name element must explicitly specify that nil values are allowed by setting the nillable attribute to true. For example:

<xs:element name="last_name" type="xs:string" nillable="true"/>
xsi:noNamespaceSchemaLocation

The xsi:noNamespaceSchemaLocation attribute locates the schema for elements that are not in any namespace. (Attributes that are not in any namespace are assumed to be declared in the same schema as their parent element.) Its value is a relative or absolute URL where the schema document can be found. It is most commonly attached to the root element but can appear further down the tree. For example, this person element claims that it should be validated against the schema found at the URL http://www.elharo.com/person.xs:

<person xsi:noNamespaceSchemaLocation="http://www.elharo.com/person.xs">
  <name>
    <first_name>Alan</first_name>
    <last_name>Turing</last_name>
  </name>
  <profession>computer scientist</profession>
  <profession>mathematician</profession>
  <profession>cryptographer</profession>
</person>

These are only suggestions. Schema processors may use other means of locating the relevant schemas and to ignore the hints provided by xsi:noNamespaceSchemaLocation.

xsi:schemaLocation

The xsi:schemaLocation attribute locates schemas for elements and attributes that are in a specified namespace. Its value is a namespace URI followed by a relative or absolute URL where the schema for that namespace can be found. It is most commonly attached to the root element but can appear further down the tree. For example, this person element in the http://www.cafeconleche.org/namespaces/person namespace claims that it should be validated against the schema found at the URL http://www.elharo.com/person.xs:

<person xmlns="http://www.cafeconleche.org/namespaces/person"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.cafeconleche.org/namespaces/person
                      http://www.elharo.com/person.xs">
  <name>
    <first_name>Alan</first_name>
    <last_name>Turing</last_name>
  </name>
  <profession>computer scientist</profession>
  <profession>mathematician</profession>
  <profession>cryptographer</profession>
</person>

If more than one namespace is used in a document, then each namespace must have its own schema. The namespace URIs and schema URLs can be listed in sequence in the same xsi:schemaLocation attribute. For example, the xsi:schemaLocation attribute on this person element says that items from the http://www.cafeconleche.org/namespaces/person namespace should be validated against the schema found at the URL http://www.elharo.com/person.xs, while items from the http://www.cafeconleche.org/namespaces/names namespace should be validated against the schema found at the relative URL names.xs.

<person xmlns="http://www.cafeconleche.org/namespaces/person"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.cafeconleche.org/namespaces/person
                      http://www.elharo.com/person.xs
                      http://www.cafeconleche.org/namespaces/names
                      names.xs">
  <name xmlns="http://www.cafeconleche.org/namespaces/names">
    <first_name>Alan</first_name>
    <last_name>Turing</last_name>
  </name>
  <profession>computer scientist</profession>
  <profession>mathematician</profession>
  <profession>cryptographer</profession>
</person>

These are only suggestions. Schema processors are allowed to use other means of locating the relevant schemas and to ignore the hints provided by xsi:schemaLocation.

xsi:type

The xsi:type attribute may be used in instance documents to indicate the type of an element, even when a full schema is not available. For example, this length element has type xs:decimal:

<length xsi:type="xs:decimal">23.5</length>

More importantly, the xsi:type attribute enables a limited form of polymorphism. That is, it allows you to place an instance of a derived type where an instance of the base type would normally be expected. The instance of the derived type must carry an xsi:type attribute identifying it as an instance of the base type. For example, suppose a schema says that a transport element must contain exactly one vehicle element. If bus, train, and airplane are all subtypes of vehicle, then these are valid transport elements:

<transport>
  <airplane xsi:type="airplane">Boeing 767</airplane>
</transport>
<transport>
  <bus xsi:type="bus">Greyhound</bus>
</transport>

However, when the xsi:type attributes are removed, these are no longer valid transport elements:

<transport>
  <airplane>Boeing 767</airplane>
</transport>
<transport>
  <bus>Greyhound</bus>
</transport>


Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.