my recent reads..

Registering namespaces with SOAP::Lite


About time to post about something ... too many OpEd pieces of late!

An interesting question came up on the soaplite mailing list concerning how to modify the namespaces registered in the SOAP envelope. Documentation is not especially clear on this point.

Of course, a quick hack is to attach a full namespace to an element directly, as in:
SOAP::Data->name('itemName')->attr({'xmlns:mns' => 'http://my.namespace'})

With version SOAP::Lite 0.65 and above, the register_ns serializer method helps to correctly construct the envelope, as shown in the following example:
#!/usr/bin/perl -w
# $Id$
#

use strict;

#SOAP::serializer->register_ns requires 0.65
use SOAP::Lite 0.65 +trace => 'debug';

my $soap = SOAP::Lite
->proxy( 'http://localhost/blah/DummyService' );

my $serializer = $soap->serializer();
$serializer->register_ns( 'http://my.namespace', 'mns' );

my $som = $soap->call(
SOAP::Data
->name('mns:test')
=> SOAP::Data->name('mns:description' => 'an item in my namespace')->type('mns:mytype')
);

This generates the following SOAP request:
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:mns="http://my.namespace"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<mns:test>
<mns:description xsi:type="mns:mytype">an item in my namespace</mns:description>
</mns:test>
</soap:Body>
</soap:Envelope>