pub struct Document { /* private fields */ }Expand description
A parsed XML part stored in flat contiguous arenas (nodes, child indices, attributes, and a single UTF-8 string buffer with interned tag and attribute names).
Implementations§
Source§impl Document
impl Document
Sourcepub fn parse_fragment(
bytes: &[u8],
in_scope: &[(String, String)],
limits: &Limits,
) -> Result<Fragment, Error>
pub fn parse_fragment( bytes: &[u8], in_scope: &[(String, String)], limits: &Limits, ) -> Result<Fragment, Error>
Parses bytes as the content of an element at which the namespace declarations
in_scope are in scope, such as the bytes of an element’s span or the new XML of an
edit in its place: each prefix ("" for the default namespace) with its URI, as
Document::namespaces_in_scope gives them. Each URI binds as given: the reader binds
a namespace to its declaration’s value as written, with no reference resolved, so a
URI of a part binds the same in the fragment. A prefix that neither the fragment nor
in_scope declares is an error here, unlike in a part, because the fragment is to
stand in a part where it must read.
§Errors
Returns Error::UndeclaredPrefix for a prefix without a declaration, and every error
of Document::parse; a prefix of in_scope that is no name, a URI that holds both
quotation marks, and a fragment that closes the element that it stands in, are
Error::Syntax.
Sourcepub fn parse_fragment_with_aliases_at(
bytes: &[u8],
in_scope: &[(String, String)],
aliases: &[(&str, &str)],
base_offset: u64,
limits: &Limits,
) -> Result<Fragment, Error>
pub fn parse_fragment_with_aliases_at( bytes: &[u8], in_scope: &[(String, String)], aliases: &[(&str, &str)], base_offset: u64, limits: &Limits, ) -> Result<Fragment, Error>
Parses bytes as a slice of a larger XML part starting at base_offset, resolving
namespace declarations in_scope and namespace aliases, while recording undeclared
prefixes on Document::undeclared_prefixes instead of failing.
Node byte spans returned by Document::span are shifted by base_offset so they match
exact byte offsets in the enclosing part.
§Errors
Returns the errors of Document::parse_with_aliases, or Error::Syntax when
in_scope contains an invalid prefix/URI or bytes closes the synthetic container.
Source§impl Document
impl Document
Sourcepub fn parse(bytes: &[u8], limits: &Limits) -> Result<Self, Error>
pub fn parse(bytes: &[u8], limits: &Limits) -> Result<Self, Error>
Parses a part.
§Errors
Returns an error when the XML is malformed, has a DOCTYPE, or breaks a limit.
Sourcepub fn parse_with_aliases(
bytes: &[u8],
limits: &Limits,
aliases: &[(&str, &str)],
) -> Result<Self, Error>
pub fn parse_with_aliases( bytes: &[u8], limits: &Limits, aliases: &[(&str, &str)], ) -> Result<Self, Error>
Parses a part and resolves each namespace URI on the left of an alias pair to the URI on the right, so that two spellings of one vocabulary read the same. The namespace declarations stay in the element attributes as written.
§Errors
Returns an error when the XML is malformed, has a DOCTYPE, or breaks a limit.
Sourcepub fn root(&self) -> Result<NodeId, Error>
pub fn root(&self) -> Result<NodeId, Error>
Returns the root element.
§Errors
Returns Error::NoRoot when the document has no element at the top level.
Sourcepub fn element(&self, id: NodeId) -> Option<Element<'_>>
pub fn element(&self, id: NodeId) -> Option<Element<'_>>
Returns the element at a node, or None for other nodes.
Sourcepub fn text(&self, id: NodeId) -> Option<&str>
pub fn text(&self, id: NodeId) -> Option<&str>
Returns the text at a node, or None for other nodes.
Sourcepub fn children(&self, id: NodeId) -> &[NodeId]
pub fn children(&self, id: NodeId) -> &[NodeId]
Returns the children of a node in document order.
Sourcepub fn child_elements(&self, id: NodeId) -> impl Iterator<Item = NodeId> + '_
pub fn child_elements(&self, id: NodeId) -> impl Iterator<Item = NodeId> + '_
Returns the child elements of a node in document order.
Sourcepub fn set_children(&mut self, id: NodeId, children: Vec<NodeId>)
pub fn set_children(&mut self, id: NodeId, children: Vec<NodeId>)
Replaces the children of a node, in document order. The new children are nodes of this document, so that an edit can drop, keep and move nodes; a node that no parent lists any more drops out of the tree. Nothing changes for a node that the document lacks.
Sourcepub fn set_attribute(
&mut self,
id: NodeId,
namespace: Option<&str>,
local: &str,
value: &str,
)
pub fn set_attribute( &mut self, id: NodeId, namespace: Option<&str>, local: &str, value: &str, )
Sets an attribute of an element, so that an edit can leave a note on it: the value of
an attribute with this namespace URI and local name changes, or the attribute is added.
Pass None as the namespace for an unprefixed attribute. Nothing changes for a node that
is no element.
Sourcepub fn undeclared_prefixes(&self) -> &[String]
pub fn undeclared_prefixes(&self) -> &[String]
Returns the namespace prefixes that the part uses on an element or an attribute without
a declaration in scope, which XML’s namespaces forbid, each once in the order of its
first use. xml and xmlns need no declaration. Such a name keeps its prefix and has
no namespace: an element of it is none that Document::is or Document::child
finds, and an attribute of it reads as an attribute without a prefix.
Sourcepub fn prefix_namespace(&self, id: NodeId, prefix: &str) -> Option<&str>
pub fn prefix_namespace(&self, id: NodeId, prefix: &str) -> Option<&str>
Returns the namespace that the prefix prefix ("" for the default namespace) is
bound to at the element id: by the nearest declaration of the element or of an
ancestor, as the part was parsed, after the aliases of Document::parse_with_aliases.
The prefix xml is bound as XML defines it. None for a prefix that no declaration in
scope binds, for a declaration of the empty name, and for a node that is no element.
A format crate reads a prefix in an attribute’s value through it, such as the namespaces
that a choice of alternate content requires.
Sourcepub fn span(&self, id: NodeId) -> Result<Range<usize>, Error>
pub fn span(&self, id: NodeId) -> Result<Range<usize>, Error>
Returns where a node’s markup lies in the bytes that were parsed, as offsets into those
bytes, before any normalization: an element from the < of its start tag to the byte
after the > of its end tag or of its empty tag; a text node from its first byte to the
byte after its last, with the character and entity references and the CDATA sections
that it joins; and a comment, a processing instruction or the XML declaration as
written. A span keeps the bytes as parsed: Document::set_children does not change
it. Replacing the bytes of a span replaces the node in the part, so that an edit keeps
every other byte as it was.
§Errors
Returns Error::NotUtf8 when the part is not in UTF-8, and Error::NoSuchNode for
a node that the document lacks.
Sourcepub fn namespaces_in_scope(&self, id: NodeId) -> Vec<(String, String)>
pub fn namespaces_in_scope(&self, id: NodeId) -> Vec<(String, String)>
Returns the namespace declarations in scope at the element id, its own among them:
each prefix ("" for the default namespace) with the URI that it binds after the
aliases of the parse, from the nearest declaration of the prefix only, the element’s
own first and then outward. Empty for a node that is no element. A fragment that takes
the place of the element’s content, or of a child, is parsed with them
(Document::parse_fragment).
Sourcepub fn namespace_uri(&self, id: NamespaceId) -> &str
pub fn namespace_uri(&self, id: NamespaceId) -> &str
Returns the URI of an interned namespace.
Sourcepub fn is(&self, id: NodeId, namespace: &str, local: &str) -> bool
pub fn is(&self, id: NodeId, namespace: &str, local: &str) -> bool
Tells whether a node is an element with this namespace URI and local name.
Sourcepub fn child(&self, id: NodeId, namespace: &str, local: &str) -> Option<NodeId>
pub fn child(&self, id: NodeId, namespace: &str, local: &str) -> Option<NodeId>
Returns the first child element with this namespace URI and local name.
Sourcepub fn attribute(
&self,
id: NodeId,
namespace: Option<&str>,
local: &str,
) -> Option<&str>
pub fn attribute( &self, id: NodeId, namespace: Option<&str>, local: &str, ) -> Option<&str>
Returns an attribute value. Pass None as the namespace for an unprefixed attribute.
Sourcepub fn text_content(&self, id: NodeId) -> String
pub fn text_content(&self, id: NodeId) -> String
Returns all text below a node, in document order.