Using Namespaces in Schemas

 

Schemas are a collection of rules (also referred to as a grammar or vocabulary) that consist of type definitions (simple and complex), as well as element and attribute declarations. Because XML documents can have several vocabularies to describe different elements and attributes, the use of namespaces and prefixes removes ambiguity for element and attribute declarations. Distinguishing between element and attribute names for each namespace is essential when you use schemas from more than one namespace.

A namespace is usually a string used to differentiate between the namespaces, such as "urn:www.microsoft.com", "http://www.microsoft.com", "http://www.w3.org/2001/XMLSchema", and "uuid:1234567890".

An XML schema consists of a preamble that is followed by declarations.

Example

The following syntax uses the schema element and references three commonly used XML vocabularies using the xmlns attribute.

<schema 
   xmlns="http://www.w3.org/2001/XMLSchema" 
   xmlns:xs="http://www.w3.org/2001/XMLSchema-datatypes" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   version="1.0">
</schema>

The schema element uses its attributes to identify any external namespaces that are to be used by the schema and all of its child elements.

The first xmlns attribute refers to the standard Worldwide Web Consortium (W3C) XML Schema Recommendation that is used by most XML schemas. The other xmlns attributes include references to the basic XML-Data Reduced (XDR) schema elements such as element, attribute, complexType, group, simpleType, and so forth.

For an XML Schema definition language (XSD) schema, the schema has a top-level schema element. The schema element definition must include the following namespace.

http://www.w3.org/2001/XMLSchema  

It is not necessary to use xs or xsi as the namespace identifiers. You can use any prefix that does not conflict with the W3C namespaces that identify the XML schema specifications, however, xs and xsi are the recommended conventions.

If you identify the W3C namespace as the default namespace, you do not need to include the prefix in every XML schema declaration and can use unqualified names such as <element>. For example, <xs:element> would be unnecessary if the default namespace is declared as http://www.w3.org/2001/XMLSchema.

Example

The following syntax instructs the XML parser to use XML Schema, <xs:schema>, to validate the structure and content of an element.

<xs:schema 
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msc="http://tempuri.org/myschema">
  <xs:element name="Price" type="msc:mydecimal"/>
</xs:schema>

For XML-Data Reduced (XDR) schemas, the Schema element must include the following namespace. Note that Schema is initial capitalized for XDR schemas.

urn:schemas-microsoft-com:xml-data  

To use XDR schema data types, the Schema element must include the following namespaces.

urn:schemas-microsoft-com:xml-data  
urn:schemas-microsoft-com:datatypes  

Example

The following syntax instructs the XML parser to use XDR schema, x-schema, to validate the structure and content of an element.

<BOOK xmlns="x-schema:http://www.microsoft.com/BookInfo.xml">  
  <TITLE>Creepy Crawlies</TITLE>
  <PRICE currency="US Dollar">22.95</PRICE>
</BOOK>

The x-schema identifier at the start of the namespace URI instructs the XML parser to treat the reference as a valid reference to an XDR schema; it retrieves the schema from the URL and validates against it.

The version attribute is informational and represents the version number of the schema. You can use the version to ensure that the XML application is using a specific version of the schema to validate XML documents. You can use multiple schema versions: one for development, another for testing, another for production, and others for individual international markets or regional markets.

Example

The following syntax instructs the XML parser to use version "1.0" of the myschema namespace to validate the structure and content of an element that uses the myschema namespace.

<xs:schema 
   xmlns="http://www.w3.org/2001/XMLSchema" 
   xmlns:xs="http://www.w3.org/2001/XMLSchema-datatypes" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:myschema="http://myschema.com/schema1"
   version="1.0">
</xs:schema>

Show: