Bugs item #14231, was opened at 2007-09-25 19:17 You can respond by visiting: http://rubyforge.org/tracker/?func=detail&atid=1971&aid=14231&group_id=494
Category: None Group: None >Status: Closed >Resolution: Accepted Priority: 3 Submitted By: Dave Howell (snarke) >Assigned to: Charlie Savage (cfis) Summary: Cannot make assignment to attributes in a namespace. Initial Comment: I'm working with the XML files created by Apple's "Pages" page layout program, which makes extensive use of libxml-ruby's biggest weakness, namespaces. Here's a sample bit of code: irb(main):031:0* node => <sf:p sf:style="paragraph-style-6">Sample Text</sf:p> I can see the attribute without difficulty: irb(main):032:0> node["style"] => "paragraph-style-6" irb(main):033:0> node.properties => style = paragraph-style-6 But it's impossible to change it: irb(main):034:0> node["style"]="paragraph-style-12" => "paragraph-style-12" irb(main):035:0> node => <sf:p sf:style="paragraph-style-6" style="paragraph-style-12">Sample Text</sf:p> Or to delete it. irb(main):036:0> node["style"]=nil => nil irb(main):037:0> node => <sf:p sf:style="paragraph-style-6">Sample Text</sf:p> irb(main):038:0> node["style"]=nil => nil irb(main):039:0> node => <sf:p sf:style="paragraph-style-6">Sample Text</sf:p> I do a fair amount of work in Ruby, but I don't know (and, frankly, have no intention to learn) C, or C++, or whichever dialect the libxml bindings are written in. But I did manage to create a partial solution. It seems quite bizarre that the .properties method returns the first attribute instead of an array of attributes. It specifically returns the first attribute as an XML::Attr object. irb(main):040:0> node.properties.name => "style" irb(main):041:0> node.properties.value => "paragraph-style-6" irb(main):042:0> node => <sf:p sf:style="paragraph-style-6">Sample Text</sf:p> Note the difference between listing the node itself, and accessing the first attribute with .properties. The namespace qualifier gets dropped. Also, the Attr object has the .name and .value methods, but is missing the obvious .value= method. So I added that, and made .value= smart enough to work correctly with an attribute's namespace. irb(main):047:0> node.properties.value="paragraph-style 12" => "paragraph-style 12" irb(main):048:0> node => <sf:p sf:style="paragraph-style 12">Sample Text</sf:p> This only works on existing attributes with namespaces; you can't add new attributes with a namespace. I looked at trying to make the node["attribute"] form work this way as well, but it took me far too many hours to get the .properties.value= working, and the [""] form uses completely different code. I am attaching my modified ruby_xml_attr.c file. Somebody who actually knows this code, or at least C, ought to make sure there aren't problems in the code before anybody should consider letting it near the main code tree, if version 5.x hasn't already made it irrelevant. ---------------------------------------------------------------------- >Comment By: Charlie Savage (cfis) Date: 2008-07-09 03:01 Message: Thanks for the patch. A new attributes class has been introduced that is similar to what REXML has. Also, updating an attribute's value now also works for namespaced attributes. Changes will be in an upcoming release. ---------------------------------------------------------------------- You can respond by visiting: http://rubyforge.org/tracker/?func=detail&atid=1971&aid=14231&group_id=494 _______________________________________________ libxml-devel mailing list libxml-devel@rubyforge.org http://rubyforge.org/mailman/listinfo/libxml-devel