gpt4 book ai didi

xml - Perl XML::LibXML::Schema 将在第一个错误时停止验证

转载 作者:行者123 更新时间:2023-12-04 06:50:22 26 4
gpt4 key购买 nike

我正在尝试使用 XML::LibXML::Schema 针对 xsd 验证 xml 文件。问题是,如果我的 xml 有多个语义问题,则验证将在第一个问题上消失,而不会报告其他问题。



It finds the first error, no reference to <foobar/>
bash-3.2$ ./test.pl test.xsd test.xml
xmlfile <test.xml> failed validation: test.xml:14: Schemas validity error : Element 'foobar': This element is not expected.



After I remove the first error, it finds another. "ten" is not an unsigned int
xmlfile <test.xml> failed validation: test.xml:11: Schemas validity error : Element 'allocation': 'ten' is not a valid value of the atomic type 'xs:unsignedInt'.



Changing "ten" to 10 fixes this issue
bash-3.2$ ./test.pl test.xsd test.xml
No issues found




我想在一次运行中获得所有问题的报告。有任何想法吗?我应该为此使用另一个模块吗?遵循我的 Perl 脚本以及我的 xsd 和 xml 文件。

谢谢你的帮助,

保罗



xsd:



 <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<!-- channel -->
<xsd:element name="channel">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="username" type="xsd:string"/>
<xsd:element name="password" type="xsd:string"/>
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
</xsd:complexType>
</xsd:element>

<!-- hotel -->
<xsd:element name="hotel">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="date">
<xsd:complexType>
<xsd:attribute name="from" use="required" type="xsd:string" />
<xsd:attribute name="to" use="required" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element ref="room" minOccurs="1"/>
</xsd:sequence>
<xsd:attribute name="id" use="required" type="xsd:unsignedInt" />
</xsd:complexType>
</xsd:element>


<!-- room -->
<xsd:element name="room">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="allocation" type="xsd:unsignedInt"></xsd:element>
</xsd:sequence>
<xsd:attribute name="id" use="required" type="xsd:string" />
</xsd:complexType>
</xsd:element>

<!-- building all together -->
<xsd:element name="request">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="channel" maxOccurs="1"/>
<xsd:element ref="hotel" maxOccurs="1"/>
</xsd:sequence>
<xsd:attribute name="type" use="required" type="xsd:string" />
</xsd:complexType>
</xsd:element>

xml:
<?xml version="1.0" encoding="UTF-8"?>
<request type="test" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<channel name="channel">
<username>user</username>
<password>pass</password>
</channel>

<hotel id="ten">
<date from="2009-07-07" to="2009-07-17"/>
<room id="1">
<allocation>ten</allocation>
</room>
</hotel>
</request>

perl 脚本:
#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;
#use Carp;
use vars qw( $xsd $xml $schema $parser $doc );

use XML::LibXML;


#
# MAIN
#
my $xsd = $ARGV[0];
my $xml = $ARGV[1];

$schema = XML::LibXML::Schema->new(location => $xsd);
$parser = XML::LibXML->new;
$doc = $parser->parse_file($xml);
eval { $schema->validate($doc) };

if ( $@ ) {
warn "xmlfile <$xml> failed validation: $@" if $@; exit(1);
}
else { print "No issues found\n"; }

最佳答案

尝试通过 recover => 1XML::LibXML->new .它可能会有所帮助。

来自 XML::LibXML::Parser 的文档...

   recover
/parser, html, reader/

recover from errors; possible values are 0, 1, and 2

A true value turns on recovery mode which allows one to parse
broken XML or HTML data. The recovery mode allows the parser to
return the successfully parsed portion of the input document. This
is useful for almost well-formed documents, where for example a
closing tag is missing somewhere. Still, XML::LibXML will only
parse until the first fatal (non-recoverable) error occurs,
reporting recoverable parsing errors as warnings. To suppress even
these warnings, use recover=>2.

Note that validation is switched off automatically in recovery
mode.

关于xml - Perl XML::LibXML::Schema 将在第一个错误时停止验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3206672/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com