简体   繁体   中英

XML::LibXML::Schema isn't validating my xml. Why?

xml:

<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>
</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>
    <xs:element name="impiegato" type ="zoo:Timpiegato"/>
    <xs:element name="manager" type ="zoo:Tmanager"/>
</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" default="-1"/>
</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" default="-1"/>
</xs:sequence>
</xs:complexType>

</xs:schema>

code:

my $xmlschema = XML::LibXML::Schema->new( location => "../xml/worker.xsd" );
if (eval { $xmlschema->validate( $doc ); } eq undef) {
  # redirect
}

I think that the problem is in this part of the schema:

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

because if I run

xmllint --schema worker.xsd workers.xml

on a terminal I get this error:

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

Is there an alternative to xs:sequence to try? Because Impiegato and Managers elements don't have an order.

fix'd the problem changing Tworkers to

<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>

Now I have another problem (likely on the namespace), asking a new question

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM