Skip to main content
Known Participant
April 27, 2016
Question

Deleting an XML object

  • April 27, 2016
  • 2 replies
  • 2406 views

Hi all,

I've been having a bit of trouble deleting XML objects with ExtendScript's native delete() function. Basically I have an XML object being passed into a function like this:

makeChanges(XMLObj, XMLObj.child1);

makeChanges(XMLObj, XMLObj.child2);

makeChanges(XMLObj, XMLObj.child3);

function makeChanges(object, element)

{

     if (condition)

          delete element;

}

However, the child element is not deleted, I suspect because the "element" variable reference is still active. If the code is changed to explicitly delete the object like so:

     if (condition)

          delete object.child1

then everything works just fine, but I make quite a few calls to this function and would like the code to be re-usable if possible. I know JavaScript has a removeChild() function for XML, but does anyone know if there's an ExtendScript workaround? I've looked through the documentation and haven't seen much, so any input is greatly appreciated.

This topic has been closed for replies.

2 replies

Klaus Göbel
Legend
May 8, 2020

Four years have passed and now I remembered this old post.
I'd like to close it now.

 

To delete an element

delete element 

 doesn't work as described.

 

Instead you have to do this:

 

delete element.parent().children()[element.childIndex()];

 

 

I know, that this is to late for you, but it may help other users looking for a solution.

Known Participant
September 11, 2020

Hello Klaus,

 

I am trying to do precisely this, and yet, cannot see why it isn't working. Perhaps it will be apparent to you.

 

This is my code:


function remove_reasonForUpdate(elem){
//remove all <reasonForUpdate>
var rfus = getElements (elem, /^(reasonForUpdate)$/, []);
var rfu_length = rfus.length;

for (var y = 0 ; y < rfu_length ;y++) {
var rfu = rfus[y];
delete rfu.parent().children()[rfu.childIndex()];
doc.Redisplay();
}
return;
}

 

Can you see what I am doing wrong?

 

Thanks in advance,
Tracey

frameexpert
Community Expert
Community Expert
September 11, 2020

Hi Tracey,

Are you trying to delete elements in a FrameMaker document or in an ExtendScript XML object? This code that Klaus posted is for deleting elements from an XML object.

-Rick

www.frameexpert.com
Klaus Göbel
Legend
April 27, 2016

Hi carlm,

"delete" is the right way.

But maybe the access to the structure of xml is wrong.

Just try my little snippet:

It creates an XML-structure, adds two items and deletes an item-

var gXML_Root = new XML ( "<FMProperties> <Item> </Item><Item></Item></FMProperties>");

$.writeln(gXML_Root);

// add some items

gXML_Root.FMProperties.Item[0]= "Item 1";

gXML_Root.FMProperties.Item[1]= "Item 2";

  $.writeln("-----------------");

  $.writeln(gXML_Root);

// delete an item

delete gXML_Root.FMProperties.Item[1];

   $.writeln(gXML_Root);

Known Participant
April 27, 2016

Hi Klaus, thanks for replying. I'm afraid that your code is simply doing the same thing as the second example illustrated above (albeit using an index-based method, which I'm also not able to do as I can't guarantee the proper location of the child element).

if (condition)

     delete object.child1;     // same as delete object.child[indexOfChild];

What I'm seeking to do is delete the XML child object after passing it into a function as it's own entity (the parent object is also passed in, if that helps).

In your example, this would look something like this:

var gXML_Root = new XML ("<FMProperties> <Item> </Item><Item></Item></FMProperties>");

gXML_Root.FM_Properties.Item[0]= "Item 1";

gXML_Root.FM_Properties.Item[1]= "Item 2";

deleteChild(gXML_Root.FM_Properties, gXML_Root.FM_Properties.Item[1]);

function deleteChild(parent, child)

{

     delete child;          // gXML_root.FM_Properties.Item[1] is not deleted

}

$.writeln(gXML_Root);

Any ideas?

Klaus Göbel
Legend
April 27, 2016

Hi carlm,

another try:

I hope, it comes closer to your needs.

var gXML_Root = new XML ( "<FMProperties> <Item> </Item><Item></Item></FMProperties>");

$.writeln(gXML_Root);

// add some items

gXML_Root.Item[0] = "Value 1";

gXML_Root.Item[1]= "Item 2";

  $.writeln("-----------------");

  $.writeln(gXML_Root);

// delete an item

var DeleteIndex = 1;

XML_Remove(gXML_Root,DeleteIndex)

   $.writeln(gXML_Root);

  

function XML_Remove(fObject,fIndex)

{

var elem = fObject.elements();

delete elem[fIndex];

}