Struct roxmltree::Node

source ·
pub struct Node<'a, 'd: 'a> { /* private fields */ }
Expand description

A node.

Implementations

Returns node’s type.

Checks that node is a root node.

Checks that node is an element node.

Checks that node is a processing instruction node.

Checks that node is a comment node.

Checks that node is a text node.

Returns node’s document.

Returns node’s tag name.

Returns an empty name with no namespace if the current node is not an element.

Examples
let doc = roxmltree::Document::parse("<e xmlns='http://www.w3.org'/>").unwrap();

assert_eq!(doc.root_element().tag_name().namespace(), Some("http://www.w3.org"));
assert_eq!(doc.root_element().tag_name().name(), "e");

Checks that node has a specified tag name.

Examples
let doc = roxmltree::Document::parse("<e xmlns='http://www.w3.org'/>").unwrap();

assert!(doc.root_element().has_tag_name("e"));
assert!(doc.root_element().has_tag_name(("http://www.w3.org", "e")));

assert!(!doc.root_element().has_tag_name("b"));
assert!(!doc.root_element().has_tag_name(("http://www.w4.org", "e")));

Returns node’s default namespace URI.

Examples
let doc = roxmltree::Document::parse("<e xmlns='http://www.w3.org'/>").unwrap();

assert_eq!(doc.root_element().default_namespace(), Some("http://www.w3.org"));
let doc = roxmltree::Document::parse("<e xmlns:n='http://www.w3.org'/>").unwrap();

assert_eq!(doc.root_element().default_namespace(), None);

Returns element’s namespace prefix.

Returns None:

  • if the current node is not an element
  • if the current element has a default namespace
  • if the current element has no namespace
Examples
let doc = roxmltree::Document::parse("<n:e xmlns:n='http://www.w3.org'/>").unwrap();

assert_eq!(doc.root_element().resolve_tag_name_prefix(), Some("n"));
let doc = roxmltree::Document::parse("<e xmlns:n='http://www.w3.org'/>").unwrap();

assert_eq!(doc.root_element().resolve_tag_name_prefix(), None);
let doc = roxmltree::Document::parse("<e xmlns='http://www.w3.org'/>").unwrap();

assert_eq!(doc.root_element().resolve_tag_name_prefix(), None);

Returns a prefix for a given namespace URI.

Examples
let doc = roxmltree::Document::parse("<e xmlns:n='http://www.w3.org'/>").unwrap();

assert_eq!(doc.root_element().lookup_prefix("http://www.w3.org"), Some("n"));
let doc = roxmltree::Document::parse("<e xmlns:n=''/>").unwrap();

assert_eq!(doc.root_element().lookup_prefix(""), Some("n"));

Returns an URI for a given prefix.

Examples
let doc = roxmltree::Document::parse("<e xmlns:n='http://www.w3.org'/>").unwrap();

assert_eq!(doc.root_element().lookup_namespace_uri(Some("n")), Some("http://www.w3.org"));
let doc = roxmltree::Document::parse("<e xmlns='http://www.w3.org'/>").unwrap();

assert_eq!(doc.root_element().lookup_namespace_uri(None), Some("http://www.w3.org"));

Returns element’s attribute value.

Examples
let doc = roxmltree::Document::parse("<e a='b'/>").unwrap();

assert_eq!(doc.root_element().attribute("a"), Some("b"));
let doc = roxmltree::Document::parse(
    "<e xmlns:n='http://www.w3.org' a='b' n:a='c'/>"
).unwrap();

assert_eq!(doc.root_element().attribute("a"), Some("b"));
assert_eq!(doc.root_element().attribute(("http://www.w3.org", "a")), Some("c"));

Checks that element has a specified attribute.

Examples
let doc = roxmltree::Document::parse(
    "<e xmlns:n='http://www.w3.org' a='b' n:a='c'/>"
).unwrap();

assert!(doc.root_element().has_attribute("a"));
assert!(doc.root_element().has_attribute(("http://www.w3.org", "a")));

assert!(!doc.root_element().has_attribute("b"));
assert!(!doc.root_element().has_attribute(("http://www.w4.org", "a")));

Returns element’s attributes.

Examples
let doc = roxmltree::Document::parse(
    "<e xmlns:n='http://www.w3.org' a='b' n:a='c'/>"
).unwrap();

assert_eq!(doc.root_element().attributes().len(), 2);

Calculates attribute’s position in the original document.

Note: this operation is expensive.

Examples
let doc = roxmltree::Document::parse(
    "<e xmlns:n='http://www.w3.org' a='b' n:a='c'/>"
).unwrap();

assert_eq!(doc.root_element().attribute_pos("a"),
           Some(roxmltree::TextPos::new(1, 32)));
assert_eq!(doc.root_element().attribute_pos(("http://www.w3.org", "a")),
           Some(roxmltree::TextPos::new(1, 38)));

Calculates attribute’s value position in the original document.

Note: this operation is expensive.

Examples
let doc = roxmltree::Document::parse(
    "<e xmlns:n='http://www.w3.org' a='b' n:a='c'/>"
).unwrap();

assert_eq!(doc.root_element().attribute_value_pos("a"),
           Some(roxmltree::TextPos::new(1, 35)));
assert_eq!(doc.root_element().attribute_value_pos(("http://www.w3.org", "a")),
           Some(roxmltree::TextPos::new(1, 43)));

Returns element’s namespaces.

Examples
let doc = roxmltree::Document::parse(
    "<e xmlns:n='http://www.w3.org'/>"
).unwrap();

assert_eq!(doc.root_element().namespaces().len(), 1);

Returns node’s text.

  • for an element will return a first text child
  • for a comment will return a self text
  • for a text node will return a self text
Examples
let doc = roxmltree::Document::parse("\
<p>
    text
</p>
").unwrap();

assert_eq!(doc.root_element().text(),
           Some("\n    text\n"));
assert_eq!(doc.root_element().first_child().unwrap().text(),
           Some("\n    text\n"));
let doc = roxmltree::Document::parse("<!-- comment --><e/>").unwrap();

assert_eq!(doc.root().first_child().unwrap().text(), Some(" comment "));

Returns element’s tail text.

Examples
let doc = roxmltree::Document::parse("\
<root>
    text1
    <p/>
    text2
</root>
").unwrap();

let p = doc.descendants().find(|n| n.has_tag_name("p")).unwrap();
assert_eq!(p.tail(), Some("\n    text2\n"));

Returns node as Processing Instruction.

Returns the parent of this node.

Returns the parent element of this node.

Returns the previous sibling of this node.

Returns the next sibling of this node.

Returns the first child of this node.

Returns the first element child of this node.

Returns the last child of this node.

Returns the last element child of this node.

Returns true if this node has siblings.

Returns true if this node has children.

Returns an iterator over ancestor nodes.

Returns an iterator over previous sibling nodes.

Returns an iterator over next sibling nodes.

Returns an iterator over first children nodes.

Returns an iterator over last children nodes.

Returns an iterator over children nodes.

Returns an iterator which traverses the subtree starting at this node.

Returns an iterator over this node and its descendants.

Returns node’s position in bytes in the original document.

Calculates node’s position in the original document.

Note: this operation is expensive.

Examples
let doc = roxmltree::Document::parse("\
<!-- comment -->
<e/>"
).unwrap();

assert_eq!(doc.root_element().node_pos(), roxmltree::TextPos::new(2, 1));

Trait Implementations

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.