Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
755 views
in Technique[技术] by (71.8m points)

perl - Why does my XSD file fail to parse with XML::LibXML?

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.

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

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

Error Message:

Schemas parser error : Failed to parse the XML resource 'export.xsd'.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
";
}

my @offerings = get_product_offerings( $xml );
print "$_
" 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;
}

Output:

author
title
genre
price
pub_date
review

By the way, the error message when validation failed was informative: Validation failed: Element 'review': This element is not expected. Expected is (pub_date ).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...