pub struct RawNode<'a>(pub Node<'a, 'a>);Expand description
Captures subtrees from the source
This type must borrow from the source during serialization and therefore requires the use of the from_doc or from_node entry points.
It will however recover only the source document or node lifetime and not the full input lifetime.
use roxmltree::Document;
use serde::Deserialize;
use serde_roxmltree::{from_doc, RawNode};
#[derive(Deserialize)]
struct Record<'a> {
#[serde(borrow)]
subtree: RawNode<'a>,
}
let document = Document::parse(r#"<document><subtree><field attribute="bar">foo</field></subtree></document>"#)?;
let record = from_doc::<Record>(&document)?;
assert!(record.subtree.has_tag_name("subtree"));Tuple Fields§
§0: Node<'a, 'a>Methods from Deref<Target = Node<'a, 'a>>§
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, 'input>
pub fn tag_name(&self) -> ExpandedName<'a, 'input>
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, 'm, N>(&self, name: N) -> boolwhere
N: Into<ExpandedName<'n, 'm>>,
pub fn has_tag_name<'n, 'm, N>(&self, name: N) -> boolwhere
N: Into<ExpandedName<'n, 'm>>,
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 lookup_prefix(&self, uri: &str) -> Option<&'input str>
pub fn lookup_prefix(&self, uri: &str) -> Option<&'input 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<&str>) -> Option<&'a str>
pub fn lookup_namespace_uri(&self, prefix: Option<&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, 'm, N>(&self, name: N) -> Option<&'a str>where
N: Into<ExpandedName<'n, 'm>>,
pub fn attribute<'n, 'm, N>(&self, name: N) -> Option<&'a str>where
N: Into<ExpandedName<'n, 'm>>,
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 attribute_node<'n, 'm, N>(
&self,
name: N,
) -> Option<Attribute<'a, 'input>>where
N: Into<ExpandedName<'n, 'm>>,
pub fn attribute_node<'n, 'm, N>(
&self,
name: N,
) -> Option<Attribute<'a, 'input>>where
N: Into<ExpandedName<'n, 'm>>,
Returns element’s attribute object.
The same as attribute(), but returns the Attribute itself instead of a value string.
Sourcepub fn has_attribute<'n, 'm, N>(&self, name: N) -> boolwhere
N: Into<ExpandedName<'n, 'm>>,
pub fn has_attribute<'n, 'm, N>(&self, name: N) -> boolwhere
N: Into<ExpandedName<'n, 'm>>,
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) -> Attributes<'a, 'input>
pub fn attributes(&self) -> Attributes<'a, 'input>
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 namespaces(&self) -> NamespaceIter<'a, 'input>
pub fn namespaces(&self) -> NamespaceIter<'a, 'input>
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 text_storage(&self) -> Option<&'a StringStorage<'input>>
pub fn text_storage(&self) -> Option<&'a StringStorage<'input>>
Returns node’s text storage.
Useful when you need a more low-level access to an allocated string.
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 tail_storage(&self) -> Option<&'a StringStorage<'input>>
pub fn tail_storage(&self) -> Option<&'a StringStorage<'input>>
Returns element’s tail text storage.
Useful when you need a more low-level access to an allocated string.
Sourcepub fn parent_element(&self) -> Option<Node<'a, 'input>>
pub fn parent_element(&self) -> Option<Node<'a, 'input>>
Returns the parent element of this node.
Sourcepub fn prev_sibling(&self) -> Option<Node<'a, 'input>>
pub fn prev_sibling(&self) -> Option<Node<'a, 'input>>
Returns the previous sibling of this node.
Sourcepub fn prev_sibling_element(&self) -> Option<Node<'a, 'input>>
pub fn prev_sibling_element(&self) -> Option<Node<'a, 'input>>
Returns the previous sibling element of this node.
Sourcepub fn next_sibling(&self) -> Option<Node<'a, 'input>>
pub fn next_sibling(&self) -> Option<Node<'a, 'input>>
Returns the next sibling of this node.
Sourcepub fn next_sibling_element(&self) -> Option<Node<'a, 'input>>
pub fn next_sibling_element(&self) -> Option<Node<'a, 'input>>
Returns the next sibling element of this node.
Sourcepub fn first_child(&self) -> Option<Node<'a, 'input>>
pub fn first_child(&self) -> Option<Node<'a, 'input>>
Returns the first child of this node.
Sourcepub fn first_element_child(&self) -> Option<Node<'a, 'input>>
pub fn first_element_child(&self) -> Option<Node<'a, 'input>>
Returns the first element child of this node.
Sourcepub fn last_child(&self) -> Option<Node<'a, 'input>>
pub fn last_child(&self) -> Option<Node<'a, 'input>>
Returns the last child of this node.
Sourcepub fn last_element_child(&self) -> Option<Node<'a, 'input>>
pub fn last_element_child(&self) -> Option<Node<'a, 'input>>
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 ancestors(&self) -> AxisIter<'a, 'input>
pub fn ancestors(&self) -> AxisIter<'a, 'input>
Returns an iterator over ancestor nodes starting at this node.
Sourcepub fn prev_siblings(&self) -> AxisIter<'a, 'input>
pub fn prev_siblings(&self) -> AxisIter<'a, 'input>
Returns an iterator over previous sibling nodes starting at this node.
Sourcepub fn next_siblings(&self) -> AxisIter<'a, 'input>
pub fn next_siblings(&self) -> AxisIter<'a, 'input>
Returns an iterator over next sibling nodes starting at this node.
Sourcepub fn first_children(&self) -> AxisIter<'a, 'input>
pub fn first_children(&self) -> AxisIter<'a, 'input>
Returns an iterator over first children nodes starting at this node.
Sourcepub fn last_children(&self) -> AxisIter<'a, 'input>
pub fn last_children(&self) -> AxisIter<'a, 'input>
Returns an iterator over last children nodes starting at this node.
Sourcepub fn descendants(&self) -> Descendants<'a, 'input>
pub fn descendants(&self) -> Descendants<'a, 'input>
Returns an iterator over this node and its descendants.