[Chicago-talk] Navigating an XSD

John Kristoff jtk at depaul.edu
Fri Jan 23 06:23:24 PST 2015


On Fri, Jan 23, 2015 at 05:12:06AM +0000, Jay Strauss wrote:
> I have an XSD document like below.  I've been googling and cpan-ing,
> but can't find what I need (i know there are lots of XML packages),
> and I don't know how to parse it using regexs (reliably).  I just want
> to read in this XSD and navigate it like a perl structure, and extract
> field values like:
[...]
> Can anyone recommend a module or a method?

Hi Jay.  I have found some success using XML::Twig and XML::Writer for reading
and writing respectively, for relatively simple, but large XML files.

With XML::Twig, you can give it handlers based on blocks, for instance,
you're interested in.  For example:

my $t = XML::Twig->new(
    twig_handlers => {
        'person' => \&section,
    },
);
$t->parsefile($xmlfile);

sub section {
    my ( $t, $section ) = @_;

    # interested in a few elements within this section
    do_something_with( $section->first_child_text('birthdate') );
    do_somethingelse_with( $section->first_child_text('ssn') );
    #
    # ...

    # do not need that element again
    $section->purge;

    return;
}

See if that might work for you.

John


More information about the Chicago-talk mailing list