Subject: | XML::LibXML::Schema->validate rejects XML::LibXML::DocumentFragment objects with confusing message |
I'm in the process of updating our perl build (perl 5.8.8 => 5.20; XML::LibXML 1.66 => XML-LibXML-2.0126). An XML validation script was doing the following:
my $parser = XML::LibXML->new;
my $schema = XML::LibXML::Schema->new(string => $schema_string);
my $actions_doc = $parser->parse_balanced_chunk($actionsSnippet);
$schema->validate($actions_doc);
This appeared to work fine with XML::LibXML 1.66 but with XML-LibXML-2.0126, validate produced the unhelpful exception 'API Error'.
I believe I've traced this change in behavior to
https://rt.cpan.org/Public/Bug/Display.html?id=93496 . This change limits the 'validate' API to XML::LibXML::Document and XML::LibXML::Element objects, rejecting the XML::LibXML::DocumentFragment object produced by parse_balanced_chunk (that it used to handle).
It's not clear to me if should be generally supported, or if a better error message should be produced. I've attached a patch with both options.
Subject: | schema_validate_doc_fragment.patch |
--- XML-LibXML-2.0126/XML-LibXML/LibXML.xs 2016-11-03 00:16:14.000000000 -0400
+++ XML-LibXML-2.0126.patch/XML-LibXML/LibXML.xs 2016-11-03 00:16:14.000000000 -0400
@@ -7575,11 +7575,14 @@
(xmlSchemaValidityWarningFunc)LibXML_error_handler_ctx,
saved_error );
- if (node->type == XML_DOCUMENT_NODE) {
+ if (node->type == XML_DOCUMENT_NODE
+ || node->type == XML_DOCUMENT_FRAG_NODE) {
RETVAL = xmlSchemaValidateDoc(vctxt, (xmlDocPtr)node);
}
- else {
+ else if (node->type == XML_ELEMENT_NODE) {
RETVAL = xmlSchemaValidateOneElement(vctxt, node);
+ } else {
+ croak( "can only validate documents or document elements" );
}
xmlSchemaFreeValidCtxt( vctxt );