使用libxml和XSD文件在Perl中验证XML [英] Validating XML in Perl with libxml and an XSD file
问题描述
我试图让我的perl脚本从在线获取Xxml文件并根据XSD文件对其进行验证.
I am trying to have my perl script get an Xxml file from online and validate it according to an XSD file.
执行此操作的代码如下:
The code to do this is as follows:
my $url = shift @ARGV;
my $response = $ua->get($url) || die "Can't fetch file";
my $file = $response->content;
my $schema_file = "schema.xsd";
my $schema = XML::LibXML::Schema->new(location => $schema_file);
my $parser = XML::LibXML->new;
my $doc = $parser->parse_string($file);
eval { $schema->validate($doc) };
die $@ if $@;
运行此命令会导致一个神秘错误:元素cropdata内容检查失败"(cropdata是我的第一个非root标记).
Running this results in a cryptic error: "Element cropdata content check failure" (cropdata is the first of my nonroot tags).
在我的XSD文件中,该条目如下所示:
In my XSD file, the entry looks like:
<xs:element name="cropdata">
<xs:complexType>
<xs:sequence>
然后是一堆"
Then a bunch of "<xs:element..../
>"
and then:
</xs:sequence>
</xs:complexType>
</xs:element>
进入perl调试器显示,让"my $doc = $parser->parse_string($file);"
行运行后,$ doc打印为XML :: LibXML :: Document = SCALAR(0x6b99f0).
Going into the perl debugger shows that after letting the line "my $doc = $parser->parse_string($file);"
run, $doc prints as XML::LibXML::Document=SCALAR(0x6b99f0).
任何人都可以帮助我了解我做错了什么吗? (警告:这可能是一个愚蠢的错误,我不会自己说了.)
Can anyone help me shed light on what I am doing wrong? (Warning: it may be a dumb mistake, I wouldn't put it past myself).
推荐答案
结果证明,我提供的示例xml文件无效,瞧瞧,在xmllint中对其进行检查和更正后,一切正常.
Turns out that the sample xml file I had been provided was invalid, and lo and behold, upon checking and correcting it in xmllint, everything works.
这篇关于使用libxml和XSD文件在Perl中验证XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!