8.1.2 about namespaces
In the NS1 _ getquote function (as mentioned in the previous section), NS1 _ is used as the namespace of the remote method. Namespace is used to prevent remote method name conflicts, for example, when the same remote method name is used in multiple services.
The namespace prefix and namespace name are also used to verify the validity of the soap information. The stub routine verifies the service return information through the information in the namespace table. The namespace table is retrieved at runtime to parse the namespace binding, deserialize the data structure, decode and verify the service return information. The namespace table should not be included in the header file required by the gsoap pre-compiler. In section 18.2, The namespace table is described in detail.
The namespace table of the delayed stock quote service client is as follows:
Struct namespace namespaces [] =
{// {"Name prefix", "space name "}
{"SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/"}, // must be the first line
{"SOAP-ENC", "http://schemas.xmlsoap.org/soap/encoding/"}, // must be the second line
{"Xsi", "http://www.w3.org/2001/XMLSchema-instance"}, // must be the third line
{"XSD", "http://www.w3.org/2001/XMLSchema"}, // 2001 XML outline
{"NS1", "urn: xmethods-delayed-quotes"}, // get it by service description
{Null, null} // end
};
The namespace in the first line is the default namespace of soap1.1 protocol. In fact, a namespace table is used to allow programmers to specify the soap encoding method and meet the namespace requirements of a specified soap service by using the namespace prefix containing the namespace. For example, using the namespace prefix NS1 defined in the preceding namespace table, the stub routine can encode the request of the getquote method. This process is automatically completed by the gsoap pre-compiler through the NS1 _ getquote function that contains the prefix NS1 defined in the getquote. h file. Generally, if you want to use a namespace prefix in a remote method name, structure name, class name, field name, or other structure or class, you must define it in the namespace table.
The namespace table will be encapsulated by the stub routine and output as follows:
...
<SOAP-ENV: envelope xmlns: SOAP-ENV = "http://schemas.xmlsoap.org/soap/envelope"
Xmlns: SOAP-ENC = "http://schemas.xmlsoap.org/soap/encoding"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xmlns: XSD = "http://www.w3.org/2001/XMLSchema"
Xmlns: NS1 = "urn: xmethods-delayed-quotes"
SOAP-ENV: encodingstyle = "http://schemas.xmlsoap.org/soap/encoding/">
...
This namespace binding will be used by the soap service to verify the SOAP request.
8.1.3 example
Using the namespace prefix can solve the problem of using the same name for remote methods in different services. See the following example:
// Contents of file "getquote. H ":
Int NS1 _ getquote (char * symbol, float & result );
Int ns_getquote (char * ticker, char * & quote );
This example allows the client program to use different namespaces to connect to different service programs and execute the remote methods.
The namespace prefix can also be used in the class declaration to distinguish the soap values of the same name but different namespaces in the XML outline. For example:
Class E _ address // an electronic address
{
Char * email;
Char * URL;
};
Class S _ address // a street address
{
Char * Street;
Int number;
Char * city;
};
In the generated serialization function, an instance of E _ address is used to represent an address element type of the E namespace prefix.
<E: Address xsi: TYPE = "E: Address">
<Email xsi: TYPE = "string"> me @ Home </Email>
<URL xsi: TYPE = "string"> www.me.com </URL>
</E: Address>
An instance of S _ address is used to represent an address element type of the S namespace prefix.
<S: Address xsi: TYPE = "s: Address">
<Street xsi: TYPE = "string"> technology drive </street>
<Number xsi: TYPE = "int"> 5 </number>
<City xsi: TYPE = "string"> softcity </city>
</S: Address>
The namespace table of the client program must have the Data Type Definitions of E and S:
Struct namespace namespaces [] =
{...
{"E", "http://www.me.com/schemas/electronic-address "},
{"S", "http://www.me.com/schemas/street-address "},
...
The namespace table must be part of the client program so that the client program can serialize and deserialize the data at runtime.