pub struct AnyElement<'xml> {
pub ns: Cow<'xml, str>,
pub name: Cow<'xml, str>,
pub attributes: Vec<AnyAttribute<'xml>>,
pub text: Option<Cow<'xml, str>>,
pub children: Vec<AnyElement<'xml>>,
}Expand description
A dynamically captured XML element.
The AnyElement type captures an arbitrary XML element tree at runtime,
preserving its namespace, name, attributes, text
content, and nested children. This is useful when the XML schema allows
arbitrary content (e.g. <xs:any namespace="##any" processContents="skip" />)
or when the element structure is not known at compile time.
String values are borrowed from the input XML where possible via Cow.
§Example
As root:
use instant_xml::{from_str, AnyElement};
let xml = r#"<item xmlns="http://example.com" key="val">hello</item>"#;
let elem: AnyElement<'_> = from_str(xml).unwrap();
assert_eq!(elem.name, "item");
assert_eq!(elem.ns, "http://example.com");
assert_eq!(elem.text.as_deref(), Some("hello"));
assert_eq!(elem.attributes.len(), 1);As child (borrowing from the input):
use instant_xml::{from_str, FromXml, AnyElement};
#[derive(Debug, FromXml, PartialEq)]
#[xml(ns("http://example.com"))]
struct Wrapper<'a> {
#[xml(borrow)]
inner: AnyElement<'a>,
}
let xml = r#"<Wrapper xmlns="http://example.com"><item>text</item></Wrapper>"#;
let parsed: Wrapper<'_> = from_str(xml).unwrap();
assert_eq!(parsed.inner.name, "item");
assert_eq!(parsed.inner.ns, "http://example.com");
assert_eq!(parsed.inner.text.as_deref(), Some("text"));Note: When using AnyElement as a field in a derived struct, add
#[xml(borrow)] so the derive macro generates the correct lifetime bounds.
Use into_owned() to convert to AnyElement<'static>
when you need to decouple from the input lifetime.
§Matching behavior
AnyElement matches any XML element regardless of namespace or name. When
used as a field in a derived struct, it will capture whichever element appears
in that position. For capturing multiple arbitrary children, use
Vec<AnyElement>.
Fields§
§ns: Cow<'xml, str>XML namespace URI of this element.
name: Cow<'xml, str>Local element name.
attributes: Vec<AnyAttribute<'xml>>Attributes on this element.
text: Option<Cow<'xml, str>>Text content of this element, if any.
children: Vec<AnyElement<'xml>>Nested child elements.
Implementations§
Source§impl<'a> AnyElement<'a>
impl<'a> AnyElement<'a>
Sourcepub fn into_owned(self) -> AnyElement<'static>
pub fn into_owned(self) -> AnyElement<'static>
Converts this element into an owned version with 'static lifetime.
This recursively converts all borrowed strings into owned copies, decoupling the result from the original XML input.
Trait Implementations§
Source§impl<'xml> Clone for AnyElement<'xml>
impl<'xml> Clone for AnyElement<'xml>
Source§fn clone(&self) -> AnyElement<'xml>
fn clone(&self) -> AnyElement<'xml>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<'xml> Debug for AnyElement<'xml>
impl<'xml> Debug for AnyElement<'xml>
Source§impl<'xml, 'a> FromXml<'xml> for AnyElement<'a>where
'xml: 'a,
impl<'xml, 'a> FromXml<'xml> for AnyElement<'a>where
'xml: 'a,
Source§fn matches(_id: Id<'_>, _field: Option<Id<'_>>) -> bool
fn matches(_id: Id<'_>, _field: Option<Id<'_>>) -> bool
Matches any element regardless of namespace or name.