无法使用XML架构和Perl(XML :: LibXML)验证XML

时间:2012-06-16 14:29:01

标签: xml perl xsd 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架构:

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

当我使用XML :: LibXML :: Schema验证xml时,我得到:

  

../ xml / workers.xml:0:架构有效性错误:元素'impiegato':不期望此元素。预计是其中之一(   {http://www.zoo.com} impiegato,{http://www.zoo.com}经理)。

Perl代码:

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

我认为这是命名空间的问题,但不知道该怎么做。

附录:

我尝试从XML架构中删除elementFormDefault =“qualified”属性。现在我有相反的错误:

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

3 个答案:

答案 0 :(得分:1)

使用Saxon验证这一点,它对我有用。我认为你运行验证的方式肯定是错误的。

答案 1 :(得分:1)

解决。问题出在perl代码中。出于某种原因,当您使用XML:LibXML将节点添加到$ doc时,内存中的该节点不会获得默认命名空间。 解决了创建另一个$ doc2,解析$ doc-&gt; toString()并验证$ doc2。

我应该在我的问题中写道,我正在添加一个节点,我的错。

代码:

my $doc2 = $parser->parse_string($root->toString());

答案 2 :(得分: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"; 
 }