Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate XML using LibXML

Currently, I am using the XML::LibXML perl module to validate an XML file against a defined XML schema. At the moment, if my XML file fails to validate successfully against the defined XML Schema, I will get a list of errors informing me, for example that certain elements were not expected and then what was expected instead. In my XML file I will have many elements of the same name but they may be nested in various places in the XML file.

My question is, is there anyway in which I can output the XPath location of any elements that may error when attempting to perform the validation?

Currently, my XML file is quite big and it is hard to "debug" it when validation fails as the name of the element that is displayed in the error, may occur many times in various places in the XML file.

My code is below for using LibXML to validate an XML file against a schema.

#!/usr/bin/perl
use strict;
use warnings;
use XML::LibXML;

my $schema_file = 'MySchema.xml';
my $document    = 'MyFile.xml';

my $schema = XML::LibXML::Schema->new(location => $schema_file);

my $parser = XML::LibXML->new;
my $doc    = $parser->parse_file($document);

eval { $schema->validate($doc) };
die $@ if $@;

print "$document validated successfully\n";
like image 930
firefly7 Avatar asked Sep 05 '10 15:09

firefly7


People also ask

How do I validate an XML file?

XML documents are validated by the Create method of the XmlReader class. To validate an XML document, construct an XmlReaderSettings object that contains an XML schema definition language (XSD) schema with which to validate the XML document.

Can I use Notepad++ to validate XML?

Notepad++ in combination with the XML Tools Plugin and the XidML schema is a smart way to validate XidML files (basically any XML file where you have a schema).

How validate XML in Perl?

xml file use XML::LibXML; $parser = XML::LibXML->new; $parser->validation(1); $parser->parse_file("books. xml"); When the document validates, the program produces no output—XML::LibXML successfully parses the document into a DOM structure that is quietly destroyed when the program ends.

How do I know if my XML schema is valid?

All you have to do is just paste the XML and click on “Check XSD validity” button. In XML Schema Definition (XSD), we can verify: Checking the schema in its value. The same name field by checking its data type.


2 Answers

I have just stumbled on the same problem and found that the XML parser does not store the line numbers by default. But you can tell him to do so with the XML_LIBXML_LINENUMBERS parameter of the constructor.

The following script will tell actual line numbers for errors instead of 0

use Modern::Perl;
use XML::LibXML;

my ($instance, $schema) = @ARGV;

my $doc = XML::LibXML->new(XML_LIBXML_LINENUMBERS => 1)->parse_file($instance); 
my $xmlschema = XML::LibXML::Schema->new( location => $schema );
my $res = eval { $xmlschema->validate( $doc ); };

say "error: $@" if $@;
say "res: ", $res//'undef';
like image 163
Seki Avatar answered Nov 05 '22 12:11

Seki


You might want to look at: XML::Validate to get line number and column number?

like image 32
nicomen Avatar answered Nov 05 '22 14:11

nicomen