From: Tim Wiederhake Date: Wed, 30 Sep 2020 11:55:02 +0000 (+0200) Subject: util: Allow validation for single XML node X-Git-Tag: v6.9.0-rc1~287 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9faa31ce79f1f63ba091a9a8edd8b7c54395ee10;p=thirdparty%2Flibvirt.git util: Allow validation for single XML node Validation is usually performed on an entire document. If we are only interested in validating a single nested node that can occur in different contexts, this would require writing different schemas for any of those different contexts. By temporarily replacing the document's root node, we can validate the relevant node only. Signed-off-by: Tim Wiederhake Reviewed-by: Peter Krempa --- diff --git a/src/util/virxml.c b/src/util/virxml.c index b4d6684560..8a94de8fe4 100644 --- a/src/util/virxml.c +++ b/src/util/virxml.c @@ -1315,6 +1315,21 @@ virXMLValidateAgainstSchema(const char *schemafile, } +int +virXMLValidateNodeAgainstSchema(const char *schemafile, + xmlDocPtr doc, + xmlNodePtr node) +{ + xmlNodePtr root; + int ret; + + root = xmlDocSetRootElement(doc, node); + ret = virXMLValidateAgainstSchema(schemafile, doc); + xmlDocSetRootElement(doc, root); + return ret; +} + + void virXMLValidatorFree(virXMLValidatorPtr validator) { diff --git a/src/util/virxml.h b/src/util/virxml.h index 0301f15308..fd0d30fcec 100644 --- a/src/util/virxml.h +++ b/src/util/virxml.h @@ -212,6 +212,12 @@ virXMLValidatorValidate(virXMLValidatorPtr validator, int virXMLValidateAgainstSchema(const char *schemafile, xmlDocPtr xml); + +int +virXMLValidateNodeAgainstSchema(const char *schemafile, + xmlDocPtr doc, + xmlNodePtr node); + void virXMLValidatorFree(virXMLValidatorPtr validator);