On Tue Jan 29 14:09:07 2008, Mark@Overmeer.net wrote:
Show quoted text> * Slaven_Rezic via RT (bug-XML-Compile@rt.cpan.org) [080129 16:25]:
> > Tue Jan 29 11:25:28 2008: Request 32775 was acted upon.
> > Transaction: Ticket created by SREZIC
> > Queue: XML-Compile
> > Subject: Make TYPE optional when compiling readers
> >
> > Is it possible to make the TYPE parameter optional when creating a
> > READER with XML::Compile::Schema::compile? I feel that it might be
> > possible to have a sensible default for the majority of existing
> > schemas. If the XML file to be parsed is given, then getting TYPE is
easy:
Show quoted text>
> In XML, you have always only one root element.
> Although I think I understand the benefits, I have some doubts. For one,
> many programs will use many name-spaces. By accident, you may start the
> wrong type if the program does not specify it explictly.
>
> The advantange releaving users from specifying the namespace, when just
> before that this namespace must be provided to load the schema anyway, is
> not so big.
> my $ns = ...
> my $schema = XML::Compile::Schema->new($ns, ...);
> my $reader = $schema->compile(READER => pack_type($ns, 'local'), ...)
> About 10 chars...
> And then, we also have schemas without namespace. Hum. I don't know.
>
> On the other hand, it would be nice to extend your suggestion into the
> extreme: an "i feel lucky" XML decoder: pass any piece of XML, and get
> it decoded according to the rules (validated) It has a few advantanges
> over XML::Simple and friends.
Yes. Without a schema it is simply not possible to convert XML into a
data structure with zero effort.
Show quoted text> > By the way, I was able to create a small script named xml2yml which does
> > exactly what the filename says in about 20 lines using XML::Compile.
> > Very nifty.
>
> I would really like to include that code in some distro... you may
> remember my lightningtalk ("I hate XML, I love YAML") at the GPW: syntax
> doesn't matter, structure does. (yaml2xml a.yml)->validate(schema.xsd)
Here it is:
#!perl
use XML::LibXML;
use XML::Compile::Schema;
use YAML;
my $xmlfile = shift or die "xml file?";
my $xsdfile = shift or die "schema file (xsd)?";
my $roottag = shift;
my $p = XML::LibXML->new;
my $doc = $p->parse_file($xmlfile);
if (!$roottag) {
my $root = $doc->documentElement;
$roottag = '{' . $root->namespaceURI . '}' . $root->localname;
}
my $schema = XML::Compile::Schema->new($xsdfile);
my $read = $schema->compile(READER => $roottag, sloppy_integers => 1);
my $data = $read->($doc);
print YAML::Dump($data);
__END__
I used sloppy_integers because the BigInts looked ugly in the YAML
output. Room for configuration options, I think.
Regards,
Slaven