Namespace

Struct Namespace 

Source
pub struct Namespace<'input> { /* private fields */ }
Expand description

A namespace.

Contains URI and prefix pair.

Implementations§

Source§

impl<'input> Namespace<'input>

Source

pub fn name(&self) -> Option<&'input str>

Returns namespace name/prefix.

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

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

assert_eq!(doc.root_element().namespaces().nth(0).unwrap().name(), None);
Examples found in repository?
examples/stats.rs (line 39)
3fn main() {
4    let args: Vec<_> = std::env::args().collect();
5
6    if args.len() != 2 {
7        println!("Usage:\n\tcargo run --example stats -- input.xml");
8        std::process::exit(1);
9    }
10
11    let text = std::fs::read_to_string(&args[1]).unwrap();
12    let opt = roxmltree::ParsingOptions {
13        allow_dtd: true,
14        ..roxmltree::ParsingOptions::default()
15    };
16    let doc = match roxmltree::Document::parse_with_options(&text, opt) {
17        Ok(v) => v,
18        Err(e) => {
19            println!("Error: {}.", e);
20            std::process::exit(1);
21        }
22    };
23
24    println!(
25        "Elements count: {}",
26        doc.root().descendants().filter(|n| n.is_element()).count()
27    );
28
29    let attrs_count: usize = doc.root().descendants().map(|n| n.attributes().len()).sum();
30    println!("Attributes count: {}", attrs_count);
31
32    let ns_count: usize = doc.root().descendants().map(|n| n.namespaces().len()).sum();
33    println!("Namespaces count: {}", ns_count);
34
35    let mut uris = HashSet::new();
36    for node in doc.root().descendants() {
37        for ns in node.namespaces() {
38            uris.insert((
39                ns.name().unwrap_or("\"\"").to_string(),
40                ns.uri().to_string(),
41            ));
42        }
43    }
44    println!("Unique namespaces count: {}", uris.len());
45    if !uris.is_empty() {
46        println!("Unique namespaces:");
47        for (key, value) in uris {
48            println!("  {:?}: {}", key, value);
49        }
50    }
51
52    println!(
53        "Comments count: {}",
54        doc.root().descendants().filter(|n| n.is_comment()).count()
55    );
56
57    println!("Comments:");
58    for node in doc.root().descendants().filter(|n| n.is_comment()) {
59        println!("{:?}", node.text().unwrap());
60    }
61}
Source

pub fn uri(&self) -> &str

Returns namespace URI.

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

assert_eq!(doc.root_element().namespaces().nth(0).unwrap().uri(), "http://www.w3.org");
Examples found in repository?
examples/stats.rs (line 40)
3fn main() {
4    let args: Vec<_> = std::env::args().collect();
5
6    if args.len() != 2 {
7        println!("Usage:\n\tcargo run --example stats -- input.xml");
8        std::process::exit(1);
9    }
10
11    let text = std::fs::read_to_string(&args[1]).unwrap();
12    let opt = roxmltree::ParsingOptions {
13        allow_dtd: true,
14        ..roxmltree::ParsingOptions::default()
15    };
16    let doc = match roxmltree::Document::parse_with_options(&text, opt) {
17        Ok(v) => v,
18        Err(e) => {
19            println!("Error: {}.", e);
20            std::process::exit(1);
21        }
22    };
23
24    println!(
25        "Elements count: {}",
26        doc.root().descendants().filter(|n| n.is_element()).count()
27    );
28
29    let attrs_count: usize = doc.root().descendants().map(|n| n.attributes().len()).sum();
30    println!("Attributes count: {}", attrs_count);
31
32    let ns_count: usize = doc.root().descendants().map(|n| n.namespaces().len()).sum();
33    println!("Namespaces count: {}", ns_count);
34
35    let mut uris = HashSet::new();
36    for node in doc.root().descendants() {
37        for ns in node.namespaces() {
38            uris.insert((
39                ns.name().unwrap_or("\"\"").to_string(),
40                ns.uri().to_string(),
41            ));
42        }
43    }
44    println!("Unique namespaces count: {}", uris.len());
45    if !uris.is_empty() {
46        println!("Unique namespaces:");
47        for (key, value) in uris {
48            println!("  {:?}: {}", key, value);
49        }
50    }
51
52    println!(
53        "Comments count: {}",
54        doc.root().descendants().filter(|n| n.is_comment()).count()
55    );
56
57    println!("Comments:");
58    for node in doc.root().descendants().filter(|n| n.is_comment()) {
59        println!("{:?}", node.text().unwrap());
60    }
61}

Trait Implementations§

Source§

impl<'input> Clone for Namespace<'input>

Source§

fn clone(&self) -> Namespace<'input>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'input> Debug for Namespace<'input>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<'input> PartialEq for Namespace<'input>

Source§

fn eq(&self, other: &Namespace<'input>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'input> Eq for Namespace<'input>

Source§

impl<'input> StructuralPartialEq for Namespace<'input>

Auto Trait Implementations§

§

impl<'input> Freeze for Namespace<'input>

§

impl<'input> RefUnwindSafe for Namespace<'input>

§

impl<'input> Send for Namespace<'input>

§

impl<'input> Sync for Namespace<'input>

§

impl<'input> Unpin for Namespace<'input>

§

impl<'input> UnwindSafe for Namespace<'input>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.