The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
#!/home/satfoto/bin/perl

use warnings;
use strict;

use XML::Compile::Schema ();
use XML::Compile::Util   qw/type_of_node/;
use Getopt::Long         qw/GetOptions :config gnu_compat bundling/;

my ($xml_input, $root_type, @schemas, $outfile);
my $format = 'PERL';
my $show   = 'ALL';

GetOptions
   'format|f=s' => \$format
 , "output|o=s" => \$outfile
 , "schema|s=s" => \@schemas
 , "show=s"     => \$show
 , "type|t=s"   => \$root_type
 , "xml|x=s"    => \$xml_input;

$xml_input = '-' if @schemas && !defined $xml_input;

if(@ARGV)
{   die "ERROR: either use options or no options, not mixed\n"
        if defined $xml_input && @ARGV;
    ($xml_input, @schemas) = @ARGV;
}

defined $xml_input
    or die "ERROR: no input message specified\n";

@schemas
    or die "ERROR: no schema's specified\n";
@schemas   = map { split /\,/ } @schemas;

$format = uc $format;
die "ERROR: format must be either 'PERL' or 'XML'\n"
    if $format ne 'PERL' && $format ne 'XML';

my $parser = XML::LibXML->new;

my $msg    = $xml_input eq '-'
           ? $parser->parse_fh(\*STDIN)
           : $parser->parse_file($xml_input);

my $top = $msg->documentElement;
$root_type ||= type_of_node $top;

my $schema = XML::Compile::Schema->new;
$schema->importDefinitions($_) for @schemas;

my $output = $schema->template
  ( $format
  , $root_type
  , show => $show
  );

if($outfile)
{   open OUT, ">:utf8", $outfile
        or die "ERROR: cannot write yaml to $outfile: $!\n";

    print OUT $output;
    close OUT
        or die "ERROR: write error for $outfile: $!\n";
}
else
{   print $output;
}

exit 0;

__END__

=head1 NAME

schema2example - convert XML schema knowledge into Perl or XML examples

=head1 SYNOPSIS

 schema2example xml-file schema-file(s)  >outfile

 schema2example -x xml-file -s schema-file(s) -o outfile

=head1 DESCRIPTION

XML schemas are quite hard to read, certainly when multiple name-spaces
are involved.  The template() function in XML::Compile::Schema function
can help displaying the expected structure of a message; this module is
a wrapper around that function.

=head2 Options

You can either specify an XML message filename and one or more
schema filenames as arguments, or use the options.

=over 4

=item --xml|-x filename

The file which contains the xml message.  A single dash means "stdin".

=item --schema|-s filename(s)

This option can be repeated, or the filenames separated by comma's, if
you have more than one schema file to parse.  All imported and included
schema components have to be provided explicitly.

=item --type|-t TYPE

The type of the root element, required if the XML is not namespaceo
qualified, although the schema is.  If not specified, the root element
is automatically inspected.

The TYPE notation is C<{namespace}localname>.  Be warned to use quoting
on the UNIX command-line, because curly braces have a special meaning
for the shell.

=item --output|-o filename

By default, the output is to stdout.

=item --show STRING

A comma seperated list of comment components which should be included,
by default C<ALL>.  An empty string or C<NONE> will exclude all comments.
The STRING can also be a comma separated combination of C<struct>, C<type>,
C<occur>, and C<facets>.

=back

=head1 SEE ALSO

This module is part of Perl's XML-Compile distribution.
Website: F<http://perl.overmeer.net/xml-compile/>

=head1 LICENSE

Copyrights 2008 by Mark Overmeer. For other contributors
see ChangeLog.

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
See F<http://www.perl.com/perl/misc/Artistic.html>