Discriminate between attributes in different namespaces #38
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This commit is an alternative approach to fixing #13 (Attribute namespaces are not preserved), inspired by the discussion on #33. It changes the type of the
attributes
map on theElement
struct from a String -> String map to an AttributeName -> String map. AttributeName is a re-export of the xml::name::OwnedName type in xmltree-rs, which is a struct containing the attribute's local name, namespace and prefix.It is possible to search the
attributes
map using a fully initialised AttributeName structure, but for convenience get_attribute(), get_mut_attribute() and take_attribute() methods, modelled on the *_element() equivalent are provided, which take an AttributePredicate. Ready-made implementations taking either a local name or local name + namespace are provided.This is a semver-breaking change, hence version is incremented to 0.11.0, I'm not sure if it is necessary to provide some kind of backward-compatibility compile time switch?
I was in two minds as to how closely to follow the pattern of the existing
take_element()
,get_element()
etc. APIs. While writing the tests I realised that those APIs do not allow you to passNone
for the namespace, you can either pass just the local name, and ignore namespace, or local name + namespace. This means it's not possible to explicitly search for elements with no namespace, which seems like a defect to me.My initial implementation, which I still have on a branch, had implementations of the
AttributePredicate
trait forString
,&str
andCow<&str>
just like the the Element equivalents, but having these and an implementation for(N, Option<NS>)
to allow the namespace to be specified means that if you want to passNone
the type is ambiguous so you have to specify which string-like type you mean, e.g.<Option<String>>::None
. This seemed quite awkward so this version only supports&str
. I can of course change this if required.