Home > Articles > Programming > Java

  • Print
  • + Share This
From the author of

Target Namespaces

While the XML Schema elements themselves are a part of the XML Schema namespace, the elements and attributes we're defining with the schema are not. For example, with the pen.xsd and pencil.xsd schemas, we may want each schema to define the elements of a specific Pen or Pencil namespace, to help keep things straight if we merge the two schemas at a later time. To do this, we can make use of the targetNamespace attribute within the XML Schema itself. The targetNamespace attribute allows us to specify the namespace for which this particular schema is defining components. So we can add targetNamespace attributes to our existing Pen and Pencil schemas:

<?xml version="1.0" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
 targetNamespace="http://www.mycompany.com/Pencil">

 <xs:element name="pencil">
 <xs:element name="color"/>
 <xs:element name="type"/>
 <xs:element name="size"/>
 </xs:element>
</xs:schema>

And:

<?xml version="1.0" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
 targetNamespace="http://www.mycompany.com/Pen">

 <xs:element name="pen">
 <xs:element name="color"/>
 <xs:element name="type"/>
 <xs:element name="size"/>
 </xs:element>
</xs:schema>

Now we have two schemas, each defining similar elements, but for two different namespaces, the http://www.myserver.com/Pencil namespace and the http://www.myserver.com/Pen namespace. Although we sometimes refer to namespaces by their shorthand, such as Pen or Pencil, it's important to note that to the XML parser the namespace consists of the whole URI.

  • + Share This
  • 🔖 Save To Your Account