Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't validate XML with XML schema and Perl (XML::LibXML)

xml:

<?xml version="1.0"?>
<workers xmlns="http://www.zoo.com" 
         xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" 
     xs:schemaLocation="http://www.zoo.com worker.xsd">
<impiegato>
    <username>mario</username>
    <password>de2f15d014d40b93578d255e6221fd60</password>
    <nome>Mario</nome>
    <sesso>F</sesso>
    <eta>23</eta>
</impiegato>
<impiegato>
    <username>maria</username>
    <password>maria</password>
    <nome>Mariaaa</nome>
    <sesso>F</sesso>
    <eta>443</eta>
</impiegato>
<impiegato>
    <username>mirco</username>
    <password>mirco</password>
    <nome>Mirco</nome>
    <sesso>F</sesso>
    <eta>27</eta>
</impiegato>
<impiegato>
    <username>martina</username>
    <password>martina</password>
    <nome>Martina</nome>
    <sesso>M</sesso>
    <eta>26</eta>
</impiegato>
<manager>
    <username>marco</username>
    <password>marco</password>
    <nome>Marco</nome>
    <sesso>M</sesso>
    <eta>25</eta>
</manager>
<manager>
    <username>giovanna</username>
    <password>zxVcGz0BPdHkY</password>
    <nome>Giovanna</nome>
    <sesso>F</sesso>
    <eta>24</eta>
</manager>
<manager>
<username>lucanervi</username>
    <password>zxePlNSDQjsxg</password>
    <nome>Luca Nervi</nome>
    <sesso>M</sesso>
    <eta>23</eta>
</manager>
</workers>

XML schema:

<?xml version="1.0"?>
<xs:schema
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
 xmlns:zoo="http://www.zoo.com"
 targetNamespace="http://www.zoo.com"
 elementFormDefault="qualified">

<xs:element name="workers" type="zoo:Tworkers"/>

<xs:complexType name="Tworkers">
<xs:sequence  maxOccurs="unbounded">
    <xs:element name="impiegato" type ="zoo:Timpiegato" minOccurs="0" />
    <xs:element name="manager" type ="zoo:Tmanager" minOccurs="0"/>
</xs:sequence>
</xs:complexType>

<xs:complexType name="Timpiegato">
<xs:sequence>
    <xs:element name="username" type ="xs:string"/>
    <xs:element name="password" type ="xs:string"/>
    <xs:element name="nome" type ="xs:string"/>
    <xs:element name="sesso" type ="xs:string"/>
    <xs:element name="eta" type ="xs:integer"/>
</xs:sequence>
</xs:complexType>

<xs:complexType name="Tmanager">
  <xs:sequence>
    <xs:element name="username" type ="xs:string"/>
    <xs:element name="password" type ="xs:string"/>
    <xs:element name="nome" type ="xs:string"/>
    <xs:element name="sesso" type ="xs:string"/>
    <xs:element name="eta" type ="xs:integer"/>
  </xs:sequence>
</xs:complexType>

</xs:schema>

When I validate the xml using XML::LibXML::Schema, I get:

../xml/workers.xml:0: Schemas validity error : Element 'impiegato': This element is not expected. Expected is one of ( {http://www.zoo.com}impiegato, {http://www.zoo.com}manager ).

Perl code:

my $parser = XML::LibXML->new;
my $doc = $parser->parse_file("../xml/workers.xml");
my $xmlschema = XML::LibXML::Schema->new( location => "../xml/worker.xsd" );
$xmlschema->validate($doc);

I think it's a problem with the namespace, but don't know what to do.

Addendum:

I tried to remove the elementFormDefault="qualified" attribute from the XML schema. Now I have the opposite error:

../xml/workers.xml:0: Schemas validity error : Element '{http://www.zoo.com}impiegato':
This element is not expected. Expected is one of ( impiegato, manager ).
like image 312
qwertoyo Avatar asked Dec 12 '25 20:12

qwertoyo


2 Answers

Validating this with Saxon, it works for me. I think it must be some mistake in the way you are running the validation.

like image 81
Michael Kay Avatar answered Dec 14 '25 08:12

Michael Kay


Solved. The problem was in the perl code. For some reason, when you add a node to a $doc using XML:LibXML, that node in memory doesn't get the default namespace. Solved creating another $doc2, parsering $doc->toString() and validating $doc2.

I should have written in my question that i was adding a node, my fault.

code:

my $doc2 = $parser->parse_string($root->toString());
like image 20
qwertoyo Avatar answered Dec 14 '25 08:12

qwertoyo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!