glexml/dtd
Validate parsed XML documents against a DTD.
validate checks a glexml.Document against the declarations of a
glexml.Dtd: element content models, attribute declarations (including
required attributes, fixed values, enumerations, and ID/IDREF
integrity), and notation references. with_default_attributes applies
the attribute defaults a validating processor would supply.
import glexml
import glexml/dtd
let assert Ok(document) = glexml.parse(source)
let assert Some(doctype) = document.doctype
case dtd.validate(document, doctype.declarations) {
[] -> // valid
violations -> // each one says what is wrong and where
}
Types
The specific validity problem.
pub type Problem {
RootElementMismatch(expected: String, found: String)
UndeclaredElement(element: String)
InvalidContent(element: String, model: String)
TextNotAllowed(element: String)
UndeclaredAttribute(element: String, attribute: String)
MissingRequiredAttribute(element: String, attribute: String)
InvalidAttributeValue(
element: String,
attribute: String,
value: String,
expected: String,
)
DuplicateId(id: String)
UnknownIdReference(id: String)
UndeclaredEntity(entity: String)
NotStandalone(description: String)
ImproperPeNesting(description: String)
InvalidDtdDeclaration(element: String, description: String)
}
Constructors
-
RootElementMismatch(expected: String, found: String)The root element is not the one named by the DOCTYPE.
-
UndeclaredElement(element: String)The element has no
<!ELEMENT>declaration. -
InvalidContent(element: String, model: String)The element’s children do not match its declared content model.
-
TextNotAllowed(element: String)Character data appears where the content model allows only elements.
-
UndeclaredAttribute(element: String, attribute: String)The attribute has no
<!ATTLIST>declaration. -
MissingRequiredAttribute(element: String, attribute: String)A
#REQUIREDattribute is missing. -
InvalidAttributeValue( element: String, attribute: String, value: String, expected: String, )The attribute value does not satisfy its declared type or
#FIXEDvalue.expecteddescribes what would be acceptable. -
DuplicateId(id: String)The same ID value appears on more than one element.
-
UnknownIdReference(id: String)An IDREF or IDREFS value that no ID in the document matches.
-
UndeclaredEntity(entity: String)A reference to an entity with no declaration. The parser only keeps such references (rather than failing) when the DTD could not have been read completely; validation reports them.
-
NotStandalone(description: String)A document declaring
standalone="yes"depends on markup declarations from outside the document, which the standalone declaration promises it does not. -
ImproperPeNesting(description: String)A markup declaration, content model group, or conditional section opens in one parameter entity’s replacement text and closes in another (the Proper Declaration/Group/Conditional Section PE Nesting validity constraints).
-
InvalidDtdDeclaration(element: String, description: String)A problem in the DTD’s own declarations, independent of the document: duplicate names in mixed content or enumerations, an undeclared notation, an ID attribute with a default, more than one ID attribute on an element, or a default value that does not satisfy the attribute’s own type.
Values
pub fn content_model_to_string(
model: glexml.ContentModel,
) -> String
Render a content model the way it would appear in an <!ELEMENT>
declaration, e.g. (head, body) or (#PCDATA | em)*.
pub fn standalone_violations(
document: glexml.Document,
external: glexml.Dtd,
) -> List(Violation)
Check the Standalone Document Declaration validity constraint: a
document declaring standalone="yes" may not depend on markup
declarations made outside it. Pass the external declarations (an
external subset loaded with glexml.parse_dtd, and any external
parameter entities); declarations also made in the document’s own
internal subset do not count as external.
Violations are reported when the document is standalone and, per section 2.9 of the specification: an element lacks an attribute whose default value is declared externally; a specified attribute value would be changed by the normalisation its externally declared type requires; or whitespace occurs directly in the content of an element whose element-only content model is declared externally.
pub fn validate(
document: glexml.Document,
dtd: glexml.Dtd,
) -> List(Violation)
Validate a document against a DTD, returning every violation found. An empty list means the document is valid.
The DTD is passed separately from the document so you can decide what to
validate against: the internal subset (doctype.declarations), a loaded
external DTD (glexml.parse_dtd), or both merged with
glexml.merge_dtds.
pub fn violation_to_string(violation: Violation) -> String
Render a violation as a human readable message.
pub fn with_default_attributes(
document: glexml.Document,
dtd: glexml.Dtd,
) -> glexml.Document
Return a copy of the document with attribute defaults from the DTD
filled in: any attribute declared with a default or #FIXED value that
is absent from an element is added, as a validating XML processor would.