2

I'm trying to remove double quotes from text nodes. The following code works:

var cars = <cars>
             <buick>
                <color>
                   "Blue"
                </color>
             </buick>
             <chevy>
                <color>
                   "Red"
                </color>
             </chevy>
           </cars>;

for each (elem in cars)
   for each (item in elem.*)
      elem[item.localName()] = item.text().toString().replace(/"/g,'');

However, I don't feel comfortable about the elem[item.localName()]= construction. Indeed, I already have a pointed to text item and it would be much more logical to use something like:

item = item.text().toString().replace(/"/g,'');

Unfortunately, this code doesn't seem to do what it's supposed to do. Any ideas why? What is the right way to do it?

mtelis
  • 654
  • 1
  • 6
  • 10

2 Answers2

1

Where are you running this code? Because it is working fine for me in Firefox.

And so is this (try it and let me know if it worked for you):

for each (txt in cars..*.text()){
    txt.parent().setChildren( txt.toString().replace(/"/g,'') );
}
Zecc
  • 4,220
  • 19
  • 17
  • I'm running the code in YQL (Open Data table element). Your code works but actually I was wondering if there is a way to change text in a node without having to refer to the parent (it's what I'm doing now, elem[item.localName()] ) – mtelis Nov 26 '10 at 06:08
  • @mtelis Hmm.. `txt.nodeValue = ...` should work, but it isn't. In any case, why exactly do you want to avoid referring to the parent element? – Zecc Nov 26 '10 at 10:50
  • It should be noted that there may actually be several different text nodes under the same element, and you should probably call `normalize()` on all of them just to be on the safe side. Case in point: I just added a comment in the middle of the text and my code broke, so it's even less robust than your original code. – Zecc Nov 26 '10 at 10:55
  • I'm trying to avoid obfuscating code, want to keep my code clean. I need to change text in current item; I have variable pointing to the item and it's ridiculous to ask the parent do the changes. The more I'm trying the less I happy about all this E4X business :-( – mtelis Nov 26 '10 at 15:15
1

Try this, please:

for each (var color in cars.*.*)
  color.* = color.toString().replace(/"/g,'');
acebanenco
  • 11
  • 1