[vox-tech] Fwd: Re: complex XML to CSV via XSLT
Bill Kendrick
nbs at sonic.net
Wed Jul 14 20:31:57 PDT 2010
And again
-bill!
----- Forwarded message from vox-tech-bounces at lists.lugod.org -----
Date: Wed, 14 Jul 2010 17:38:18 -0700
From: vox-tech-bounces at lists.lugod.org
Subject: Auto-discard notification
To: vox-tech-owner at lists.lugod.org
The attached message has been automatically discarded.
Date: Wed, 14 Jul 2010 16:43:24 -0700
From: Dylan Beaudette <debeaudette at ucdavis.edu>
Subject: Re: [vox-tech] complex XML to CSV via XSLT
To: vox-tech at lists.lugod.org
Cc: Wes Hardaker <wjhns156 at hardakers.net>
Reply-To: debeaudette at ucdavis.edu
Wow. Pretty neat. Looks like that almost works. It does not work when there
are several 'pedons' in the collection.
Ideas?
Dylan
On Wednesday 14 July 2010, Wes Hardaker wrote:
> >>>>> 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
--
Dylan Beaudette
Soil Resource Laboratory
http://casoilresource.lawr.ucdavis.edu/
University of California at Davis
530.754.7341
----- End forwarded message -----
--
-bill!
Sent from my computer
More information about the vox-tech
mailing list