我试图让我的 perl 脚本从网上获取 Xxml 文件并根据 XSD 文件对其进行验证。
执行此操作的代码如下:
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 $@;
运行此结果会导致一个神秘错误:“元素作物数据内容检查失败”(作物数据是我的第一个非根标签)。
在我的 XSD 文件中,条目如下所示:
<xs:element name="cropdata">
<xs:complexType>
<xs:sequence>
然后是一堆“>”
然后:
</xs:sequence>
</xs:complexType>
</xs:element>
进入 perl 调试器显示,在运行 "my $doc = $parser->parse_string($file);"
行后,$doc 打印为 XML::LibXML::文档=标量(0x6b99f0)。
谁能帮助我阐明我做错了什么? (警告:这可能是一个愚蠢的错误,我不会把它过去)。
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 $@;
Running this results in a cryptic error: "Element cropdata content check failure" (cropdata is the first of my nonroot tags).
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>
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).
发布评论
评论(1)
事实证明,我提供的示例 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.