pub struct Dtd {
pub elements: HashMap<String, ElementDecl>,
pub element_order: Vec<String>,
pub attlists: HashMap<String, Vec<AttDecl>>,
pub root_name: String,
pub public_id: Option<String>,
pub system_id: Option<String>,
pub unparsed_entities: HashMap<String, UnparsedEntity>,
pub entities: Vec<EntityDecl>,
pub decl_order: Vec<DeclRef>,
pub internal_decls: Vec<String>,
pub internal_subset_prolog_index: u32,
}Expand description
Parsed DTD content used by [validate]. Built up incrementally
by the bytes-reader as it encounters <!ELEMENT> / <!ATTLIST>
declarations in the internal subset.
Fields§
§elements: HashMap<String, ElementDecl>Element declarations keyed by element name.
element_order: Vec<String>Element names in declaration order — the order libxml2 keeps in
xmlDtd.children, which lxml’s DTD.elements() exposes. The
elements map is for lookup; this preserves source order.
attlists: HashMap<String, Vec<AttDecl>>Attribute declarations keyed by element name. libxml2’s
ATTLIST groups can append (multiple <!ATTLIST elem ...> for
the same elem are merged here).
root_name: StringRoot element name from the DOCTYPE header: <!DOCTYPE root_name ...>. Populated for any successfully-parsed doctype, even
one with no internal subset and no external ID. Empty when
the document has no DOCTYPE at all.
public_id: Option<String>PUBLIC "..." identifier from the DOCTYPE header, when
present. None for SYSTEM-only or no external ID.
system_id: Option<String>SYSTEM "..." identifier (or the second literal of a PUBLIC "..." "..." form) from the DOCTYPE header. None when the
header has no external ID.
unparsed_entities: HashMap<String, UnparsedEntity>Unparsed external general entities — those declared with an
NDATA annotation per XML 1.0 § 4.2.2. Keyed by entity name;
the value carries the SYSTEM identifier (the URI a non-XML
processor would fetch) and the PUBLIC identifier when present.
Used by XSLT’s unparsed-entity-uri() /
unparsed-entity-public-id() functions (XSLT 1.0 § 12.4).
entities: Vec<EntityDecl>General <!ENTITY> declarations in source order — what lxml’s
DTD.entities() exposes and the DTD serializer reconstructs.
Carries internal, external, and unparsed entities (a superset of
unparsed_entities) plus parameter
entities (flagged parameter).
decl_order: Vec<DeclRef>Declaration references in source order, so the DTD serializer can
reproduce libxml2’s xmlDtd.children ordering.
internal_decls: Vec<String>Raw markup declarations from the internal subset, in document
order, each as it appeared in source (<!ENTITY …>,
<!ELEMENT …>, <!ATTLIST …>, <!NOTATION …>). Captured for
round-trip serialization of the DOCTYPE’s [ … ] body; empty
when the document had no internal subset. Only declarations
read directly from the source are captured (not those produced
by parameter-entity expansion).
internal_subset_prolog_index: u32Number of document-level comments/PIs that preceded the
<!DOCTYPE …> in the prolog. Lets the compat layer splice the
internal-subset node into the document’s sibling chain at its
true position (misc[..k] → DOCTYPE → misc[k..] → root) so a
comment that came before the DOCTYPE serializes before it, as
libxml2 does. Zero when the DOCTYPE was the first prolog item
(the common case) or when there is no DOCTYPE.
Implementations§
Source§impl Dtd
impl Dtd
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
true when no element or attribute declarations have been
captured. Used as a fast-path skip in the validator.
Sourcepub fn add_element(&mut self, decl: ElementDecl)
pub fn add_element(&mut self, decl: ElementDecl)
Insert (or replace) an element declaration. libxml2’s
behaviour on duplicate <!ELEMENT name ...> for the same
name is to emit a warning and keep the first declaration;
we keep the first too (this matches the de-facto standard
across major parsers).
Sourcepub fn add_attlist(&mut self, element: String, decls: Vec<AttDecl>)
pub fn add_attlist(&mut self, element: String, decls: Vec<AttDecl>)
Append attribute declarations for an element. Multiple
<!ATTLIST elem ...> blocks merge — XML 1.0 § 3.3.