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
XML Schema Preamble
An XML schema consists of a preamble that is followed by declarations.
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.
It is not necessary to use
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,
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
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.
To use XDR schema data types, the Schema element must include the following namespaces.
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>
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.
Specifying the Version
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.
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
<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>