Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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);
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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];
}
Copy link to clipboard
Copied
I appreciate the help Klaus. Unfortunately I can't use the index as a selector because the location of the child element within the parent element varies, which is why I pass the child element to be deleted directly into the function.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Hi Rick,
Thanks for your response. I need to delete the element from the FM document.
Thanks,
Tracey
Copy link to clipboard
Copied
I've got it going ...
rfu.Delete();