[Xml-compile] xsi:type namespace

Brendan Knox knox.brendan at gmail.com
Wed Mar 3 02:22:21 GMT 2010


Hi,

I have recently been trying to use some schema that implement xsi:type.
 Unfortunately one of the schema involved does not specify a namespace and I
believe this is causing some issues with the xsi:type mapping.  I have
included some sample code below that hopefully highlights the problem (not
the true schema, just simplified versions).

----------------------------------------------------------------------------
use XML::Compile::Schema;
use Data::Dumper;

my $xs =3D new XML::Compile::Schema;

my $schema_base =3D <<_EOBASE_;
<?xml version=3D"1.0" encoding=3D"UTF-8"?>
<xs:schema targetNamespace=3D"BASE" xmlns:xs=3D"http://www.w3.org/2001/XMLS=
chema"
xmlns=3D"BASE" attributeFormDefault=3D"unqualified"
elementFormDefault=3D"unqualified">

<xs:element name=3D"document">
    <xs:complexType>
        <xs:sequence>
            <xs:element name=3D"command" type=3D"BASEcommand" />
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:complexType name=3D"BASEcommand" abstract=3D"true"> <xs:sequence>
</xs:sequence> </xs:complexType>

</xs:schema>
_EOBASE_

my $schema_other =3D <<_EOOTHER_;
<?xml version=3D"1.0" encoding=3D"UTF-8"?>
<xs:schema xmlns:xs=3D"http://www.w3.org/2001/XMLSchema" xmlns:BASE=3D"BASE"
xmlns=3D"" attributeFormDefault=3D"qualified" elementFormDefault=3D"qualifi=
ed">
<xs:import namespace=3D"BASE" schemaLocation=3D"schema_base.xsd"/>

<xs:complexType name=3D"test_command">
    <xs:complexContent>
        <xs:extension base=3D"BASE:BASEcommand">
            <xs:sequence>
                <xs:element name=3D"afield" type=3D"xs:string"/>
            </xs:sequence>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>

</xs:schema>
_EOOTHER_

$xs->importDefinitions($schema_base);
$xs->importDefinitions($schema_other);

my $xml_string =3D <<_EOX_;
<?xml version=3D"1.0" encoding=3D"ISO-8859-1"?>
<document xmlns:xsi=3D"http://www.w3.org/2001/XMLSchema-instance"
xmlns=3D"BASE">
    <command xmlns=3D"" xsi:type=3D"test_command">
        <afield>12345</afield>
    </command>
</document>
_EOX_

my $reader =3D $xs->compile(READER =3D> '{BASE}document', xsi_type =3D> {
'{BASE}BASEcommand' =3D> [ 'test_command' ] });
my $xml =3D $reader->($xml_string);

print Dumper $xml;
----------------------------------------------------------------------------

As can be seen the $schema_other does not have a namespace.

When run against XML::Compile 1.13, I get the following output:

error: cannot find type {http://www.w3.org/2001/XMLSchema}test_command at
{BASE}document/command

If I change the xsi_type mapping to:
 my $reader =3D $xs->compile(READER =3D> '{BASE}document', xsi_type =3D> {
'{BASE}BASEcommand' =3D> [ '{}test_command' ] });

The same error message occurs.

Out of curiosity I modified XML/Compile/Translate.pm line 682 to:
$altnode->setAttribute(type =3D> "{$ns}$type");

This would now properly interpret the XML string into the expected Perl
hash.  I do not believe that is the solution though, so I am wondering if
there is something I am doing incorrectly with the xsi_type mapping or
XML::Compile cannot handle this schema?  Unfortunately I cannot edit the
schema as they are third-party.


Any help appreciated,
Brendan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/xml-compile/attachments/20100303/88=
6be2fe/attachment.htm


More information about the Xml-compile mailing list