Skip Menu |

This queue is for tickets about the XML-Compile CPAN distribution.

Report information
The Basics
Id: 32775
Status: resolved
Priority: 0/
Queue: XML-Compile

People
Owner: Nobody in particular
Requestors: SREZIC [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: 0.65
Fixed in: (no value)



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: my $p = XML::LibXML->new; my $doc = $p->parse_file($xmlfile); my $root = $doc->documentElement; $roottag = '{' . $root->namespaceURI . '}' . $root->localname; I don't know much xsd, but it seems that there's no notation to mark a "root element" (BTW, in Relax NG this is possible). But is is safe enough to assume the first element in the xsd to be the root element? Or to build a tree out of the xsd spec and determine the root element of this tree? 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. Regards, Slaven
Subject: Re: [rt.cpan.org #32775] Make TYPE optional when compiling readers
Date: Tue, 29 Jan 2008 20:08:46 +0100
To: Slaven_Rezic via RT <bug-XML-Compile [...] rt.cpan.org>
From: Mark Overmeer <mark [...] overmeer.net>
* Slaven_Rezic via RT (bug-XML-Compile@rt.cpan.org) [080129 16:25]: Show quoted text
> 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:
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. 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) -- Regards, MarkOv ------------------------------------------------------------------------ Mark Overmeer MSc MARKOV Solutions Mark@Overmeer.net solutions@overmeer.net http://Mark.Overmeer.net http://solutions.overmeer.net
Subject: Re: [rt.cpan.org #32775] Make TYPE optional when compiling readers
Date: Tue, 29 Jan 2008 22:03:16 +0100
To: Slaven_Rezic via RT <bug-XML-Compile [...] rt.cpan.org>
From: Mark Overmeer <solutions [...] overmeer.net>
* Slaven_Rezic via RT (bug-XML-Compile@rt.cpan.org) [080129 16:25]: Show quoted text
> my $p = XML::LibXML->new; > my $doc = $p->parse_file($xmlfile); > my $root = $doc->documentElement; > $roottag = '{' . $root->namespaceURI . '}' . $root->localname;
Oh, my answer is missing one point, which should be mentioned to understand it: a schema can be (is even default) elementQualified="false" which mean that the root element does not have a name-space qualification either. -- Regards, MarkOv ------------------------------------------------------------------------ Mark Overmeer MSc MARKOV Solutions Mark@Overmeer.net solutions@overmeer.net http://Mark.Overmeer.net http://solutions.overmeer.net
From: SREZIC [...] cpan.org
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
Subject: Re: [rt.cpan.org #32775] Make TYPE optional when compiling readers
Date: Wed, 30 Jan 2008 21:31:23 +0100
To: Slaven_Rezic via RT <bug-XML-Compile [...] rt.cpan.org>
From: Mark Overmeer <mark [...] overmeer.net>
* Slaven_Rezic via RT (bug-XML-Compile@rt.cpan.org) [080130 09:30]: Show quoted text
> Here it is: > > #!perl > use XML::LibXML; > use XML::Compile::Schema; > use YAML;
What about the attached upgrade of your example? MarkOv

Message body is not shown because sender requested not to inline it.

CC: SREZIC [...] cpan.org
Subject: Re: [rt.cpan.org #32775] Make TYPE optional when compiling readers
Date: 31 Jan 2008 20:30:31 +0100
To: bug-XML-Compile [...] rt.cpan.org
From: Slaven Rezic <slaven [...] rezic.de>
"Mark Overmeer via RT" <bug-XML-Compile@rt.cpan.org> writes: Show quoted text
> <URL: http://rt.cpan.org/Ticket/Display.html?id=32775 > > > * Slaven_Rezic via RT (bug-XML-Compile@rt.cpan.org) [080130 09:30]:
> > Here it is: > > > > #!perl > > use XML::LibXML; > > use XML::Compile::Schema; > > use YAML;
> > What about the attached upgrade of your example?
Looks fine. -- Slaven Rezic - slaven <at> rezic <dot> de Berlin Perl Mongers - http://berlin.pm.org
Often, the messages themselves are not typed, so the type of the message cannot be autodetected. Besides, for performance reasons, you want the user to select the schema's to be used manually. So, the xml2yaml script may be smart, the library's ::compile() method will not be smart.