XML Compile
XML Compile
XML Compile
XML SUCKS!
What are we doing?
XML SUCKS!
XML SUCKS!
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);
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:
element/attribute qualified/unqualified
message validating
Oops, schemas are complex
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:
ticket => undef
ticket => [ 42 ]
ticket => [ 3, 4, 5]
so:
if(my $t = $data->{ticket})
{ print “tickets: @$t\n”;
}
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>
“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