[vox-tech] complex XML to CSV via XSLT
Wes Hardaker
wjhns156 at hardakers.net
Wed Jul 14 15:51:34 PDT 2010
>>>>> On Wed, 14 Jul 2010 11:40:35 -0700, Dylan Beaudette <debeaudette at ucdavis.edu> said:
DB> I would like to generalize some sample XSLT documents to convert the
DB> entire file into a sequence of CSV files.
Remember, that whatever you do in another language you can do in perl
with less code and more obfuscation:
use XML::Simple;
use IO::File;
use strict;
my $doc = XMLin($ARGV[0], SuppressEmpty => '', KeyAttr => 'foobarbogus');
my %iohandles;
$doc = $doc->{'pedon'};
foreach my $key (keys(%$doc)) {
if (!exists($iohandles{$key})) {
die "I'm being hacked" if ($key !~ /^[_a-zA-Z0-9]/);
$iohandles{$key} = new IO::File;
$iohandles{$key}->open(">$key.csv");
print STDERR "writing out $key.csv\n";
}
if (ref($doc->{$key}) ne 'HASH') {
next;
}
foreach my $subkey (keys(%{$doc->{$key}})) {
if (ref($doc->{$key}{$subkey}) ne 'ARRAY') {
if (ref($doc->{$key}{$subkey}) ne 'HASH') {
print STDERR "{$key}{$subkey} unexpected type\n";
last;
}
$doc->{$key}{$subkey} = [$doc->{$key}{$subkey}];
}
foreach my $record (@{$doc->{$key}{$subkey}}) {
$iohandles{$key}->print(join(",",values(%$record)),"\n");
}
}
}
# perl hack.pl pedons.xml
writing out hz_mottles.csv
writing out hz_frags.csv
writing out horizon.csv
writing out hz_roots.csv
writing out memo.csv
writing out description.csv
{description}{surface_frags_bd} unexpected type
writing out hz_pores.csv
writing out hz_pvsf.csv
writing out notes.csv
writing out hz_rmf.csv
writing out diagnostic.csv
writing out vegetation.csv
writing out pedon_spatial.csv
{pedon_spatial}{y} unexpected type
--
Wes Hardaker
My Pictures: http://capturedonearth.com/
My Thoughts: http://pontifications.hardakers.net/
More information about the vox-tech
mailing list