22.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 because 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 http://example.com/person.xsd :

 <person xsi:noNamespaceSchemaLocation="http://example.com/person.xsd">   <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 ignore the hint 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 http://www.elharo.com/person.xsd:

 <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.xsd">   <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, 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 http://www.elharo.com/person.xsd, while items from the http://www.cafeconleche.org/namespaces/ names namespace should be validated against the schema found at the relative URL names.xsd :

 <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.xsd                       http://www.cafeconleche.org/namespaces/names                       names.xsd">   <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 make an element 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 derived type.

For example, suppose a schema says that a ticket element has type TicketType . If the schema also defines BusTicketType and AirplaneTicketType elements as subtypes of TicketType , then a ticket element could also use the BusTicketType and AirplaneTicketType content models provided it had an xsi:type="BusTicketType " or xsi:type="AirplaneTicketType " attribute.



XML in a Nutshell
XML in a Nutshell, Third Edition
ISBN: 0596007647
EAN: 2147483647
Year: 2003
Pages: 232

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net