XML Modules

YAPC 2003

XML Modules

Perl XML modules by example

XML modules presentation

Example description

Code examples

Conclusions

-

XML::Modules

145 XML::.* modules on May 22cd

How to choose?

Application space (data vs document)

Size of the data set

Processing Model

Quality

-

TLAs and Other Accronyms

DOM

an Object Model and an API for tree processing

SAX

an API for stream processing

Xpath

a query language for XML documents

XSLT

an XML transformation language

-

XML::Module::Map

Other Useful Modules

DBD::AnyData

XML::Generator::DBI

XML::SAX::Writer

XML::Handler::YAWriter

XML-Filter-DataIndenter

-

Document-oriented XML

<para>

Typically applications will just include the

<filename>gnome.h</filename> header file, the

<filename>libgnome.h</filename> or the

<filename>gnome-i18n.h</filename> header file and this

will define the <link linkend="gettext-macro">_</link>()

macro as an invocation to <link

linkend="gettext">gettext</link>(). In the case of

libraries, shared object modules (dynamically linked

libraries) or shared object CORBA servers (CORBA servers

implemented as shared libraries), you should define the

<emphasis>GNOME_EXPLICIT_TRANSLATION_DOMAIN</emphasis>

macro ...

</para>

Data-oriented XML

<Finvoice Version="1.0">

<SellerPartyDetails>

<SellerPartyIdentifier>0123456-7</SellerPartyIdentifier>

<SellerOrganisationName>Pullin Kala Oy</SellerOrganisationName>

<SellerOrganisationTaxCode>0123456-7</SellerOrganisationTaxCode>

<SellerPostalAddressDetails>

<SellerStreetName>Haapatie 7</SellerStreetName>

<SellerTownName>Helsinki</SellerTownName>

</SellerPostalAddressDetails>

</SellerPartyDetails>

<InvoiceRow>

<Sku>AA1</Sku><TotalPrice Currency=”Euro”>20</TotalPrice>

<OrderedQuantity OrderedQuantityUnit=”kg”>10</OrderedQUantity>

<DeliveredQuantity DeliveredQuantityUnit=”kg”>10</DeliveredQUantity>

</InvoiceRow>

<InvoiceRow>

<Sku>AA2</Sku><TotalPrice Currency=”Euro”>50</TotalPrice>

</InvoiceRow>

</Finvoice>

Data vs Doc

Text is messy, data is simpler!

Data has more structure

Data has no mixed-content

Some tools work best (or only!) for data-oriented XML

Most XML these days is data-oriented

-

Typical data-oriented XML

Standard Documents

Configuration files

Data Base dumps

Serialized objects

XML-RPC, SOAP messages

-

Typical use of data-oriented XML

XML is an EXCHANGE format

Extract data

Use Perl data structures

Put it in a Data Base

Add data

Convert the XML document

From external to internal DTD

Avoid complex XML to XML transformations

-

Six Blind Men and An Elephant

What is XML?

“XML is very like a spear”:

XML invoices

Definitely data

Small documents

Order matters (somewhat)

Exchanged as XML

The data should live in a Data Base

-

Kouralinen Dollareita

Ways To Rome (2)

http://xmltwig.com/article/ways_to_rome_2/

Contains all the code examples

Patches (+ new examples) Welcome!

Finvoice

The electronic invoice of the Finnish Bankers' Association

An exchange format for invoices

Sent by Seller to Buyer

http://www.fba.fi/finvoice/index.html

Other standards for invoices: Visa, SAP

-

Specification of the Problem

Receive an invoice

Check it (PO #, qty...)

If OK

store the relevant data in a DB

Else

# bonus credits only

add error diagnostic in document

output the updated XML

What it Amounts to

Access the data in the XML

Non-repeated data

BuyerPartyIdentifier

Repeated data

InvoiceRow

Optional data: DeliveredQuantity

TIMTOWDI

Get the data and forget about the XML

Load the XML and process it (tree-mode processing)

Process the XML as it is being parsed (stream-mode processing)

Still more ways...

-

Load'n Forget

XML::Simple, XML::Smart

Load the XML into Perl data structures

Content of an element:

$xml->{BuyerPartyDetails}->{BuyerOrganisationName}

Repeated Elements:

my @rows= @{$xml->{InvoiceRow}};

Value of an attribute:

$row->{DeliveredQuantity}->{QuantityUnitCode}

-

Tree Processing

XML::LibXML, XML::XPath, XML::DOM, XML::EasyObj, XML::Twig

Load the XML into a tree

Navigation

my $child= $node->getFirstChild; # DANGEROUS

my $child= $elt->first_child( 'InvoiceDetails')

Queries

my @rows= $node->getElementsByTagName( 'InvoiceRow');

$doc->findnodes( '/Finvoice/InvoiceRow');

$doc->findvalue( 'BuyerPartyIdentifier')

$doc->findvalue( '/Finvoice/BuyerDetails/BuyerId')

-

Stream Processing

XML::PYX, XML::SAX, XML::SAX::*

Check and fill a Perl data structure

2 passes

Use chained SAX filters to separate checking and loading the data

-

TIMT3WTDI

HTML::TreeBuilder

Regexps (shame on you!)

XSLT

-

Other issues

Validation

Preliminary step

Encodings

Welcome to the wonderful world of Unicode

-

Conclusion

Data-oriented XML can be very easy to process

Very little XML-specific code

Trees are Great

Have Fun!

-

Resources

The Perl-XML FAQ

http://perl-xml.sourceforge.net/faq/

XML.com Perl-XML articles

http://xml.com/pub/q/perlxml

The perl-xml mailing list

http://listserv.activestate.com/mailman/listinfo/perl-xml

The XML Cover Pages

http://www.oasis-open.org/cover/sgml-xml.html

The annotated XML specification

http://xml.com/axml/axml.html

Zvon.org

http://zvon.org

Michel Rodriguez

2003-05-29T23:56:36