Skip to main content

Module validate

Module validate 

Source
Expand description

DTD validation walk.

Given a Document and its captured Dtd, check every element and attribute against the declarations. Returns the first error encountered (libxml2 stops at first fatal error in xmlValidateDocument too).

§Algorithm

Two passes:

  1. Tree walk — for each element node:
    • Look up its <!ELEMENT> decl. If absent in a non-empty DTD, that’s an error (libxml2’s “No declaration for element foo”).
    • Match its child sequence against the content model.
    • Check each attribute against the corresponding <!ATTLIST>: required ones present, enumerated values valid, FIXED values equal, IDs unique, IDREFs recorded for pass 2.
  2. Cross-ref pass — every IDREF target collected in pass 1 must equal some ID seen anywhere in pass 1.

The content-model matcher is a small recursive descent over the Group tree. We don’t bother with NFA→DFA conversion: real DTD content models are tiny (rarely more than a few dozen particles), and recursive matching with greedy/lazy fallback on ?/*/+ handles them in microseconds.

Structs§

DtdError
One DTD validation failure. Mirrors libxml2’s per-error structure shape just enough for the compat layer to surface.

Functions§

is_valid
Convenience for the common “did it validate?” yes/no question. Equivalent to validate(doc, dtd).is_ok().
validate
Validate doc against dtd. Returns Ok(()) if every declaration was satisfied; otherwise an Err carrying every error found in document order.