Validating XML in Perl with libxml and an XSD File

I am trying to get my perl script to get an Xxml file from the Internet and check it against the XSD file.

The code for 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 result leads to a cryptic error: "Error checking the contents of the element fragment" (cropdata is the first of my nonroot tags).

In my XSD file, the entry looks like this:

<xs:element name="cropdata">
<xs:complexType>
<xs:sequence>

Then a bunch of " <xs:element..../>" and then:

</xs:sequence>
</xs:complexType>
</xs:element>

Going to the perl debugger shows that after running the line "my $doc = $parser->parse_string($file);", $ doc prints as XML :: LibXML :: Document = SCALAR (0x6b99f0).

Can someone help me shed light on what I am doing wrong? (Warning: this could be a stupid mistake, I would not miss it myself).

+3
1

, xml , , , , xmllint, .

0

Source: https://habr.com/ru/post/1757867/


All Articles