0% found this document useful (0 votes)
44 views

XML Compile

This document discusses XML::Compile, a Perl module that allows programmers to work with XML messages without needing to understand XML schemas. It provides concise summaries of the key points: XML::Compile allows programmers to read and write XML messages using pure Perl data structures by avoiding the need to understand XML schemas. It supports all major schema components including elements, attributes, data types and inheritance. While schemas can be complex, XML::Compile hides this complexity and provides template generators so programmers do not need to understand the underlying schema specifications. The module represents XML messages as Perl data structures like hashes and arrays to provide a simple programming interface.

Uploaded by

Amit Medankar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

XML Compile

This document discusses XML::Compile, a Perl module that allows programmers to work with XML messages without needing to understand XML schemas. It provides concise summaries of the key points: XML::Compile allows programmers to read and write XML messages using pure Perl data structures by avoiding the need to understand XML schemas. It supports all major schema components including elements, attributes, data types and inheritance. While schemas can be complex, XML::Compile hides this complexity and provides template generators so programmers do not need to understand the underlying schema specifications. The module represents XML messages as Perl data structures like hashes and arrays to provide a simple programming interface.

Uploaded by

Amit Medankar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 30

XML::Compile::SOAP

Mark Overmeer, [email protected]


What are we doing?

XML SUCKS!
What are we doing?

XML SUCKS!

XML Schemas SUCK even more


What are we doing?

XML SUCKS!

XML Schemas SUCK even more

WSDL & SOAP SUCK the most!


XML::Compile

Avoid the need to know learn XML and Schemas: pure


Perl within your program
XML::Compile

Avoid the need to know learn XML and Schemas: pure


Perl within your program

Pure perl, compliant, complete, validating XML


message reading and writing.
XML::Compile

Avoid the need to know learn XML and Schemas: pure


Perl within your program

Pure perl, compliant, complete, validating XML


message reading and writing.

Minimal programmer interface,


maximum usability.
Reading an XML message

use XML::Compile::Schema;

my $schema = XML::Compile::Schema->new($xsdfile);
my $reader = $schema->compile(READER => '{ns}local');

my $hash = $reader->($xmlmsg);

use Data::Dumper;
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;

my $schema = XML::Compile::Schema->new($xsdfile);
my $reader = $schema->compile(READER => '{ns}local');

my $hash = $reader->($xmlmsg);

my $writer = $schema->compile(WRITER => '{ns}local');


my $doc = XML::LibXML::Document->new('1.0','UTF-8');

my $xml = $writer->($doc, $hash); # partial doc


print $xml->toString;
Compliant?

Supports:
elements
attributes
references
Compliant?

Supports:
elements
attributes
references

sequence
choice
all
group
attributeGroup
Compliant?

Also supports:
simpleType
union
list
restriction
complexType
simpleContent
extension
restriction
complexContent
extension
restriction
Compliant?

Also supports:
minOccurs/maxOccurs on elements
minOccurs/maxOccurs on blocks
fixed, default on elements and attributes
...
Compliant?

Also supports:
minOccurs/maxOccurs on elements
minOccurs/maxOccurs on blocks
fixed, default on elements and attributes
...

any
anyAttribute
substitutionGroups (!)
Compliant?

Finally:

full name-space support (hidden)

element/attribute qualified/unqualified

message validating
Oops, schemas are complex

The good things: you do not have to understand them


really.
automatic name-spaces
type structures hidden (inheritance etc)
Oops, schemas are complex

The good things: you do not have to understand them


really.
automatic name-spaces
type structures hidden (inheritance etc)
template generator:

print $schema->template(PERL => $type);


print $schema->template(XML => $type);
Oops, schemas are complex

The good things: you do not have to understand them


really.
automatic name-spaces
type structures hidden (inheritance etc)
template generator

The bad things: current limitations


only name-space based schemas
mixed content only via hooks
schemas themselves not validated
you need a schema to use the module
SimpleType

Schema:
<element name=”a” type=”int” />

XML message:
<a>42</a>

Perl:
a => 42
complexType

Schema:
<complexType name=”a”>
<sequence>
<element name=”b” type=”int” />
<element name=”c” type=”int” />
</sequence>
</complexType>
<element name=”d” type=”tns:a” />

XML message:
<d><b>42</b><c>11</c></d>

Perl:
d => { b => 42, c => 11 }
Attributes

Schema:
<complexType name=”a”>
<sequence>
<element name=”b” type=”int” />
</sequence>
<attribute name=”c” type=”int” />
</complexType>
<element name=”d” type=”tns:a” />

XML message:
<d c=”34”><b>12</b></d>

Perl:
d => { b => 12, c => 34 }
complexType/simpleContent

Schema:
<complexType name=”a”>
<simpleContent>
<extension base=”string” />
</simpleContent>
<attribute name=”c” type=”int” />
</complexType>
<element name=”d” type=”tns:a” />

XML message:
<d c=”7”>YAPC</d>

Perl:
d => { _ => 'YAPC', c => 7 }
complexType/complexContent

Schema:
<complexType name=”monger”>
<complexContent>
<extension base=”person” />
</complexContent>
<attribute name=”nick” type=”string” />
</complexType>
<element name=”monger” type=”tns:monger” />

XML message:
<monger nick=”Nick”><name>Nick</name></monger>

Perl:
monger => { nick => 'Nick', name => 'Nick' }
element maxOccurs > 1

Schema:
<element name=”ticket” maxOccurs=”unbounded” />

XML message:
<ticket>123</ticket>
<ticket>324</ticket>

Perl:
ticket => [ 123, 324 ]

undef or ARRAY, always!


element maxOccurs > 1

undef or ARRAY:
ticket => undef
ticket => [ 42 ]
ticket => [ 3, 4, 5]

so:
if(my $t = $data->{ticket})
{ print “tickets: @$t\n”;
}

other XML modules:


if(my $t = $data->{ticket})
{ my @t = ref $t eq 'ARRAY' ? @$t : $t;
print “tickets: @t\n”;
}
and so on...

list: count => [ 'one', 'two' ]

union: limit => 'unbounded'

any: '{ns}local' => XML::LibXML::Element

anyAttribute '{ns}local' => XML::LibXML::Attr

... all simply a SCALAR, HASH, or ARRAY


except...
block maxOccurs > 1

Schema:
<sequence maxOccurs=”unbounded”>
<element name=”a” type=”int” />
<element name=”b” type=”int” />
</sequence>

XML message:
<a>3</a><b>4</b>
<a>5</a><b>6</b>

Perl: ARRAY of HASHes


seq_a => [ {a => 3, b => 4}
, {a => 5, b => 6} ]
SOAP

Pass messages over internet.

Payload (all XML)


Header for delivery
Body with payload
Envelope to wrap it up

Transport (in application)


Query - Answer relation
Protocol (HTTP)
End-point (server, destination)
SOAP

Two kinds of SOAP:

“Document”
well defined body
requires longer Schemas

“XML-RPC”
interface quick and dirty: no types defined, comes from the code
(in Perl not, do problematic)
SOAP::Lite's special
Discouraged in SOAP1.2
WSDL

Group all information required for a SOAP transaction


into one XML structure, both
message structure and
transport details

You thought that Schemas were overly complicated?


Well...

Demo time! Switching to browser pages.

You might also like