Post on 24-Jun-2015
transcript
XML::Compile::SOAP
Mark Overmeer, perl@overmeer.net
What are we doing?
XML SUCKS!XML SUCKS!
What are we doing?
XML SUCKS!XML SUCKS!
XML Schemas SUCK even moreXML Schemas SUCK even more
What are we doing?
XML SUCKS!XML SUCKS!
XML Schemas SUCK even moreXML Schemas SUCK even more
WSDL & SOAP SUCK the most!WSDL & SOAP SUCK the most!
XML::Compile
Avoid the need to know learn XML and Schemas: pure Avoid the need to know learn XML and Schemas: pure Perl within your programPerl within your program
XML::Compile
Avoid the need to know learn XML and Schemas: pure Avoid the need to know learn XML and Schemas: pure Perl within your programPerl within your program
Pure perl, compliant, complete, validating XML Pure perl, compliant, complete, validating XML message reading and writing.message reading and writing.
XML::Compile
Avoid the need to know learn XML and Schemas: pure Avoid the need to know learn XML and Schemas: pure Perl within your programPerl within your program
Pure perl, compliant, complete, validating XML Pure perl, compliant, complete, validating XML message reading and writing.message reading and writing.
Minimal programmer interface,Minimal programmer interface,maximum usability.maximum usability.
Reading an XML message
use XML::Compile::Schema;use XML::Compile::Schema;
my $schema = XML::Compile::Schema->new($xsdfile);my $schema = XML::Compile::Schema->new($xsdfile);my $reader = $schema->compile(READER => 'my $reader = $schema->compile(READER => '{ns}local{ns}local');');
my $hash = $reader->($xmlmsg);my $hash = $reader->($xmlmsg);
use Data::Dumper;use Data::Dumper;print Dumper $hash;print Dumper $hash;
{http://www.w3.org/2001/XMLSchema}int{ schema name-space: any IRI }localName
Reading and Writing an XML message
use XML::Compile::Schema;use XML::Compile::Schema;
my $schema = XML::Compile::Schema->new($xsdfile);my $schema = XML::Compile::Schema->new($xsdfile);my $reader = $schema->my $reader = $schema->compile(READERcompile(READER => '{ns}local'); => '{ns}local');
my $hash = $reader->($xmlmsg);my $hash = $reader->($xmlmsg);
my $writer = $schema->my $writer = $schema->compile(WRITERcompile(WRITER => '{ns}local'); => '{ns}local');my $doc = XML::LibXML::Document->new('1.0','UTF-8');my $doc = XML::LibXML::Document->new('1.0','UTF-8');
my $xml = $writer->($doc, $hash); # partial docmy $xml = $writer->($doc, $hash); # partial docprint $xml->toString;print $xml->toString;
Compliant?
Supports:Supports:elementselementsattributesattributesreferencesreferences
Compliant?
Supports:Supports:elementselementsattributesattributesreferencesreferences
sequencesequencechoicechoiceallallgroupgroupattributeGroupattributeGroup
Compliant?
Also supports:Also supports:simpleTypesimpleType
unionlistrestriction
complexTypecomplexTypesimpleContent
extensionrestriction
complexContentextensionrestriction
Compliant?
Also supports:Also supports:minOccurs/maxOccurs on elementsminOccurs/maxOccurs on elementsminOccurs/maxOccurs on blocksminOccurs/maxOccurs on blocksfixed, default on elements and attributesfixed, default on elements and attributes......
Compliant?
Also supports:Also supports:minOccurs/maxOccurs on elementsminOccurs/maxOccurs on elementsminOccurs/maxOccurs on blocksminOccurs/maxOccurs on blocksfixed, default on elements and attributesfixed, default on elements and attributes......
anyanyanyAttributeanyAttributesubstitutionGroups (!)substitutionGroups (!)
Compliant?
Finally:Finally:
full name-space support (hidden)full name-space support (hidden)
element/attribute qualified/unqualifiedelement/attribute qualified/unqualified
message validatingmessage validating
Oops, schemas are complex
The good things: you do not have to understand them The good things: you do not have to understand them really.really.
automatic name-spacesautomatic name-spacestype structures hidden (inheritance etc)type structures hidden (inheritance etc)
Oops, schemas are complex
The good things: you do not have to understand them The good things: you do not have to understand them really.really.
automatic name-spacesautomatic name-spacestype structures hidden (inheritance etc)type structures hidden (inheritance etc)template generator:template generator:
print $schema->template(PERL => $type);print $schema->template(PERL => $type);print $schema->template(XML => $type);print $schema->template(XML => $type);
Oops, schemas are complex
The good things: you do not have to understand them The good things: you do not have to understand them really.really.
automatic name-spacesautomatic name-spacestype structures hidden (inheritance etc)type structures hidden (inheritance etc)template generatortemplate generator
The bad things: current limitationsThe bad things: current limitationsonly name-space based schemasonly name-space based schemasmixed content only via hooksmixed content only via hooksschemas themselves not validatedschemas themselves not validatedyou need a schema to use the moduleyou need a schema to use the module
SimpleType
Schema:Schema:<element name=”a” type=”int” /><element name=”a” type=”int” />
XML message:XML message:<a>42</a><a>42</a>
Perl:Perl:a => 42a => 42
complexType
Schema:Schema:<complexType name=”a”><complexType name=”a”> <sequence> <sequence> <element name=”b” type=”int” /> <element name=”b” type=”int” /> <element name=”c” type=”int” /> <element name=”c” type=”int” /> </sequence> </sequence></complexType></complexType><element name=”d” type=”tns:a” /><element name=”d” type=”tns:a” />
XML message:XML message:<d><b>42</b><c>11</c></d><d><b>42</b><c>11</c></d>
Perl:Perl:d => { b => 42, c => 11 }d => { b => 42, c => 11 }
Attributes
Schema:Schema:<complexType name=”a”><complexType name=”a”> <sequence> <sequence> <element name=”b” type=”int” /> <element name=”b” type=”int” /> </sequence> </sequence> <attribute name=”c” type=”int” /> <attribute name=”c” type=”int” /></complexType></complexType><element name=”d” type=”tns:a” /><element name=”d” type=”tns:a” />
XML message:XML message:<d c=”34”><b>12</b></d><d c=”34”><b>12</b></d>
Perl:Perl:d => { b => 12, c => 34 }d => { b => 12, c => 34 }
complexType/simpleContent
Schema:Schema:<complexType name=”a”><complexType name=”a”> <simpleContent> <simpleContent> <extension base=”string” /> <extension base=”string” /> </simpleContent> </simpleContent> <attribute name=”c” type=”int” /> <attribute name=”c” type=”int” /></complexType></complexType><element name=”d” type=”tns:a” /><element name=”d” type=”tns:a” />
XML message:XML message:<d c=”7”>YAPC</d><d c=”7”>YAPC</d>
Perl:Perl:d => { _ => 'YAPC', c => 7 }d => { _ => 'YAPC', c => 7 }
complexType/complexContent
Schema:Schema:<complexType name=”monger”><complexType name=”monger”> <complexContent> <complexContent> <extension base=”person” /> <extension base=”person” /> </complexContent> </complexContent> <attribute name=”nick” type=”string” /> <attribute name=”nick” type=”string” /></complexType></complexType><element name=”monger” type=”tns:monger” /><element name=”monger” type=”tns:monger” />
XML message:XML message:<monger nick=”Nick”><name>Nick</name></monger><monger nick=”Nick”><name>Nick</name></monger>
Perl:Perl:monger => { nick => 'Nick', name => 'Nick' }monger => { nick => 'Nick', name => 'Nick' }
element maxOccurs > 1
Schema:Schema:<element name=”ticket” maxOccurs=”unbounded” /><element name=”ticket” maxOccurs=”unbounded” />
XML message:XML message:<ticket>123</ticket><ticket>123</ticket><ticket>324</ticket><ticket>324</ticket>
Perl:Perl:ticket => [ 123, 324 ]ticket => [ 123, 324 ]
undef or ARRAY, always!undef or ARRAY, always!
element maxOccurs > 1
undef or ARRAY:undef or ARRAY:ticket => undefticket => undefticket => [ 42 ]ticket => [ 42 ]ticket => [ 3, 4, 5]ticket => [ 3, 4, 5]
so:so:if(my $t = $data->{ticket})if(my $t = $data->{ticket}){ print “tickets: @$t\n”;{ print “tickets: @$t\n”;}}
other XML modules:other XML modules:if(my $t = $data->{ticket})if(my $t = $data->{ticket}){ my @t = ref $t eq 'ARRAY' ? @$t : $t;{ my @t = ref $t eq 'ARRAY' ? @$t : $t; print “tickets: @t\n”; print “tickets: @t\n”;}}
and so on...
list:list: count => [ 'one', 'two' ]count => [ 'one', 'two' ]
union:union: limit => 'unbounded'limit => 'unbounded'
any:any: '{ns}local' => XML::LibXML::Element'{ns}local' => XML::LibXML::Element
anyAttributeanyAttribute '{ns}local' => XML::LibXML::Attr'{ns}local' => XML::LibXML::Attr
... all simply a SCALAR, HASH, or ARRAY... all simply a SCALAR, HASH, or ARRAYexcept...except...
block maxOccurs > 1
Schema:Schema:<sequence maxOccurs=”unbounded”><sequence maxOccurs=”unbounded”> <element name=”a” type=”int” /> <element name=”a” type=”int” /> <element name=”b” type=”int” /> <element name=”b” type=”int” /></sequence></sequence>
XML message:XML message:<a>3</a><b>4</b><a>3</a><b>4</b><a>5</a><b>6</b><a>5</a><b>6</b>
Perl: ARRAY of HASHesPerl: ARRAY of HASHesseq_a => [ {a => 3, b => 4}seq_a => [ {a => 3, b => 4} , {a => 5, b => 6} ] , {a => 5, b => 6} ]
SOAP
Pass messages over internet.Pass messages over internet.
Payload (all XML)Payload (all XML)Header for deliveryHeader for deliveryBody with payloadBody with payloadEnvelope to wrap it upEnvelope to wrap it up
Transport (in application)Transport (in application)Query - Answer relationQuery - Answer relationProtocol (HTTP)Protocol (HTTP)End-point (server, destination)End-point (server, destination)
SOAP
Two kinds of SOAP:Two kinds of SOAP:
““Document”Document”well defined bodyrequires longer Schemas
““XML-RPC”XML-RPC”interface quick and dirty: no types defined, comes from the code (in Perl not, do problematic)SOAP::Lite's specialDiscouraged in SOAP1.2
WSDL
Group all information required for a SOAP transaction Group all information required for a SOAP transaction into one XML structure, bothinto one XML structure, both
message structure andmessage structure andtransport detailstransport details
You thought that Schemas were overly complicated? You thought that Schemas were overly complicated? Well...Well...
Demo time! Switching to browser pages.Demo time! Switching to browser pages.