l i n u x - u s e r s - g r o u p - o f - d a v i s
L U G O D
 
Next Meeting:
July 21: Defensive computing: Information security for individuals
Next Installfest:
TBD
Latest News:
Jul. 4: July, August and September: Security, Photography and Programming for Kids
Page last updated:
2010 Jul 14 20:31

The following is an archive of a post made to our 'vox-tech mailing list' by one of its subscribers.

Report this post as spam:

(Enter your email address)
Re: [vox-tech] complex XML to CSV via XSLT
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [vox-tech] complex XML to CSV via XSLT



>>>>> On Wed, 14 Jul 2010 11:40:35 -0700, Dylan Beaudette <debeaudette@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/
_______________________________________________
vox-tech mailing list
vox-tech@lists.lugod.org
http://lists.lugod.org/mailman/listinfo/vox-tech



LinkedIn
LUGOD Group on LinkedIn
Sign up for LUGOD event announcements
Your email address:
facebook
LUGOD Group on Facebook
'Like' LUGOD on Facebook:

Hosting provided by:
Sunset Systems
Sunset Systems offers preconfigured Linux systems, remote system administration and custom software development.

LUGOD: Linux Users' Group of Davis
PO Box 2082, Davis, CA 95617
Contact Us

LUGOD is a 501(c)7 non-profit organization
based in Davis, California
and serving the Sacramento area.
"Linux" is a trademark of Linus Torvalds.

Sponsored in part by:
EDGE Tech Corp.
For donating some give-aways for our meetings.