问题描述
我正在尝试使用 LibXML :: Schema Validator针对架构验证XML CPAN模块.在同一脚本中,我正在使用 XML :: DOM CPAN模块来解析XML.我希望我的脚本采用XML文件针对XSD对其进行验证并进行解析.
I am trying to validate an XML against schema using LibXML::Schema Validator CPAN module. In that same script am using XML::DOM CPAN module to parse the XML. I want my script to take XML file validate it against XSD and parse it.
当我尝试运行脚本时,针对xsd进行验证后,它退出并且不解析XML.我希望它解析XML文件(如果有效)并生成DOM结构.如果有人可以分享一些见解,我将不胜感激.
When I try to run the script, after validating against xsd it exits and does not parse XML. I want it parse the XML file if it is valid and generate DOM structure. I would really appreciate if someone could share some insights on it.
#usr/bin/perl -w
use XML::LibXML;
my $schema = XML::LibXML::Schema->new(location =>'export.xsd');
my $parser = XML::LibXML->new;
my $xml = 'Export.xml';
my $doc = $parser->parse_file($xml);
eval { $schema->validate( $doc ) };
print $@ if $@;
print "$xml is valid\n";
use XML::DOM;
#use strict;
my $parser = new XML::DOM::Parser;
my $doc = $parser->parsefile ("Export.xml");
my $productOfferingnodes = $doc->getElementsByTagName("productOfferings")->item(0);
my @productOffering = $productOfferingnodes->getChildNodes();
{
foreach my $productOffering(@productOffering)
{
if ($productOffering->getNodeType == ELEMENT_NODE)
{
print $productOffering->getNodeName;
}
}
}
错误消息:
Schemas parser error : Failed to parse the XML resource 'export.xsd'.
推荐答案
您的代码混乱.您的脚本以应为shebang行的行开头,但不是.您可以在此简短脚本中重新定义两个变量.您检查验证是否失败,并且即使成功也要愉快地进行.这些可能不是造成问题的原因,但是它们确实使诊断问题变得更加困难.我试图重构您的代码.下面的代码通过perl -c
.此外,我使用示例XML
和XSD
文件.如该页所述,缺少元素,验证失败.添加缺少的信息后,验证成功,并产生了预期的输出.
Your code is messy. Your script begins with a line which is intended to be the shebang line but is not. You re-define two variables in this short script. You check if validation failed and merrily go on your way even if it did. These are likely not the cause of your problems, but they do make diagnosing the problem harder. I tried to refactor your code. The code below passes perl -c
. Further, I tried it using sample XML
and XSD
files. As explained on that page, with a missing element, validation failed. When the missing information was added, validation succeeded and expected output was produced.
#!/usr/bin/env perl
use strict; use warnings;
use XML::LibXML;
use XML::DOM;
my $xml = 'Export.xml';
my $xsd = 'export.xsd';
if ( my $error = validate_xml_against_xsd($xml, $xsd) ) {
die "Validation failed: $error\n";
}
my @offerings = get_product_offerings( $xml );
print "$_\n" for @offerings;
sub get_product_offerings {
my ($xml) = @_;
my $parser = XML::DOM::Parser->new;
my $doc = $parser->parsefile($xml);
my $nodes = $doc->getElementsByTagName("book")->item(0);
return map {
$_->getNodeType == ELEMENT_NODE
? $_->getNodeName
: ()
} $nodes->getChildNodes;
}
sub validate_xml_against_xsd {
my ($xml, $xsd) = @_;
my $schema = XML::LibXML::Schema->new(location => $xsd);
my $parser = XML::LibXML->new;
my $doc = $parser->parse_file($xml);
eval { $schema->validate( $doc ) };
if ( my $ex = $@ ) {
return $ex;
}
return;
}
输出:
author
title
genre
price
pub_date
review
顺便说一句,验证失败时的错误消息是有益的:Validation failed: Element 'review': This element is not expected. Expected is (pub_date ).
By the way, the error message when validation failed was informative: Validation failed: Element 'review': This element is not expected. Expected is (pub_date ).
这篇关于为什么我的XSD文件无法使用XML :: LibXML解析?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!