40

I am studying SOAP web services and I am really new to Web Services. In the WSDL I got a little confused with regards to the targetNamespace element in the definition and the namespace included in the xsd:schema.

<definitions .... targetNamespace=" " >

<xsd:schema>
<xsd:import namespace=" " schemaLocation=""/>
</xsd:schema>

What does it mean by these two elements and I searched through many articles in the web and I was unable to clearly identify this. Can anybody please explain me?

Do these two get same values?

Script47
  • 14,230
  • 4
  • 45
  • 66
Dilan
  • 1,389
  • 5
  • 14
  • 24

3 Answers3

33

The target namespace in the WSDL file will be the name space associated with the SOAP service itself. For example we have a customer service the namespace would be something like http://www.acme.com/Customer/V1/CustomerService.

The XSD imports section is where you specify the namespace of the XSD you are about to import. A XSD file will contain the data that the service will send i.e. it contains the object definitions that will be serialized into XML and sent up and down as the request and responses. A XSD can also contain the operations that the WSDL will expose.

A WSDL can import one or more XSD files and each XSD will have its own namespace. A XSD will contain a namespace such as:

  1. http://www.acme.com/Customer/V1/GetCustomerRequest for the GetCustomerRequest this will define the data structure of the GetCustomerRequest operation.
  2. http://www.acme.com/Customer/V1/CustomerObject for the CustomerObject this will define the data structure of the CustomerObject.

Namespaces are a bit like Java packages they just allow you to define a hierarchy of objects. One important thing to understand is that you might have two Customer objects one that belongs to your sale system and one that belong to your CRM system for example. By placing these in separate namespaces you will be able to use both Customer object in the same service as long as they have unique namespaces.

Normally these namespaces will form part of the SOA catalog as well and defining them will be part of your SOA governance standards. They are important if you want to do SOA successfully.

Namphibian
  • 12,046
  • 7
  • 46
  • 76
  • are you there man? I'm having a doubt. The company gave me an WSDL with 2 targetNameSpaces. Which one do I use to create the QName? – Tito Jan 08 '16 at 10:53
  • 2
    does the URI assigned to `namespace` have to be a valid URI visible on the network? – amphibient Feb 27 '18 at 21:15
  • 2
    @amphibient it has to be a valid URL in syntax only. It does not need to be visible on the network. – Namphibian Feb 27 '18 at 23:47
13

I'll explain using Java analogy.

Namespace is like Java packages. Each xml element is in a namespace. Providing a targetNamespace means that all elements (and types) defined within are in that namespace. It's similar to that all Java classes are within the package.

The xsd being imported will have its own target namespace that would mean that all elements in the xsd will be in the namespace defined.

The import in the wsdl is like providing a java import specifying the package.

I hope I am clear and helpful :D.

Premraj
  • 72,055
  • 26
  • 237
  • 180
sashwat
  • 607
  • 4
  • 10
9
  • (source) namespace is like package declaration in java i.e. package com.exampl, used in schema creation.
  • targetNamespace is like package import in java, this is generally use for re-using one schema inside another schema.

targetNamespace is an XML Schema "artifact".User-defined data types may have possibility of name clashes when we work with different team. This an attribute of schema element defines namespace i.e. package. By convention we use URI/URLs, but we could use any string..

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

for example:

xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/beans/spring-mvc.xsd" xmlns:mvc="http://www.springframework.org/schema/mvc"
  //  or if the schema  exist in current directory we can declare as follows
xsi:schemaLocation="http://www.springframework.org/schema/mvc spring-mvc.xsd" xmlns:mvc="http://www.springframework.org/schema/mvc"
 //for import xml schema
<xsd:import schemaLocation="http://localhost:9999/ws/hello?xsd=1" namespace="http://ws.peter.com/"/>

Note: xsi:schemaLocation mean the namespace and URL are separated with whitespace. And xmlns:mvc mean the namespace http://www.springframework.org/schema/mvc define as mvc alias.

namespace analogy:

+---------+--------------------------------------------------------+------------------------------+------------------------+
| Context |                          Name                          |     Namespace identifier     |       Local name       |
+---------+--------------------------------------------------------+------------------------------+------------------------+
| Path    | /home/user/readme.txt                                  | /home/user (path)            | readme.txt (file name) |
| XML     | xmlns:xhtml="http://www.w3.org/1999/xhtml"<xhtml:body> | http://www.w3.org/1999/xhtml | body                   |
| Java    | java.util.Date                                         | java.util                    | Date                   |
+---------+--------------------------------------------------------+------------------------------+------------------------+ 

for more details

Premraj
  • 72,055
  • 26
  • 237
  • 180
  • Excellent! Thanx for this clarification of namespaces. So, I will just place the *WSDL and all the *.XSD files to the current workdir of the php script. Hopefully the SoapClient will find them there by those local names ;) – Filip OvertoneSinger Rydlo Sep 09 '16 at 18:13