3

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 ).
qwertoyo
  • 1,332
  • 2
  • 15
  • 31

3 Answers3

1

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

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • I'll edit the question with the code that is used for the validation – qwertoyo Jun 16 '12 at 15:37
  • The document seems valid to me as well. The validators on Oxygen and SpringSource Tool Suite don't complain either. – toniedzwiedz Jun 16 '12 at 15:39
  • Yeah I know, if I try to validate it with xmllint, it validates – qwertoyo Jun 16 '12 at 15:40
  • added more infos: "I tried to remove the elementFormDefault="qualified" attribute from the XML schema. Now I have the opposite error" – qwertoyo Jun 16 '12 at 15:49
  • @qwertoyo you can't just drop the attribute without changing your document. The default value is "unqualified" so you'd have to explicitly use an empty namespace for your elements. – toniedzwiedz Jun 16 '12 at 15:59
  • @Tom yeah I know it, I tried just to see what would change. I need to keep the attribute and the namespace because the project is pretty big and based on namespaces, the only thing that isn't working is the xmlschema validation. Now I'm really stuck. – qwertoyo Jun 16 '12 at 16:03
  • 1
    @qwertoyo I'm afraid I can't be of much help. This seems to be more of a Perl-related issue than an XML problem. I suggested an edit to your question to make it more visible for Perl programmers. As for trying to work around the strange behavior. Have you attempted to add a namespace prefix to the top element? – toniedzwiedz Jun 16 '12 at 16:09
  • @Tom yes I tried to add the prefix (""), but I get an error: "namespace error : Namespace prefix zoo on workers is not defined". Thanks for the edit suggestion – qwertoyo Jun 16 '12 at 16:13
1

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());
qwertoyo
  • 1,332
  • 2
  • 15
  • 31
0
use strict;
use warnings;

use XML::LibXML;

my $xml_file = 'test.xml';
my $xsd_file = 'test.xsd';
my $schema   = XML::LibXML::Schema->new(location => 
$xsd_file);
my $parser   = XML::LibXML->new;

my $tree = $parser->parse_file($xml_file);

# Valdate the XML file.
eval { $schema->validate($tree) }; 
if ( $@ ) { 
warn "xmlfile failed validation\n$@" if $@;
} 
else { 
 print "Valide XML\n"; 
 }
parthi u
  • 1
  • 1
  • Code-only answers are considered low quality: make sure to provide an explanation what your code does and how it solves the problem. Please see also https://meta.stackoverflow.com/questions/392712/explaining-entirely-code-based-answers – Mickael B. Jan 18 '20 at 14:47