[Xml-compile] writer and element with nillable="true" minOccurs="0"

Roman Daniel roman.daniel at davosro.cz
Mon Nov 9 16:42:28 GMT 2009


I encounter a strange (for me) behaviour of XML::Compile::Schema (1.07)
regarding the combination above.

I have an outer element of complex type (sequence) and inner element having
nillable=3D"true" and minOccurs=3D"0".
If I call writer for the outer element and don't supply the inner element
key in the hash, the inner element is still rendered in the output - as
empty XML element.
If the element is not nillable it doesn't appear at all.
Look at the example

use strict;
use warnings;

use XML::Compile::Schema;
for my $nillable ( '', 'nillable=3D"true"' ) {
    my $schema =3D XML::Compile::Schema->new( schema($nillable) );

    my $writer =3D $schema->compile( WRITER =3D> "{http://myns.cz/}outer" );
    my $reader =3D $schema->compile( READER =3D> "{http://myns.cz/}outer" );
    my $doc    =3D XML::LibXML::Document->new;
    my $e      =3D $writer->( $doc, {} );
    $doc->setDocumentElement($e);
    warn "NILLABLE: $nillable\n", $doc->toString(1), "\n\n";

    # check, whether reader can accept this
    my $x =3D $reader->($doc->toString);
}


sub schema { my $nillable =3D shift; return <<"END_XML"; }
<?xml version=3D"1.0"?>
<xs:schema
    xmlns:xs=3D"http://www.w3.org/2001/XMLSchema"
    elementFormDefault=3D"qualified"
    targetNamespace=3D"http://myns.cz/">

<xs:element name=3D"outer">
    <xs:complexType>
        <xs:sequence>
            <xs:element name=3D"inner" minOccurs=3D"0" $nillable>
                <xs:simpleType>
                  <xs:restriction base=3D"xs:string">
                    <xs:minLength value=3D"1"/>
                  </xs:restriction>
                </xs:simpleType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>

</xs:schema>
END_XML


And its output:


NILLABLE:
<?xml version=3D"1.0"?>
<x0:outer xmlns:x0=3D"http://myns.cz/"/>


NILLABLE: nillable=3D"true"
<?xml version=3D"1.0"?>
<x0:outer xmlns:x0=3D"http://myns.cz/" xmlns:xsi=3D"
http://www.w3.org/2001/XMLSchema-instance">
  <x0:inner></x0:inner>
</x0:outer>


error: string `' does not have minimum length 1 at {
http://myns.cz/}outer/inner#facet

When element is not nillable it doesn't appear in the XML at all. If it is
nillable it appears as empty, which is unacceptable for reader, since the
content of the element is unempty string.
Is it a bug or am I missing something? Thanks for any clue,


Roman Daniel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/xml-compile/attachments/20091109/62=
28a7cf/attachment.htm


More information about the Xml-compile mailing list