+ All Categories
Home > Documents > XML Compile

XML Compile

Date post: 24-Jun-2015
Category:
Upload: amit-medankar
View: 41 times
Download: 4 times
Share this document with a friend
Popular Tags:
30
XML::Compile::SOA Mark Overmeer, [email protected]
Transcript
Page 1: XML Compile

XML::Compile::SOAP

Mark Overmeer, [email protected]

Page 2: XML Compile

What are we doing?

XML SUCKS!XML SUCKS!

Page 3: XML Compile

What are we doing?

XML SUCKS!XML SUCKS!

XML Schemas SUCK even moreXML Schemas SUCK even more

Page 4: XML Compile

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!

Page 5: XML Compile

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

Page 6: XML Compile

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.

Page 7: XML Compile

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.

Page 8: XML Compile

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

Page 9: XML Compile

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;

Page 10: XML Compile

Compliant?

Supports:Supports:elementselementsattributesattributesreferencesreferences

Page 11: XML Compile

Compliant?

Supports:Supports:elementselementsattributesattributesreferencesreferences

sequencesequencechoicechoiceallallgroupgroupattributeGroupattributeGroup

Page 12: XML Compile

Compliant?

Also supports:Also supports:simpleTypesimpleType

unionlistrestriction

complexTypecomplexTypesimpleContent

extensionrestriction

complexContentextensionrestriction

Page 13: XML Compile

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......

Page 14: XML Compile

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 (!)

Page 15: XML Compile

Compliant?

Finally:Finally:

full name-space support (hidden)full name-space support (hidden)

element/attribute qualified/unqualifiedelement/attribute qualified/unqualified

message validatingmessage validating

Page 16: XML Compile

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)

Page 17: XML Compile

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);

Page 18: XML Compile

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

Page 19: XML Compile

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

Page 20: XML Compile

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 }

Page 21: XML Compile

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 }

Page 22: XML Compile

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 }

Page 23: XML Compile

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' }

Page 24: XML Compile

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!

Page 25: XML Compile

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”;}}

Page 26: XML Compile

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...

Page 27: XML Compile

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} ]

Page 28: XML Compile

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)

Page 29: XML Compile

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

Page 30: XML Compile

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.


Recommended