Repository navigation
ext/dom: Add Dom\XMLSchema to reuse a compiled XML Schema. - #25437
Draft
ArtUkrainskiy wants to merge 1 commit into
Draft
ArtUkrainskiy wants to merge 1 commit into
ArtUkrainskiy wants to merge 1 commit into
Conversation
Dom\XMLSchema::createFromFile()/createFromString() compile the schema once, validate() checks a Dom\XMLDocument or DOMDocument with a fresh validation context. schemaValidate() now shares the compile and validate helpers.
Contributor
Author
|
@devnexen you're the code owner for ext/dom, so I'd love your take before I write an RFC. Mostly the three API questions in the description. Does a separate Dom\XMLSchema class look like the right direction to you? |
Member
|
I think it s a viable feature. yes yes and yes. See you in the RFC ;) |
ndossche
reviewed
Nov 15, 2026
| docp = (xmlDocPtr) dom_object_get_node(intern); | ||
| bool is_valid = dom_schema_validate_doc(sptr, docp, flags); | ||
| xmlSchemaFree(sptr); | ||
| if (EG(exception)) { |
Member
There was a problem hiding this comment.
You don't need this check, the VM/JIT handles return values in presence of exceptions properly already
|
|
||
| static inline dom_xml_schema_object *dom_xml_schema_from_obj(zend_object *obj) | ||
| { | ||
| return (dom_xml_schema_object *) ((char *) obj - offsetof(dom_xml_schema_object, std)); |
| } | ||
| /* }}} end dom_document_relaxNG_validate_xml */ | ||
|
|
||
| /* Dom\XMLSchema: a compiled XML Schema that validates many documents. */ |
Member
There was a problem hiding this comment.
If possible, moving this to a new file would be cleaner
| ZEND_ASSERT(schema->schema != NULL); | ||
|
|
||
| bool is_valid = dom_schema_validate_doc(schema->schema, docp, flags); | ||
| if (EG(exception)) { |
Member
There was a problem hiding this comment.
Ditto about exceptions&return values
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is a proof of concept. I'd like feedback on the API before writing an RFC.
schemaValidate()compiles the XSD on every call, validates, and frees the schema. For a real schema with imports, that compile is almost the whole cost. Apps that check many invoices or orders against one schema pay it on every document.This adds
Dom\XMLSchema, which owns the compiledxmlSchemaPtr:schemaValidate()now uses the same compile and validate helpers. Its behaviour doesn't change.The test schema is UBL 2.1 Invoice, an OASIS standard for e-invoices. Its main file imports shared type definitions, so compiling it reads 14 XSD files, 2.8 MB in total. The schema and the 1.2 KB and 20 KB invoices are the official ones from https://docs.oasis-open.org/ubl/os-UBL-2.1/UBL-2.1.zip. The larger invoices are the 20 KB example with its 5 item lines copied over and over until the invoice has 100, 1,000 or 10,000 lines. They still pass the schema.
Time to load one document and validate it:
Execution time, median of 9 alternating runs. Under callgrind one 20 KB validation is 433M instructions with
schemaValidate()and 1.07M withvalidate().schemaValidate()itself stays within noise after the refactor, at −0.06% instructions.Tests for ext/dom, ext/libxml and ext/xmlreader pass, and valgrind is clean on the new tests.
A few things I'm not sure about.
createFromString()has no base URI, so relative imports resolve against the CWD, same asschemaValidateSource(). Should it take a?string $baseUri?DOMDocument, or onlyDom\XMLDocument?RelaxNG and
XMLReader::setSchema()could follow the same pattern later.I haven't tried other compilers, other real-world schemas or an ASan build yet.