Skip to main content

Document

Struct Document 

Source
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

Source

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 &amp; 2"/></feed>"#;
let doc = parsec_xml::parse(src).unwrap();
assert_eq!(doc.to_xml(), src);
Source

pub fn write_xml(&self, node: NodeId, out: &mut String)

A node and everything under it as XML. An element written alone takes along the namespace declarations it’s in the scope of, and the attributes the DTD defaulted on it and under it, so that it means the same on its own.

Source§

impl Document

Source

pub fn root(&self) -> NodeId

The document node, the root of the tree.

Source

pub fn root_element(&self) -> NodeId

The document element.

Source

pub fn len(&self) -> usize

How many nodes there are, the document node included.

Source

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);
Source

pub fn is_empty(&self) -> bool

Always false: there is at least the document node.

Source

pub fn kind(&self, node: NodeId) -> NodeKind<'_>

Source

pub fn is_element(&self, node: NodeId) -> bool

Source

pub fn name(&self, node: NodeId) -> Option<Name<'_>>

An element’s name.

Source

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 >.

Source

pub fn parent(&self, node: NodeId) -> Option<NodeId>

Source

pub fn first_child(&self, node: NodeId) -> Option<NodeId>

Source

pub fn last_child(&self, node: NodeId) -> Option<NodeId>

Source

pub fn next_sibling(&self, node: NodeId) -> Option<NodeId>

Source

pub fn prev_sibling(&self, node: NodeId) -> Option<NodeId>

Source

pub fn children(&self, node: NodeId) -> impl Iterator<Item = NodeId> + '_

Source

pub fn ancestors(&self, node: NodeId) -> impl Iterator<Item = NodeId> + '_

The node’s ancestors, nearest first, the document node last.

Source

pub fn descendants(&self, node: NodeId) -> impl Iterator<Item = NodeId> + '_

The node and everything under it, in document order.

Source

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.

Source

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.

Source

pub fn attribute_named(&self, node: NodeId, qname: &str) -> Option<&str>

The value of the attribute with this name as written, prefix and all.

Source

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.

Source

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.

Source

pub fn xml_lang(&self, node: NodeId) -> Option<&str>

The node’s language: the nearest xml:lang on it or above it.

Source

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.

Source

pub fn xml_decl(&self) -> Option<&XmlDecl>

The XML declaration, if the document has one.

Source

pub fn doctype(&self) -> Option<&Doctype>

Source

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).

Source

pub fn notations(&self) -> &[Notation]

Source

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.

Source

pub fn namespaces(&self) -> bool

Whether the document was parsed with namespaces on.

Trait Implementations§

Source§

impl Debug for Document

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
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.

Source§

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.

Source§

fn len(&self) -> usize

How many node ids there are, including those of removed nodes.
Source§

fn kind(&self, n: Id) -> NodeType

Source§

fn parent(&self, n: Id) -> Option<Id>

Source§

fn first_child(&self, n: Id) -> Option<Id>

Source§

fn next_sibling(&self, n: Id) -> Option<Id>

Source§

fn prev_sibling(&self, n: Id) -> Option<Id>

Source§

fn qualified_name(&self, n: Id) -> &str

An element’s name as written, with any prefix: what name() gives. Lowercase in HTML.
Source§

fn local_name(&self, n: Id) -> &str

An element’s name without its prefix: what local-name() gives.
Source§

fn namespace_uri(&self, n: Id) -> &str

An element’s namespace URI; “” for no namespace.
Source§

fn tag_key(&self, n: Id) -> TagKey<'_>

What crate::Index keys an element’s name tests on.
Source§

fn attribute_count(&self, n: Id) -> u32

How many attributes an element has. In XPath’s data model, 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<'_>>

An element’s 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

How many namespaces node 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

A text, CDATA section, comment or processing instruction’s text.
Source§

fn pi_target(&self, n: Id) -> Option<&str>

A processing instruction’s target.
Source§

fn is_xml(&self) -> bool

Whether names compare exactly (XML), rather than ignoring ASCII case with prefixes left as part of the name (HTML).
Source§

fn id(&self, n: Id) -> Option<&str>

The value 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>

The language an element declares, if it declares one, for lang(): xml:lang, or HTML’s lang.
Source§

fn is_empty(&self) -> bool

Auto Trait Implementations§

§

impl !Freeze for Document

§

impl Forget for Document

§

impl Move for Document

§

impl RefUnwindSafe for Document

§

impl Send for Document
where RawVecInner: Send, BoxRaw<str>: Send, RawTable<(Box<str>, u32)>: Send, RawTable<(u32, u32)>: Send, OnceLock<Declared>: Send, &'static str: Send, String: Send,

§

impl Sync for Document
where RawVecInner: Sync, BoxRaw<str>: Sync, RawTable<(Box<str>, u32)>: Sync, RawTable<(u32, u32)>: Sync, OnceLock<Declared>: Sync, String: Sync,

§

impl Unpin for Document
where *const u8 is !null: Unpin, Box<str>: Unpin, Align4<u32>: Unpin, &'static str: Unpin, String: Unpin,

§

impl UnsafeUnpin for Document

§

impl UnwindSafe for Document
where RawVecInner: UnwindSafe, BoxRaw<str>: UnwindSafe, HashMap<Box<str>, u32>: UnwindSafe, HashMap<u32, u32>: UnwindSafe, OnceLock<Declared>: UnwindSafe, &'static str: UnwindSafe, String: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.