< previous
index

SOAP without WSDL

next >

[describing XML::Compile 0.53, 2007/08/20]
It is a little bit more work to use SOAP without having a WSDL file; you will have to define the stucture of the messages yourself.

   my $soap      = XML::Compile::SOAP::SOAP11->new;
   my $NS        = 'http://example.com/stockquote/schemas';

   my $get_price = $soap->compile
     ( 'CLIENT', 'INPUT'
     , header => [ transaction => "{$NS}Transaction" ]
     , body   => [ request     => "{$NS}GetLastTradePrice" ]
     , mustUnderstand => 'transaction'
     , destination    => [ transaction => 'NEXT http://actor' ]
     );

The transaction and request labels are chosen freely: some way or an other, the user must be able to connect data items to message components.

The returned CODE reference will be used on the CLIENT. For the server, it is an INPUT message, which means that a writer is created. The mustUnderstand and destination are optional.

Now the application:

   my %data_in =
     ( transaction => 5
     , request     => {symbol => 'DIS'}
     );

   # create a XML::LibXML tree
   my $xml  = $get_price->(\%data_in, 'UTF-8');
   print $xml->toString;

The result is:

   <SOAP-ENV:Envelope
      xmlns:x0="http://example.com/stockquote/schemas"
      xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
     <SOAP-ENV:Header>
       <x0:Transaction
         mustUnderstand="1"
         actor="http://schemas.xmlsoap.org/soap/actor/next http://actor">
           5
       </x0:Transaction>
     </SOAP-ENV:Header>
     <SOAP-ENV:Body>
       <x0:GetLastTradePrice>
         <symbol>DIS</symbol>
       </x0:GetLastTradePrice>
     </SOAP-ENV:Body>
   </SOAP-ENV:Envelope>

The "parameter unpack rules" make your live a little easier:

   my $xml  = $get_price->(transaction => 5, symbol => 'DIS');
   print $xml->toString;

YAPC::EU 2007 Vienna, Presentation of XML::Compile second part, by Mark Overmeer.