pub struct NamespaceResolver { /* private fields */ }Expand description
Storage for the currently defined namespace bindings, used to resolve prefixes into namespaces. Holds all the internal logic to push and pop namespaces with their levels.
Implementations§
Source§impl NamespaceResolver
impl NamespaceResolver
Sourcepub fn add(
&mut self,
prefix: PrefixDeclaration<'_>,
namespace: Namespace<'_>,
) -> Result<(), NamespaceError>
pub fn add( &mut self, prefix: PrefixDeclaration<'_>, namespace: Namespace<'_>, ) -> Result<(), NamespaceError>
Adds a new binding of prefix to namespace at the current nesting level.
The binding is removed when the level is popped out. May fail
if you try to (re-)declare the reserved prefixes xml and xmlns.
let mut resolver = NamespaceResolver::default();
assert_eq!(resolver.resolve_element(QName(b"name")).0, ResolveResult::Unbound);
assert_eq!(
resolver.resolve_element(QName(b"ns:name")).0,
ResolveResult::Unknown(b"ns".to_vec()),
);
resolver.add(PrefixDeclaration::Default, Namespace(b"example.com")).expect("doc example");
resolver.add(PrefixDeclaration::Named(b"ns"), Namespace(b"my:namespace")).expect("doc example");
assert_eq!(
resolver.resolve_element(QName(b"name")).0,
ResolveResult::Bound(Namespace(b"example.com")),
);
assert_eq!(
resolver.resolve_element(QName(b"ns:name")).0,
ResolveResult::Bound(Namespace(b"my:namespace")),
);Sourcepub fn push(&mut self, start: &BytesStart<'_>) -> Result<(), NamespaceError>
pub fn push(&mut self, start: &BytesStart<'_>) -> Result<(), NamespaceError>
Begins a new scope and adds to it all namespace bindings found in the specified start element.
The namespace name a declaration binds is its attribute’s normalized
value: Namespaces in XML §3 defines it as “the attribute value in the
namespace declaration”, which XML §3.3.3 has already normalized by the
time the value reaches an application. So a declaration written with a
literal tab in it binds a name with a space where the tab was, while
one written urn:a	b binds a name that still holds the tab — a
character reference stands for the character it names, and is not white
space the algorithm folds. Either way the binding is the very string
Attribute::normalized_value
reports for the same attribute: one document cannot have two answers.
Sourcepub const fn max_declarations_per_element(&self) -> usize
pub const fn max_declarations_per_element(&self) -> usize
Returns the maximum number of xmlns / xmlns:* declarations that
push will accept on a single start tag.
Sourcepub fn set_max_declarations_per_element(&mut self, limit: usize) -> &mut Self
pub fn set_max_declarations_per_element(&mut self, limit: usize) -> &mut Self
Sets the maximum number of xmlns / xmlns:* declarations that
push will accept on a single start tag. Pass usize::MAX
to disable the limit.
Sourcepub fn set_level(&mut self, level: usize)
pub fn set_level(&mut self, level: usize)
Sets the number of push calls not yet followed by a
pop. Setting a value lower than the current level
behaves as if pop were called until the level matches.
let mut resolver = NamespaceResolver::default();
assert_eq!(resolver.level(), 0);
resolver.push(&BytesStart::new("tag")).expect("doc example");
assert_eq!(resolver.level(), 1);
resolver.set_level(10);
assert_eq!(resolver.level(), 10);
resolver.pop();
assert_eq!(resolver.level(), 9);Sourcepub fn resolve<'n>(
&self,
name: QName<'n>,
use_default: bool,
) -> (ResolveResult<'_>, LocalName<'n>)
pub fn resolve<'n>( &self, name: QName<'n>, use_default: bool, ) -> (ResolveResult<'_>, LocalName<'n>)
Resolves a potentially qualified element or attribute name into (namespace name, local name).
§Parameters
name: the possibly qualified name to resolve;use_default: whether to translate an absent prefix to the current default namespace. Should betruefor element names,falsefor attribute names.
Sourcepub fn resolve_element<'n>(
&self,
name: QName<'n>,
) -> (ResolveResult<'_>, LocalName<'n>)
pub fn resolve_element<'n>( &self, name: QName<'n>, ) -> (ResolveResult<'_>, LocalName<'n>)
Convenience for resolve(name, true), expressing that an element name is
being resolved.
Sourcepub fn resolve_attribute<'n>(
&self,
name: QName<'n>,
) -> (ResolveResult<'_>, LocalName<'n>)
pub fn resolve_attribute<'n>( &self, name: QName<'n>, ) -> (ResolveResult<'_>, LocalName<'n>)
Convenience for resolve(name, false), expressing that an attribute name
is being resolved.
Sourcepub fn resolve_event<'i>(
&self,
event: Event<'i>,
) -> (ResolveResult<'_>, Event<'i>)
pub fn resolve_event<'i>( &self, event: Event<'i>, ) -> (ResolveResult<'_>, Event<'i>)
Finds a namespace name for a given event, if applicable.
A namespace is resolved only for Start,
Empty and End events; for all other
events ResolveResult::Unbound is returned.
Sourcepub fn resolve_prefix(
&self,
prefix: Option<Prefix<'_>>,
use_default: bool,
) -> ResolveResult<'_>
pub fn resolve_prefix( &self, prefix: Option<Prefix<'_>>, use_default: bool, ) -> ResolveResult<'_>
Resolves the given optional prefix (usually obtained from QName::prefix)
into a corresponding namespace.
Sourcepub const fn bindings(&self) -> NamespaceBindingsIter<'_> ⓘ
pub const fn bindings(&self) -> NamespaceBindingsIter<'_> ⓘ
Returns all the bindings currently in effect except the default xml and
xmlns bindings.
let mut resolver = NamespaceResolver::default();
assert_eq!(resolver.bindings().collect::<Vec<_>>(), vec![]);
resolver.push(&BytesStart::from_content("a xmlns=\"a1\" xmlns:a=\"a2\"", 1)).expect("doc example");
assert_eq!(resolver.bindings().collect::<Vec<_>>(), vec![
(PrefixDeclaration::Default, Namespace(b"a1")),
(PrefixDeclaration::Named(b"a"), Namespace(b"a2")),
]);Sourcepub const fn bindings_of(
&self,
level: usize,
) -> NamespaceBindingsOfLevelIter<'_> ⓘ
pub const fn bindings_of( &self, level: usize, ) -> NamespaceBindingsOfLevelIter<'_> ⓘ
Returns all the bindings on the specified level, including the default
xml and xmlns bindings (which live at level 0).