Skip to content

Loading…

set node tag name #4

Open
frank06 opened this Issue · 8 comments

3 participants

@frank06

Concretely, this is my usecase: I simply need to change this:

<body>
   (2400 children)
</body>

into this:

<div>
   (2400 children)
</div>

Tag names in the DOM are immutable, however, there is a W3C DOM API for renaming nodes renameNode():
http://www.w3schools.com/Dom/met_document_renamenode.asp
https://developer.mozilla.org/en/dom/document

that doesn't seem to be available in o3-xml: console.log(typeof node.ownerDocument.renameNode) yields undefined, whereas in Firefox document.renameNode outputs function.

Then I saw that Nokogiri (that also wraps libxml2) does allow setting a node name: http://nokogiri.org/Nokogiri/XML/Node.html#method-i-node_name%3D

FWIW, I did try to create a new element, and move all the children along via appendChild. This turned out to be a very expensive operation.

Would it be possible to enable this functionality, or what would the best workaround be? Thanks!

@krizsa

If I understand your problem correctly, you don't necessary need the renameNode method (with an uri parameter), but it would be enough for you to turn the read only properties on xml nodes, settable properties, like nokogiri does it. I assumed that the same functionality nokogiri has will solve your problem so I checked their implementation about how to set the name of a libxml2 node, and did the same. So now you can do myXmlNode.name = "someNewName";
Let me know please if it solved your problem. (I updated the binaries)

@frank06

I tried out the new binaries, thanks. While the idea is exactly what I'm looking for, and it does properly set name, it doesn't alter some other related variables such as tagName. And therefore (?) doesn't seem to get updated in the tree.

body = elem.selectSingleNode('//xhtml:body');
body.name = 'div';
console.log(body.name); // outputs 'div'
console.log(body.tagName); // outputs 'body'
console.log(body.xml); // this is the interesting part, it *doesn't* output the updated name, the element still starts with <body> instead of <div>
@krizsa

Hmmm... that's not good. Actually it was the only idea I had for this problem to be honest, I didn't see any other libxml2 function that supposed to change the name. Well I'm not sure I can help you with this but if I find anything I will let you know.

@frank06

Ok, please let me know. I'm resorting to a hack with a regular expression now (which by the way is orders of magnitude faster than appending children to a new element).

@krizsa

@polotek: Thank you for your help, that was the exact same function I tried, but it did not solve his problem...

@frank06

@krizsa: Strange... if it works in libxmljs it should be possible in o3-xml, right?

@polotek

I will admit I don't fully understand what's going on in the o3-xml code. There's lots of abstraction there. I'm assuming you have to do this to support the browser plugins and other environments that o3 runs in. Plus my C/C++ skills are still pretty intermediate. I would like to contribute by helping out with this and some other things, but I would need a lot more knowledge about how things work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.