3

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).

5
  • 1
    Try use Data::Dumper; Dumper($doc); right after the parse_string call -- does the $doc object look correct? Commented Aug 3, 2010 at 18:25
  • @Ether, $doc will almost certainly be a scalar ref to a number that is the memory address of a C data structure. Commented Aug 3, 2010 at 20:27
  • 2
    What does the input XML look like? Is it valid per the schema? Commented Aug 4, 2010 at 2:31
  • Oh my god, thank you Jim. 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. Commented Aug 4, 2010 at 16:03
  • Could you post this as an answer and accept it please (that way the question won't show as "unanswered", which means people waste time looking at it) Thanks :) Commented Nov 20, 2010 at 19:46

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.