[Xml-compile] XML to parse does not contain a namespace

Karen Etheridge perl at froods.org
Tue Nov 13 19:02:48 GMT 2012


On Sun, Nov 11, 2012 at 11:37:18PM +0100, Mark Overmeer wrote:
> * Karen Etheridge (perl at froods.org) [121110 00:45]:
> > I am attempting to parse XML with an XSD, but the XML does not contain an
> > xmlns attribute.  Is there any remedy to tell the compiled reader what the
> > target namespace is of all the elements being read in?
> 
> Many abuse namespaces.
> What about overruling the form?
> 
>   my $schema = XML::Compile::Cache->new($xsdfn
>     , element_form_default => 'unqualified');

That doesn't seem sufficient, as I still error on:

error: data not recognized, found a `Foo'

Below is a simple example which demonstrates the problem.

    use strict;
    use warnings;
    use XML::Compile::Schema;
    use XML::Compile::Cache;
    use XML::Compile::Util 'pack_type';
    use Data::Dumper;

    my $xsd = <<XSD;
    <?xml version="1.0"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
      targetNamespace="http://www.w3schools.com"
      xmlns="http://www.w3schools.com"
      elementFormDefault="qualified">

      <xs:element name="Foo">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="transaction_id" type="xs:string"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:schema>
    XSD

    my $xml = <<XML;
    <?xml version="1.0" encoding="UTF-8"?>
    <Foo>
      <transaction_id>123</transaction_id>
    </Foo>
    XML

    my $schema = XML::Compile::Cache->new(
        $xsd,
    # this doesn't help either
    #    target_namespace => '',
        element_form_default => 'unqualified',
    );

    my $reader = $schema->compile(
        READER => pack_type('http://www.w3schools.com', 'Foo'),

        # including these bits doesn't help either
        key_rewrite => 'PREFIXED',
        prefixes => [
                'http://www.w3schools.com' => '',
        ],
    );

    # fails with: error: data not recognized, found a `Foo'
    my $data = $reader->($xml);




More information about the Xml-compile mailing list