[lxml-dev] getname() method on 'smart' attribute string values
I have a situation where I want to find any attributes in a document that contain a certain value, and then change that value and record the fact that the new value is the result of such a change. In order to track these changes, I am populating a dictionary keyed by the parent element, with each value being another dictionary keyed by attribute name with a value of the list of parts of the attribute that have been changed. So: get_attrs = etree.XPath('//@*[contains(concat(" ", ., " "), concat(" #", $old_id, " "))]') for attribute in get_attrs(element, old_id=some_string): element = attribute.getparent() And then I discover that in fact I have to use element.attrib.items() and search through for which attribute matched. It would be much easier if the attribute 'smart' string result from the XPath evaluation had a method which would specify *which* attribute of the parent it was the value of. Or am I missing a better, existing way of doing this? Note that while there is in fact a finite set of attribute names I need to check, it's a potentially expanding set, and I'd rather not have to touch the code when that expansion happens. Jamie
Hi, Jamie Norrish wrote:
I have a situation where I want to find any attributes in a document that contain a certain value, and then change that value and record the fact that the new value is the result of such a change. In order to track these changes, I am populating a dictionary keyed by the parent element, with each value being another dictionary keyed by attribute name with a value of the list of parts of the attribute that have been changed. So:
get_attrs = etree.XPath('//@*[contains(concat(" ", ., " "), concat(" #", $old_id, " "))]')
for attribute in get_attrs(element, old_id=some_string): element = attribute.getparent()
And then I discover that in fact I have to use element.attrib.items() and search through for which attribute matched. It would be much easier if the attribute 'smart' string result from the XPath evaluation had a method which would specify *which* attribute of the parent it was the value of.
I like that. It would be an attribute, though, maybe "attrname", to make it clear that the name is a) fixed at the time it's found and b) only available for attributes, as elements have their own way of providing the tag name (which is not fixed). Stefan
On Thu, 2009-08-20 at 10:37 +0200, Stefan Behnel wrote:
I like that. It would be an attribute, though, maybe "attrname", to make it clear that the name is a) fixed at the time it's found and b) only available for attributes, as elements have their own way of providing the tag name (which is not fixed).
Sounds good to me! Jamie
Jamie Norrish wrote:
On Thu, 2009-08-20 at 10:37 +0200, Stefan Behnel wrote:
I like that. It would be an attribute, though, maybe "attrname", to make it clear that the name is a) fixed at the time it's found and b) only available for attributes, as elements have their own way of providing the tag name (which is not fixed).
Sounds good to me!
You can give it a try on the trunk, if you like. https://codespeak.net/viewvc/?view=rev&revision=67010 http://codespeak.net/lxml/build.html Stefan
participants (2)
-
Jamie Norrish
-
Stefan Behnel