[Xml-compile] unique constraint not validating

ml at usermode.de ml at usermode.de
Sun Feb 5 09:48:52 GMT 2012


 Hello,

 It seems that XML::Compile does not know unique constraints:

 unique.xsd:

 <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <xsd:complexType name="t_a">
     <xsd:sequence>
       <xsd:element name="b" type="t_b" maxOccurs="unbounded" />
     </xsd:sequence>
   </xsd:complexType>

   <xsd:complexType name="t_b">
     <xsd:attribute name="attr" />
   </xsd:complexType>

   <xsd:element name="a" type="t_a">
     <xsd:unique name="b-attr">
       <xsd:selector xpath="b"/>
       <xsd:field xpath="@attr"/>
     </xsd:unique>
   </xsd:element>
 </xsd:schema>

 ---------

 unique.xml:

 <a>
   <b attr="1" />
   <b attr="1" />
 </a>

 -------------

 Expected behaviour:

 $ xmllint --schema unique.xsd unique.xml
 <?xml version="1.0"?>
 <a>
   <b attr="1"/>
   <b attr="1"/>
 </a>
 unique.xml:3: element b: Schemas validity error : Element 'b': 
 Duplicate key-sequence ['1'] in unique identity-constraint 'b-attr'.
 unique.xml fails to validate

 --------------

 unique.pl:

 #!/usr/bin/perl

 use warnings;
 use strict;

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

 my $schema = XML::Compile::Schema->new('unique.xsd');
 #$schema->printIndex;

 use XML::Compile::Util qw/pack_type/;
 my $elem   = pack_type '', 'a';
 my $reader   = $schema->compile(READER => $elem);

 my $xml;
 while(<>) {
   $xml .= $_;
 }

 my $data   = $reader->($xml);
 print Dumper($data);

 ---------------------

 $ ./unique.pl unique.xml
 $VAR1 = {
           'b' => [
                  {
                    'attr' => '1'
                  },
                  {
                    'attr' => '1'
                  }
                ]
         };

 ------------

 I've found an old conversation from 2008 regarding unique IDs:

 http://grokbase.com/t/sc/xml-compile/085f2pr9yf/xml-compile-schema-validation-on-xs-id-has-problem


 Are there any news?

 Possible workarounds:
 * Do another validation with XML::LibXML::Schema to catch unique 
 constraints
 * Remove 'unique' from schema and move logic into application/database

 What would you suggest?


 Regards,

 Markus.











More information about the Xml-compile mailing list