shlomif / perl-XML-LibXML

The XML-LibXML CPAN Distribution for Processing XML using the libxml2 library
https://metacpan.org/release/XML-LibXML
Other
17 stars 35 forks source link

Enhance XML::Lib::Error->nodename: include xpath route to the node #20

Open dblanch opened 6 years ago

dblanch commented 6 years ago

Hi

As far as I know when validating an xml document with schema, that is:

eval { $xsd->validate( $dom ) };

you get an XML::Lib::Error object if it fails validating a field datatype. This Error object has the field:

$@->nodename()

that only shows the name of the node where the error occurred.

I kindly request an enhancement: to include the xpath route to the node.

With this information, tools can find the exact node where the error is, which is very helpful to show the user which field is wrong, the previous value .. etc.

This is what the documentation says about nodename field:

nodename $nodename = $@->nodename(); Name of the node where error occurred, if available. When this field is non-empty, libxml2 actually returned a physical pointer to the specified node. Due to memory management issues, it is very difficult to implement a way to expose the pointer to the Perl level as a XML::LibXML::Node. For this reason, XML::LibXML::Error currently only exposes the name the node.

Perhaps the xpath route could be shown instead.

shlomif commented 5 years ago

Sorry for the late reply. I will accept a patch/PR adding the xpath route as a different slot/field of $@ as long as it adds docs and tests.