Understanding XML namespaces

In an XML element names are user defined and there could arise conflicts. One possible scenario is, consider a WSDL in which a message consists of parts referencing elements, with the same name but from different XSDs. For example, consider below code that is snipped from a WSDL (though not valid due to no namespace references). Here the requestMessage has two parts one refers to ID from employee schema imported from EmpRequest.xsd and another part refers to an ID from salary schema imported from SalRequest.xsd. But there is no logical relation between elements in the part and the referenced schema document.


Figure 1 Element name conflict example

These name conflicts in XML can be avoided by implementing namespaces. Each XSD can be assigned a targetNamespace and the referencing part use ID elements by using xmlns (called as namespace). Here targetNamespace is a logical name of the XML document. This targetNamespace is used as xmlns in the referencing document. This way the child XML can import multiple parent documents and reference their elements by using parent’s targetNamespace as xmlns in child document.

In XML family language there are three types of namespaces – targetNamespace, xmlns (read as namespace) and default namespace.

  • targetNamespace: targetNamespace as a logical name of a document similar to class name in java class and is defined by the developer. Usually targetNamespace would be an attribute within the root element of the XML document. The value provided for namespace is called URI (Unique Resource Identifier) and is always defined by the developer. In below figure, an XSD in its root schema element has targetNamespace attribute.


Figure 2 Example code snippet for targetNamespace

  • xmlns (read as namespace): xmlns can be thought of as an instance of targetNamespace. One XML document can refer multiple targetNamespaces by creating corresponding xmlns.

    In the world of webservices, XML documents contain elements that most of the times refer other elements from a parent document. Technically xmlns is used as an attribute in every node to identify element’s targetNamespace. But to avoid using a long string value for xmlns, you can use qualifiers. Qualifier is a user defined keyword that holds the value of xmlns. In figure3, line5 to line8 defines xmlns for different target Namespaces. In each of these lines targetNamespace is referenced by using xmlns and qualifier delimited by “:”. For example, at line6 EmpRequest.xsd document is referenced with xmlns:empReqNS. Here empReqNS is called a qualifier. Every element, attribute and pre-defined keyword is prefixed with qualifier. In the figure below the root element, definition, is a keyword defined in xml document with targetNamespace http://schmas.xmlsoap.org/wsdl. Therefore, definition element is prefixed with “wsdl:”, and qualifier wsdl is defined in the same root node with value http://schmas.xmlsoap.org/wsdl. There are many elements in this document refer to targetNamespace http://schmas.xmlsoap.org/wsdl, and this targetNamespace defines most of the keywords used in a WSDL document. Another frequently used targetNamespace is http://www.w3.org/2001/XMLSchema. Almost all keywords used in types block of the WSDL refer elements from this targetNamespace, and this is owned by w3 schools. In an XML document, you can have element refer another element in the same document. For example, the input message used inside operation block refers to the message defined in message block.


Figure 3 Example code snippet for xmlns

By connecting xmlns and targetNamespace parser validates each of the element present in the XML. Therefore, it is good practice to have elements always prefixed by its corresponding xmlns qualifier. Below figure 4 shows a sample input file that is used to invoke execute operation in the WSDL shown in figure 3.

Figure 4 Sample XML input with xmlns

Root element has xmlns attribute pointing to the input schema XSD with targetNamespace http://www.clarityconsulting.com/trng/helloworld/xsd/Request. Please note that this not the targetNamespace of the XSD and not WSDL. Parser validates the input elements against the definition of types in XSD.

  • default namespace: Elements in an XML can contain multiple xmlns, each identified by a unique qualifier and optionally one xmlns without a qualifier. The xmlns without a qualifier is called default namespace. The scope of the default namespace is the element and its children.


Figure 5 Example for default namespace

If there are elements in an XML without a qualifier prefix, parser climbs up to its parents’, grandparents’ and great grandparents’ nodes till it finds a default namespace.

Notes:

  1. An XML document that has keywords defined by developers should have developer defined targetNamespace, so that the keywords in this document can be refered by its child documents.
  2. An XML document that has no developer defined keywords and have keywords referred from another document should have xmlns defined in the root element pointing to the targetNamespace referred.


Figure 6 targetNamespace, xmlns and default namespace.

Leave a Reply

Your email address will not be published. Required fields are marked *