pub struct Node<'a, 'd: 'a> { /* private fields */ }
Expand description
A node.
Implementations
sourceimpl<'a, 'd: 'a> Node<'a, 'd>
impl<'a, 'd: 'a> Node<'a, 'd>
sourcepub fn is_element(&self) -> bool
pub fn is_element(&self) -> bool
Checks that node is an element node.
sourcepub fn is_comment(&self) -> bool
pub fn is_comment(&self) -> bool
Checks that node is a comment node.
sourcepub fn tag_name(&self) -> ExpandedName<'a>
pub fn tag_name(&self) -> ExpandedName<'a>
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");
sourcepub fn has_tag_name<N>(&self, name: N) -> boolwhere
N: Into<ExpandedName<'a>>,
pub fn has_tag_name<N>(&self, name: N) -> boolwhere
N: Into<ExpandedName<'a>>,
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")));
sourcepub fn default_namespace(&self) -> Option<&'a str>
pub fn default_namespace(&self) -> Option<&'a str>
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);
sourcepub fn resolve_tag_name_prefix(&self) -> Option<&'a str>
pub fn resolve_tag_name_prefix(&self) -> Option<&'a str>
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);
sourcepub fn lookup_prefix(&self, uri: &str) -> Option<&'a str>
pub fn lookup_prefix(&self, uri: &str) -> Option<&'a str>
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"));
sourcepub fn lookup_namespace_uri(&self, prefix: Option<&'a str>) -> Option<&'a str>
pub fn lookup_namespace_uri(&self, prefix: Option<&'a str>) -> Option<&'a str>
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"));
sourcepub fn attribute<N>(&self, name: N) -> Option<&'a str>where
N: Into<ExpandedName<'a>>,
pub fn attribute<N>(&self, name: N) -> Option<&'a str>where
N: Into<ExpandedName<'a>>,
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"));
sourcepub fn has_attribute<N>(&self, name: N) -> boolwhere
N: Into<ExpandedName<'a>>,
pub fn has_attribute<N>(&self, name: N) -> boolwhere
N: Into<ExpandedName<'a>>,
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")));
sourcepub fn attributes(&self) -> &'a [Attribute<'d>]
pub fn attributes(&self) -> &'a [Attribute<'d>]
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);
sourcepub fn attribute_pos<N>(&self, name: N) -> Option<TextPos>where
N: Into<ExpandedName<'a>>,
pub fn attribute_pos<N>(&self, name: N) -> Option<TextPos>where
N: Into<ExpandedName<'a>>,
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)));
sourcepub fn attribute_value_pos<N>(&self, name: N) -> Option<TextPos>where
N: Into<ExpandedName<'a>>,
pub fn attribute_value_pos<N>(&self, name: N) -> Option<TextPos>where
N: Into<ExpandedName<'a>>,
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)));
sourcepub fn namespaces(&self) -> &'a [Namespace<'d>]
pub fn namespaces(&self) -> &'a [Namespace<'d>]
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);
sourcepub fn text(&self) -> Option<&'a str>
pub fn text(&self) -> Option<&'a str>
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 "));
sourcepub fn tail(&self) -> Option<&'a str>
pub fn tail(&self) -> Option<&'a str>
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"));
sourcepub fn parent_element(&self) -> Option<Self>
pub fn parent_element(&self) -> Option<Self>
Returns the parent element of this node.
sourcepub fn prev_sibling(&self) -> Option<Self>
pub fn prev_sibling(&self) -> Option<Self>
Returns the previous sibling of this node.
sourcepub fn next_sibling(&self) -> Option<Self>
pub fn next_sibling(&self) -> Option<Self>
Returns the next sibling of this node.
sourcepub fn first_child(&self) -> Option<Self>
pub fn first_child(&self) -> Option<Self>
Returns the first child of this node.
sourcepub fn first_element_child(&self) -> Option<Self>
pub fn first_element_child(&self) -> Option<Self>
Returns the first element child of this node.
sourcepub fn last_child(&self) -> Option<Self>
pub fn last_child(&self) -> Option<Self>
Returns the last child of this node.
sourcepub fn last_element_child(&self) -> Option<Self>
pub fn last_element_child(&self) -> Option<Self>
Returns the last element child of this node.
sourcepub fn has_siblings(&self) -> bool
pub fn has_siblings(&self) -> bool
Returns true if this node has siblings.
sourcepub fn has_children(&self) -> bool
pub fn has_children(&self) -> bool
Returns true if this node has children.
sourcepub fn prev_siblings(&self) -> PrevSiblings<'a, 'd> ⓘ
pub fn prev_siblings(&self) -> PrevSiblings<'a, 'd> ⓘ
Returns an iterator over previous sibling nodes.
sourcepub fn next_siblings(&self) -> NextSiblings<'a, 'd> ⓘ
pub fn next_siblings(&self) -> NextSiblings<'a, 'd> ⓘ
Returns an iterator over next sibling nodes.
sourcepub fn first_children(&self) -> FirstChildren<'a, 'd> ⓘ
pub fn first_children(&self) -> FirstChildren<'a, 'd> ⓘ
Returns an iterator over first children nodes.
sourcepub fn last_children(&self) -> LastChildren<'a, 'd> ⓘ
pub fn last_children(&self) -> LastChildren<'a, 'd> ⓘ
Returns an iterator over last children nodes.
sourcepub fn traverse(&self) -> Traverse<'a, 'd> ⓘ
pub fn traverse(&self) -> Traverse<'a, 'd> ⓘ
Returns an iterator which traverses the subtree starting at this node.
sourcepub fn descendants(&self) -> Descendants<'a, 'd> ⓘ
pub fn descendants(&self) -> Descendants<'a, 'd> ⓘ
Returns an iterator over this node and its descendants.