|
Packit |
d27c7e |
=head1 Using PatAct Modules
|
|
Packit |
d27c7e |
|
|
Packit |
d27c7e |
This document is targeted towards people who want to write scripts or
|
|
Packit |
d27c7e |
modules that use pattern and action modules. If you want to create a
|
|
Packit |
d27c7e |
new pattern or action module, please see ``Creating PatAct Modules.''
|
|
Packit |
d27c7e |
|
|
Packit |
d27c7e |
You would want to use pattern/action modules if you want to apply a
|
|
Packit |
d27c7e |
complex set of patterns or queries against an XML instance and perform
|
|
Packit |
d27c7e |
actions associated with those patterns or queries. To be able to use
|
|
Packit |
d27c7e |
pattern/action modules you will need a pattern-matching module that
|
|
Packit |
d27c7e |
supports the format of the pattern or query language you can use and
|
|
Packit |
d27c7e |
an action module that will perform the types of actions you need to
|
|
Packit |
d27c7e |
perform.
|
|
Packit |
d27c7e |
|
|
Packit |
d27c7e |
Available pattern-matching modules are:
|
|
Packit |
d27c7e |
|
|
Packit |
d27c7e |
XML::PatAct::
|
|
Packit |
d27c7e |
::MatchName Simple element name, element hierarchy matching
|
|
Packit |
d27c7e |
|
|
Packit |
d27c7e |
Available action modules are:
|
|
Packit |
d27c7e |
|
|
Packit |
d27c7e |
XML::PatAct::
|
|
Packit |
d27c7e |
::ToObjects Convert XML instances into Perl objects
|
|
Packit |
d27c7e |
::Amsterdam Simplistic style-sheet using before/after strings
|
|
Packit |
d27c7e |
|
|
Packit |
d27c7e |
Using pattern/action modules involves loading the modules, creating a
|
|
Packit |
d27c7e |
pattern/action list, creating instances of the pattern and matching
|
|
Packit |
d27c7e |
modules, and then starting a parse using the matching module as a
|
|
Packit |
d27c7e |
handler:
|
|
Packit |
d27c7e |
|
|
Packit |
d27c7e |
use XML::Parser::PerlSAX;
|
|
Packit |
d27c7e |
use XML::PatAct::MatchName;
|
|
Packit |
d27c7e |
use XML::PatAct::ToObjects;
|
|
Packit |
d27c7e |
|
|
Packit |
d27c7e |
my $patterns = [
|
|
Packit |
d27c7e |
'schema' => [ qw{ -holder } ],
|
|
Packit |
d27c7e |
'table' => [ qw{ -make Schema::Table } ],
|
|
Packit |
d27c7e |
'name' => [ qw{ -field Name -as-string } ],
|
|
Packit |
d27c7e |
];
|
|
Packit |
d27c7e |
|
|
Packit |
d27c7e |
my $matcher = XML::PatAct::MatchName->new( Patterns => $patterns );
|
|
Packit |
d27c7e |
my $handler = XML::PatAct::ToObjects->new( Patterns => $patterns,
|
|
Packit |
d27c7e |
Matcher => $matcher);
|
|
Packit |
d27c7e |
|
|
Packit |
d27c7e |
my $parser = XML::Parser::PerlSAX->new( Handler => $handler );
|
|
Packit |
d27c7e |
my $schema = $parser->parse(Source => { SystemId => $ARGV[0] } );
|
|
Packit |
d27c7e |
|
|
Packit |
d27c7e |
The example above use the MatchName and ToObjects pattern and action
|
|
Packit |
d27c7e |
modules. The pattern list contains pairs of patterns and actions in
|
|
Packit |
d27c7e |
the format specified by MatchName and ToObjects, other modules will
|
|
Packit |
d27c7e |
use other formats. The patterns that MatchName supports are a simple
|
|
Packit |
d27c7e |
element name or a hierarchy of element names. The actions that
|
|
Packit |
d27c7e |
ToObjects support describe how to create Perl objects from the XML
|
|
Packit |
d27c7e |
instances.
|
|
Packit |
d27c7e |
|
|
Packit |
d27c7e |
The $matcher object is an instance of XML::PatAct::MatchName.
|
|
Packit |
d27c7e |
$matcher is created and associated with the pattern/action list that
|
|
Packit |
d27c7e |
will be matched against. The $handler object is an instance of
|
|
Packit |
d27c7e |
XML::PatAct::ToObjects. $handler is created and associated with the
|
|
Packit |
d27c7e |
pattern/action list to be matched against as well as the pattern
|
|
Packit |
d27c7e |
matching instance $matcher.
|
|
Packit |
d27c7e |
|
|
Packit |
d27c7e |
$handler is a PerlSAX event handler. XML::Parser::PerlSAX is used as
|
|
Packit |
d27c7e |
the source of XML events. Other PerlSAX event generators include
|
|
Packit |
d27c7e |
XML::Grove::PerlSAX and XML::ESISParser. $parser is created with the
|
|
Packit |
d27c7e |
$handler object as it's Handler.
|
|
Packit |
d27c7e |
|
|
Packit |
d27c7e |
The `parse()' method of $parser is called to run the handler (the
|
|
Packit |
d27c7e |
matching object) to produce the output from XML::PatAct::ToObjects,
|
|
Packit |
d27c7e |
which is a Perl object converted from XML, $schema.
|
|
Packit |
d27c7e |
|
|
Packit |
d27c7e |
The above example is an abbrieviated version. A complete example of
|
|
Packit |
d27c7e |
usage of the MatchName and ToObjects modules, including source XML, is
|
|
Packit |
d27c7e |
in the documentation for the XML::PatAct::ToObjects module. The
|
|
Packit |
d27c7e |
script and source XML are also in the examples directory.
|