[Xml-compile] First steps with XML:Compile
Rolf Schaufelberger
rs at plusw.de
Mon Jun 30 22:10:40 BST 2008
Am Donnerstag, 26. Juni 2008 00:55:22 schrieb Rolf Schaufelberger:
> add some more info;
>
> Am Donnerstag, 26. Juni 2008 00:14:52 schrieb Rolf Schaufelberger:
> > Hi ,
> > I'm just trying my first steps in understanding XML::Compile.
> > My goal is, to write a XML file according to given xsd files
> > So I want to write an openTRANS XML document, (www.opentrans.org)
> >
> > where I have the xsd files
> >
> > openTRANS_ORDER_1_0_draft.xsdcountries_draft.xsd
> > openbase_1_0_draft.xsd
> > currencies_draft.xsd
> > lang_draft.xsd
> > punit_draft.xsd
> > udx_draft.xsd
> > unit_draft.xsd
> >
> > The first one defines the document type, the second library elements and
> > the others are included by the second one.
> > In my test script I list all files (with absolute path in an array and
> > pass it to XML::Compile::Schema->new.
> > When I call printIndex I get a long list :
> >
> > namespace: http://www.opentrans.org/XMLSchema/1.0_draft
> > filename: /home/rs/openTrans/openTRANS_ORDER_1_0_draft.xsd
> > definitions of elements:
> > ORDER
> > ORDER_HEADER
> > ORDER_INFO
> > ORDER_ITEM_LIST
> > ORDER_SUMMARY
> > namespace:
> > filename: /home/rs/openTrans/openbase_1_0_draft.xsd
> > definitions of elements:
> > ACCOUNT
> > ACCOUNTING_INFO
> > ADDRESS
> > ...
> > CONTROL_INFO
> > COST_ACCOUNT
> > ....
> >
> >
> > Now I define a hash like this to define an order:
> >
> > my $hash = { order=>
> > {version=> '1.0',
> > type => 'standard',
> > ORDER_HEADER => {
> > ORDER_INFO => {
> > ORDER_ID => 123,
> > ORDER_DATE => '2008-06-25',
> > }
> > },
> > ...
> >
> > and , taken as example from the docs :
> >
> > # create and use a writer
> > my $doc = XML::LibXML::Document->new('1.0', 'UTF-8');
> > my $myns = 'http://www.opentrans.org/XMLSchema/1.0_draft';
> > my $write = $schema->compile(WRITER => "{$myns}ORDER");
> > my $xml = $write->($doc, $hash);
> >
> >
> > running this I get an error:./openTrans.pl
> > error: cannot find
> > element '{http://www.opentrans.org/XMLSchema/1.0_draft}CONTROL_INFO' at
> > {http://www.opentrans.org/XMLSchema/1.0_draft}ORDER/ORDER_HEADER/CONTROL_
> >IN FO
> >
> > So it looks like CONTROL_INFo is the first element appearing in
> > openTRANS_ORDER_1_0_draft.xsdcountries_draft.xsd
> > that is defined in
> > openbase_1_0_draft.xsd
> > and the mapping with the namespace is not working.
> > How do I have to do that ?
>
> When I call :
> print Dumper $schema->template (PERL=> "CONTROL_INFO");
>
> I get an nice output:
>
> $VAR1 = '{ # sequence of STOP_AUTOMATIC_PROCESSING, GENERATOR_INFO,
> # GENERATION_DATE, MIME_ROOT
>
> # is a {http://www.w3.org/2001/XMLSchema}string
> # is optional
> # with some value restrictions
> STOP_AUTOMATIC_PROCESSING => "example",
> ....
>
> but when I call
> print Dumper $schema->template (PERL=> "{$myns}ORDER");
>
> I get the same error as above
> error: cannot find
> element '{http://www.opentrans.org/XMLSchema/1.0_draft}CONTROL_INFO' at
> {http://www.opentrans.org/XMLSchema/1.0_draft}ORDER/ORDER_HEADER/CONTROL_IN
>FO
>
> and print Dumper $schema->namespaces gives
>
> .....
>
> 'CONTROL_INFO' => {
> 'ref' => undef,
> 'name' => 'CONTROL_INFO',
> 'efd' => 'qualified',
> 'schema' => $VAR1->{'tns'}{''}[0],
> 'full' => 'CONTROL_INFO',
> 'node' => bless( do{\(my $o =
> 135642424)}, 'XML::LibXML::Element' ),
> 'ns' => '',
> 'sg' => undef,
> 'abstract' => '',
> 'type' => 'element',
> 'id' => undef,
> 'afd' => 'unqualified',
> 'prefix' => ''
> },
> ......
So , I could find a workaround for the namespace problem by using a xsd file
that contains all necessary parts (no include, no import).
And I could produce a xml file by building a perl hash and translate that to
XML. All very fine, works like expected.
Now just one question:
The generated XML uses a x0 namespace for all elements. How can I switch this
off ?
...
<x0:QUANTITY>
1
</x0:QUANTITY>
<x0:ORDER_UNIT>
C62
</x0:ORDER_UNIT>
...
--
Rolf Schaufelberger
More information about the Xml-compile
mailing list