pub struct Document { /* private fields */ }Expand description
A parsed XML document.
Nodes are numbered in document order, from the document node at 0.
Every node and attribute has the Span of the source it came from;
a node an entity reference produced has the span of that reference.
Implementations§
Source§impl Document
impl Document
Sourcepub fn to_xml(&self) -> String
pub fn to_xml(&self) -> String
The whole document as XML: the XML declaration (without an
encoding, since this is a string), the <!DOCTYPE> with its
internal subset as written, and the tree. Entity references come
out expanded, but for those to entities Parsec didn’t read.
Attributes the DTD defaulted are left to the DTD.
let src = r#"<feed xmlns="http://www.w3.org/2005/Atom" xmlns:m="urn:m"><m:x a="1 & 2"/></feed>"#;
let doc = parsec_xml::parse(src).unwrap();
assert_eq!(doc.to_xml(), src);Source§impl Document
impl Document
Sourcepub fn root_element(&self) -> NodeId
pub fn root_element(&self) -> NodeId
The document element.
Sourcepub fn node(&self, index: usize) -> Option<NodeId>
pub fn node(&self, index: usize) -> Option<NodeId>
The node at index, as NodeId::index numbers them, if there
is one: for a node number that came from outside, such as the C
API’s.
let doc = parsec_xml::parse("<a/>").unwrap();
assert_eq!(doc.node(1), Some(doc.root_element()));
assert_eq!(doc.node(2), None);pub fn kind(&self, node: NodeId) -> NodeKind<'_>
pub fn is_element(&self, node: NodeId) -> bool
Sourcepub fn span(&self, node: NodeId) -> Span
pub fn span(&self, node: NodeId) -> Span
Where the node came from: an element’s from its start tag’s < to
its end tag’s >.
pub fn parent(&self, node: NodeId) -> Option<NodeId>
pub fn first_child(&self, node: NodeId) -> Option<NodeId>
pub fn last_child(&self, node: NodeId) -> Option<NodeId>
pub fn next_sibling(&self, node: NodeId) -> Option<NodeId>
pub fn prev_sibling(&self, node: NodeId) -> Option<NodeId>
pub fn children(&self, node: NodeId) -> impl Iterator<Item = NodeId> + '_
Sourcepub fn ancestors(&self, node: NodeId) -> impl Iterator<Item = NodeId> + '_
pub fn ancestors(&self, node: NodeId) -> impl Iterator<Item = NodeId> + '_
The node’s ancestors, nearest first, the document node last.
Sourcepub fn descendants(&self, node: NodeId) -> impl Iterator<Item = NodeId> + '_
pub fn descendants(&self, node: NodeId) -> impl Iterator<Item = NodeId> + '_
The node and everything under it, in document order.
Sourcepub fn attributes(
&self,
node: NodeId,
) -> impl ExactSizeIterator<Item = Attribute<'_>> + '_
pub fn attributes( &self, node: NodeId, ) -> impl ExactSizeIterator<Item = Attribute<'_>> + '_
An element’s attributes, in the order of its start tag, then the defaults the DTD supplied.
Sourcepub fn attribute(
&self,
node: NodeId,
namespace: &str,
local: &str,
) -> Option<&str>
pub fn attribute( &self, node: NodeId, namespace: &str, local: &str, ) -> Option<&str>
The value of the attribute with this namespace ("" for none)
and local name.
Sourcepub fn attribute_named(&self, node: NodeId, qname: &str) -> Option<&str>
pub fn attribute_named(&self, node: NodeId, qname: &str) -> Option<&str>
The value of the attribute with this name as written, prefix and all.
Sourcepub fn text(&self, node: NodeId) -> String
pub fn text(&self, node: NodeId) -> String
The text of the node and everything under it, as XPath’s
string() reads it: character data and CDATA, in order.
Sourcepub fn lookup_namespace(&self, node: NodeId, prefix: &str) -> Option<&str>
pub fn lookup_namespace(&self, node: NodeId, prefix: &str) -> Option<&str>
The namespace prefix ("" for the default namespace) is bound to
at node, from the declarations on it and its ancestors.
Sourcepub fn xml_lang(&self, node: NodeId) -> Option<&str>
pub fn xml_lang(&self, node: NodeId) -> Option<&str>
The node’s language: the nearest xml:lang on it or above it.
Sourcepub fn preserves_space(&self, node: NodeId) -> bool
pub fn preserves_space(&self, node: NodeId) -> bool
Whether white space at the node is to be kept: the nearest
xml:space on it or above it is preserve.
pub fn doctype(&self) -> Option<&Doctype>
Sourcepub fn entities(&self) -> &[EntityDecl]
pub fn entities(&self) -> &[EntityDecl]
The entities the internal subset declared, in order, the first declaration of each name only (later ones don’t count).
pub fn notations(&self) -> &[Notation]
Sourcepub fn warnings(&self) -> &[Diagnostic]
pub fn warnings(&self) -> &[Diagnostic]
What the parser didn’t read and says so, as the XML spec asks of a processor that doesn’t validate: the external subset, external entities, and declarations it skipped after a parameter entity it didn’t read. None of these is an error.
Sourcepub fn namespaces(&self) -> bool
pub fn namespaces(&self) -> bool
Whether the document was parsed with namespaces on.
Trait Implementations§
Source§impl Tree for Document
Names compare exactly, prefixes resolve to the namespaces the document
declared, and namespace declarations are namespace nodes, not
attributes. With namespaces off (crate::Options::namespaces), every
name is its own local name in no namespace and xmlns attributes are
attributes like any other.
The doctype isn’t in the tree XPath reads, as XPath’s data model has
no node for it. An EntityRef is a node
XPath has no kind for, so no node test matches it.
impl Tree for Document
Names compare exactly, prefixes resolve to the namespaces the document
declared, and namespace declarations are namespace nodes, not
attributes. With namespaces off (crate::Options::namespaces), every
name is its own local name in no namespace and xmlns attributes are
attributes like any other.
The doctype isn’t in the tree XPath reads, as XPath’s data model has
no node for it. An EntityRef is a node
XPath has no kind for, so no node test matches it.
Source§fn namespace_declaration(&self, n: Id, i: u32) -> Option<(&str, &str)>
fn namespace_declaration(&self, n: Id, i: u32) -> Option<(&str, &str)>
xmlns="" comes back with an empty URI, which takes the default
namespace away; parsec-xpath leaves no namespace node for it.
fn kind(&self, n: Id) -> NodeType
fn parent(&self, n: Id) -> Option<Id>
fn first_child(&self, n: Id) -> Option<Id>
fn next_sibling(&self, n: Id) -> Option<Id>
fn prev_sibling(&self, n: Id) -> Option<Id>
Source§fn qualified_name(&self, n: Id) -> &str
fn qualified_name(&self, n: Id) -> &str
name()
gives. Lowercase in HTML.Source§fn local_name(&self, n: Id) -> &str
fn local_name(&self, n: Id) -> &str
local-name() gives.Source§fn namespace_uri(&self, n: Id) -> &str
fn namespace_uri(&self, n: Id) -> &str
Source§fn attribute_count(&self, n: Id) -> u32
fn attribute_count(&self, n: Id) -> u32
xmlns and xmlns:* are namespace nodes rather than attributes, so
an XML tree leaves them out of these. An HTML tree keeps them, as
libxml2’s HTML parser does.Source§fn attribute(&self, n: Id, i: u32) -> Option<Attr<'_>>
fn attribute(&self, n: Id, i: u32) -> Option<Attr<'_>>
ith attribute, in document order. A query reads
attributes by place, so this should take constant time.Source§fn namespace_declaration_count(&self, n: Id) -> u32
fn namespace_declaration_count(&self, n: Id) -> u32
n declares: an XML element’s xmlns
and xmlns:* attributes. A root that isn’t an element may declare
the namespaces in scope where its tree was cut from, which its
elements then inherit. The namespace axis works out what’s in scope
from these. None by default, as an HTML tree has none: its elements
have only the xml namespace, which is always in scope.Source§fn data(&self, n: Id) -> &str
fn data(&self, n: Id) -> &str
Source§fn is_xml(&self) -> bool
fn is_xml(&self) -> bool
Source§fn id(&self, n: Id) -> Option<&str>
fn id(&self, n: Id) -> Option<&str>
id() finds an element by: HTML’s id, or XML’s
xml:id or an attribute the DTD declares an ID.Source§fn lang(&self, n: Id) -> Option<&str>
fn lang(&self, n: Id) -> Option<&str>
lang(): xml:lang, or HTML’s lang.