diff --git a/src/element.rs b/src/element.rs index 197d9ed0..58acc309 100644 --- a/src/element.rs +++ b/src/element.rs @@ -211,7 +211,15 @@ impl Element { match e { ReaderEvent::StartElement { name, attributes, namespace } => { let attributes = attributes.into_iter() - .map(|o| Attribute::new(o.name.local_name, o.value)) + .map(|o| { + Attribute::new( + match o.name.prefix { + Some(prefix) => format!("{}:{}", prefix, o.name.local_name), + None => o.name.local_name + }, + o.value + ) + }) .collect(); let ns = if let Some(ref prefix) = name.prefix { namespace.get(prefix) @@ -219,6 +227,7 @@ impl Element { else { namespace.get(NS_NO_PREFIX) }.map(|s| s.to_owned()); + let mut root = Element::new(name.local_name, ns, attributes, Vec::new()); root.from_reader_inner(reader)?; return Ok(root); @@ -237,7 +246,15 @@ impl Element { match e { ReaderEvent::StartElement { name, attributes, namespace } => { let attributes = attributes.into_iter() - .map(|o| Attribute::new(o.name.local_name, o.value)) + .map(|o| { + Attribute::new( + match o.name.prefix { + Some(prefix) => format!("{}:{}", prefix, o.name.local_name), + None => o.name.local_name + }, + o.value + ) + }) .collect(); let ns = if let Some(ref prefix) = name.prefix { namespace.get(prefix) diff --git a/src/tests.rs b/src/tests.rs index 44fef37b..64a32f92 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -7,11 +7,12 @@ use xml::writer::EventWriter; use element::Element; -const TEST_STRING: &'static str = r#"meownya"#; +const TEST_STRING: &'static str = r#"meownya"#; fn build_test_tree() -> Element { let mut root = Element::builder("root") .ns("root_ns") + .attr("xml:lang", "en") .attr("a", "b") .build(); root.append_text_node("meow"); @@ -22,6 +23,7 @@ fn build_test_tree() -> Element { let other_child = Element::builder("child") .ns("child_ns") .attr("d", "e") + .attr("xml:lang", "fr") .build(); root.append_child(other_child); root.append_text_node("nya"); @@ -105,3 +107,11 @@ fn two_elements_with_same_arguments_different_order_are_equal() { let elem2: Element = "".parse().unwrap(); assert_ne!(elem1, elem2); } + +#[test] +fn namespace_attributes_works() { + let mut reader = EventReader::new(Cursor::new(TEST_STRING)); + let root = Element::from_reader(&mut reader).unwrap(); + assert_eq!("en", root.attr("xml:lang").unwrap()); + assert_eq!("fr", root.get_child("child", "child_ns").unwrap().attr("xml:lang").unwrap()); +}