Skip Menu |

This queue is for tickets about the XML-LibXML CPAN distribution.

Report information
The Basics
Id: 85924
Status: resolved
Priority: 0/
Queue: XML-LibXML

People
Owner: SHLOMIF [...] cpan.org
Requestors: DJIBEL [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Critical
Broken in: 0.91
Fixed in: 2.0101



Subject: Validate XML using LibXML - XSD - line and column number
Dear, I am working on XP machine. That is my programme : #!/usr/bin/perl use strict; use warnings; use XML::LibXML; my $FichierXML = 'filetest.xml'; my $parser = XML::LibXML->new(); my $tree = $parser->parse_file($FichierXML); my $root = $tree->getDocumentElement; # Validation XSD du fichier XML # Récupérons le fichier XSD dans la balise annuaire my $FichierXSD = $root->getAttribute('xsi:noNamespaceSchemaLocation'); my $schema = XML::LibXML::Schema->new( location => $FichierXSD ); eval { $schema->validate($tree) }; die "[XSD] Le fichier $FichierXML est non valide.\n$@" if $@; I am using the XML::LibXML perl module to validate an XML file against a defined XML schema. When the file fails to validate, I will get a list of errors informing me.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. How can I get the lines number and column number of each error in the XML file ? Best regards, Djibel
Hi Djibel, thanks for the report. See below for my comments: On Thu Jun 06 08:44:45 2013, DJIBEL wrote: Show quoted text
> Dear, > > I am working on XP machine. That is my programme : > > #!/usr/bin/perl > use strict; > use warnings; > use XML::LibXML; > > my $FichierXML = 'filetest.xml'; > my $parser = XML::LibXML->new(); > > my $tree = $parser->parse_file($FichierXML); > my $root = $tree->getDocumentElement; > > > # Validation XSD du fichier XML > # Récupérons le fichier XSD dans la balise annuaire > my $FichierXSD = $root->getAttribute('xsi:noNamespaceSchemaLocation'); > my $schema = XML::LibXML::Schema->new( location => $FichierXSD ); > eval { $schema->validate($tree) }; > die "[XSD] Le fichier $FichierXML est non valide.\n$@" if $@; >
I see several problems with this program: 1. The comments and identifiers are in French - not in English. See: http://perl-begin.org/tutorials/bad-elements/#code_in_foreign_lang 2. It is not self-contained, it references external files, and also requires network access. Please change it to be self-contained, with all the relevant files included (And "under the same terms as Perl" licensing), and in a .zip file, or a version control repository such as a one on http://github.com/ or http://bitbucket.org/ . Show quoted text
> I am using the XML::LibXML perl module to validate an XML file against > a defined XML schema. When the file fails to validate, I will get a > list of errors informing me.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. > > How can I get the lines number and column number of each error in the > XML file ?
I'm not sure if libxml2 supports it, but I'll check it and if so - try to add support (after you supply me with the relevant files). I know Relax-NG has a similar problem. Regards, -- Shlomi Fish Show quoted text
> > Best regards, > > Djibel
Dear Shlomi Fish, What's the problem with french language :-) ! I have join you three files : - Perl script - XML file - XSD file The error message is : xmlfile <test.xml> failed validation test.xml:0: Schemas validity error : Element 'toto': This element is not expected. Expected is ( password ). The Message is OK, but it missing the line number and column line. Best Regards, Djibel
Subject: test.xml
<?xml version="1.0" encoding="UTF-8" ?> <userdetails xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="test.xsd"> <username>Fred</username> <toto>test</toto> <password>pass123</password> </userdetails>
Subject: test.xsd
<?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="userdetails"> <xsd:complexType> <xsd:sequence> <xsd:element name="username" type="xsd:string"/> <xsd:element name="password" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>
Subject: xslt-xsd.pl
#!/usr/bin/perl use Carp; use strict; use warnings; use XML::LibXML; my $xml_file = 'test.xml'; my $xsd_file = 'test.xsd'; my $schema = XML::LibXML::Schema->new(location => $xsd_file); my $parser = XML::LibXML->new; my $tree = $parser->parse_file($xml_file); # Valdate the XML file. eval { $schema->validate($tree) }; if ( $@ ) { warn "xmlfile <$xml_file> failed validation\n$@" if $@; } else { print "Valide XML\n"; }
Hi DJIBEL, thanks for your cooperation. On Thu Jun 06 17:47:43 2013, DJIBEL wrote: Show quoted text
> Dear Shlomi Fish, > > What's the problem with french language :-) !
See my link. Show quoted text
> > I have join you three files : > - Perl script > - XML file > - XSD file >
Thanks. Show quoted text
> The error message is : > > xmlfile <test.xml> failed validation > test.xml:0: Schemas validity error : Element 'toto': This element is > not expected. Expected is ( password ). > > The Message is OK, but it missing the line number and column line.
I am able to reproduce it here. I'll try to investigate. Regards, -- Shlomi Fish
Hi! XML parser dos not store line numbers by default. In order to have line numbers information, you need use "XML_LIBXML_LINENUMBERS" option, for example: my $parser = XML::LibXML->new( XML_LIBXML_LINENUMBERS => 1 ); or $parser->line_numbers(1); -- Yuriy
Hi Yuriy, On Wed Aug 14 09:17:50 2013, yoreek wrote: Show quoted text
> Hi! > > XML parser dos not store line numbers by default. > > In order to have line numbers information, you need use > "XML_LIBXML_LINENUMBERS" option, for example: > > my $parser = XML::LibXML->new( XML_LIBXML_LINENUMBERS => 1 ); > > or > > $parser->line_numbers(1); > > -- > Yuriy
thanks! I can verify that with the attached file a line number indeed gets displayed. Resolving. Regards, -- Shlomi Fish
Subject: xslt-xsd.pl
#!/usr/bin/perl use Carp; use strict; use warnings; use XML::LibXML; my $xml_file = 'test.xml'; my $xsd_file = 'test.xsd'; my $schema = XML::LibXML::Schema->new(location => $xsd_file); my $parser = XML::LibXML->new(XML_LIBXML_LINENUMBERS => 1 ); my $tree = $parser->parse_file($xml_file); # Validate the XML file. eval { $schema->validate($tree) }; if ( $@ ) { warn "xmlfile <$xml_file> failed validation\n$@" if $@; } else { print "Valide XML\n"; }
Le Mer 14 Aoû 2013 09:17:50, yoreek a écrit : Show quoted text
> Hi! > > XML parser dos not store line numbers by default. > > In order to have line numbers information, you need use > "XML_LIBXML_LINENUMBERS" option, for example: > > my $parser = XML::LibXML->new( XML_LIBXML_LINENUMBERS => 1 ); > > or > > $parser->line_numbers(1); > > -- > Yuriy
Hi, Thank you for the answer. Djibril
Le Mer 14 Aoû 2013 09:52:46, SHLOMIF a écrit : Show quoted text
> Hi Yuriy, > > On Wed Aug 14 09:17:50 2013, yoreek wrote:
> > Hi! > > > > XML parser dos not store line numbers by default. > > > > In order to have line numbers information, you need use > > "XML_LIBXML_LINENUMBERS" option, for example: > > > > my $parser = XML::LibXML->new( XML_LIBXML_LINENUMBERS => 1 ); > > > > or > > > > $parser->line_numbers(1); > > > > -- > > Yuriy
> > thanks! I can verify that with the attached file a line number indeed > gets displayed. Resolving. > > Regards, > > -- Shlomi Fish
Dear Shlomi, Can you active it by default in your module. It will be a nice idee. Or, if you does not want to active it by default, change the POD docmentation to explain the way to active it. Best regards, djibel
Hi Djibel, On Fri Aug 16 04:57:18 2013, DJIBEL wrote: Show quoted text
> Le Mer 14 Aoû 2013 09:52:46, SHLOMIF a écrit :
> > Hi Yuriy, > > > > On Wed Aug 14 09:17:50 2013, yoreek wrote:
> > > Hi! > > > > > > XML parser dos not store line numbers by default. > > > > > > In order to have line numbers information, you need use > > > "XML_LIBXML_LINENUMBERS" option, for example: > > > > > > my $parser = XML::LibXML->new( XML_LIBXML_LINENUMBERS => 1 ); > > > > > > or > > > > > > $parser->line_numbers(1); > > > > > > -- > > > Yuriy
> > > > thanks! I can verify that with the attached file a line number indeed > > gets displayed. Resolving. > > > > Regards, > > > > -- Shlomi Fish
> > Dear Shlomi, > > Can you active it by default in your module. It will be a nice idea.
I'd rather not activate it by default because it may break existing code (backwards compatibility), may make XML::LibXML slower by default, and may not reflect the API of libxml2 properly. Show quoted text
> Or, if you does not want to active it by default, change the POD > docmentation to explain the way to active it.
It is explained here: https://metacpan.org/module/SHLOMIF/XML-LibXML-2.0101/lib/XML/LibXML/Parser.pod Regards, -- Shlomi Fish Show quoted text
> > Best regards, > > djibel
Le Ven 16 Aoû 2013 05:15:14, SHLOMIF a écrit : Show quoted text
> Hi Djibel, > > On Fri Aug 16 04:57:18 2013, DJIBEL wrote:
> > Le Mer 14 Aoû 2013 09:52:46, SHLOMIF a écrit :
> > > Hi Yuriy, > > > > > > On Wed Aug 14 09:17:50 2013, yoreek wrote:
> > > > Hi! > > > > > > > > XML parser dos not store line numbers by default. > > > > > > > > In order to have line numbers information, you need use > > > > "XML_LIBXML_LINENUMBERS" option, for example: > > > > > > > > my $parser = XML::LibXML->new( XML_LIBXML_LINENUMBERS => 1 ); > > > > > > > > or > > > > > > > > $parser->line_numbers(1); > > > > > > > > -- > > > > Yuriy
> > > > > > thanks! I can verify that with the attached file a line number > > > indeed > > > gets displayed. Resolving. > > > > > > Regards, > > > > > > -- Shlomi Fish
> > > > Dear Shlomi, > > > > Can you active it by default in your module. It will be a nice idea.
> > I'd rather not activate it by default because it may break existing > code (backwards compatibility), may make XML::LibXML slower by > default, and may not reflect the API of libxml2 properly. >
> > Or, if you does not want to active it by default, change the POD > > docmentation to explain the way to active it.
> > It is explained here: > > https://metacpan.org/module/SHLOMIF/XML-LibXML- > 2.0101/lib/XML/LibXML/Parser.pod > > Regards, > > -- Shlomi Fish >
> > > > Best regards, > > > > djibel
OK, Thank you !