Home > Articles > Programming > Java

  • Print
  • + Share This
From the author of

The Schema Namespace

The first way in which XML Schemas make use of namespaces is the XML Schema namespace. Because XML Schemas are well-formed XML documents, which are made up of elements and attributes themselves, it's necessary to alert applications to the fact that these elements and attributes are a part of the XML Schemas Recommendation, and not some other type of generic document. To do this, we could declare a default namespace for the Schema:

<?xml version="1.0" ?>
<schema xmlns="http://www.w3.org/2001/XMLSchema">
 <element name="pen">
 <element name="color"/>
 <element name="type"/>
 <element name="size"/>
 </element>
</schema>

In the above version, we simply declare the Schema namespace as the default for the document, so we don't need to use the xs prefix on components. If we defined the pen.xsd Schema to use a prefix, it would look like this:

<?xml version="1.0" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xs:element name="pen">
 <xs:element name="color"/>
 <xs:element name="type"/>
 <xs:element name="size"/>
 </xs:element>
</xs:schema>

The advantage to the first method is readability. However, the advantage of the second is compatibility, for using the Schema with other documents and easily importing/including this schema into other schemas. In general, it's considered best practice to employ the qualified (prefixed) names when authoring XML Schema documents.

  • + Share This
  • 🔖 Save To Your Account