Skip to main content
Participant
October 12, 2010
Question

Delete xml element not used in document

  • October 12, 2010
  • 3 replies
  • 3984 views

Hi,

If you have a number of texts tagged with an xml element and then delete the text, the element still exist in the structure.

You can see the element still connected to a text, by the blue diamond on the element symbol.

But i want do delete the elements that have no connection to texts, but cant find the right property to look for.

In Java script CS4.

Anyone knows?

/Mikael

This topic has been closed for replies.

3 replies

June 28, 2022

I'm from Brazil, so excuse me for my imperfect English... 

ctrl + alt + 1

Click on root when it open, select all archives, click on menu and 

Than you can delet ROOT.

All the XML that is not actually being used will be deleted

 

Community Expert
June 28, 2022

Hi @23353479 ,

thanks for your contribution!

 

FWIW:

This is an very old thread that started in 2010; you may have wondered why nobody did a suggestion like you did here.

 

My explanation:

This thread once belonged to a special forum called InDesign Scripting.

So only scripting solutions were discussed; ExtendScript (JavaScript), some AppleScript or VBScript in other threads.

 

Later, around October, November 2019, the InDesign Scripting forum was terminated due to a platform change and one by one over the years a lot of threads of that old InDesign Scripting forum were moved over to this InDesign User Forum here.

 

Regards,
Uwe Laubender
( Adobe Community Professional )

Participant
January 21, 2016

Does anyone solved this problem?

Inspiring
September 4, 2016

Hmmm, a little bit late for an answer, but this works for me. If someone would like to test it ...

var _xmlElement = app.activeDocument.xmlElements[0];

var _xmlPlaced = __isXMLElementPlaced(_xmlElement);

switch(_xmlPlaced) {

    case true :

        alert(_xmlElement.markupTag.name + " (or one of the children) placed");

        break;

    case false :

        alert(_xmlElement.markupTag.name + " not placed");

        break;

    default :

        alert("No valid XML Element.");

}

function __isXMLElementPlaced(_xmlElement) {

       

    if(!_xmlElement ||  !_xmlElement.isValid || !(_xmlElement instanceof XMLElement)) {

        return null;

    }

    var _xmlChildren;

    var _numOfXMLChildren;

    var _xmlChildPlaced;

    var i;

   

    if(_xmlElement.parentStory instanceof XmlStory && _xmlElement.xmlContent instanceof Text) {

        _xmlChildren = _xmlElement.xmlElements.everyItem().getElements();

        _numOfXMLChildren = _xmlChildren.length;

        for(i=0; i<_numOfXMLChildren; i++) {

            _xmlChildPlaced = __isXMLElementPlaced(_xmlChildren);

            if(_xmlChildPlaced === true) {

                return true;

            }

        }

        return false;

    } else {

        return true;

    }

} /* END function __isXMLElementPlaced */

Roland

Loic.Aigon
Legend
September 4, 2016

Hi,

Try this:

var main = function() {

  var doc = app.properties.activeDocument,

  xes, xe;

  if (!doc) return;

  xes = doc.xmlElements[0].evaluateXPathExpression(".//*");

  while ( xe = xes.pop() ) {

  xe.texts[0].isValid && !xe.texts[0].parentTextFrames.length && xe.remove();

  }

}

var u;

app.doScript("main();",u,u,UndoModes.ENTIRE_SCRIPT);

Jongware
Community Expert
Community Expert
October 12, 2010

I'm having troubles getting the blue diamond (Strange -- I seem to have no problem with that at work!)

But: perhaps you can test xmlItems[xxx].characters.length -- if it's zero, there are no characters connected. You should still check if it might have children, though, because it might be possible these still point to text.

(.. Thinking, while typing that last sentence: or maybe they don't .. logically, the 'parent' also points to each item 'in' it.)

Participant
October 13, 2010

Nope,

If you create a new document with two textframes, tag them "myTag", then delete one of the text frames, and run this:

var myTag  = app.activeDocument.xmlElements[0];

$.write(myTag.characters.length, myTag.contents);

You still get the characters and contents out. The same goes for paragraph, texts.

I just cant find the difference between the tag with content on the page or the one without...

/Mikael

Inspiring
November 16, 2010

Hi,

Does anyone found a solution for this problem?

I have the same problem. I can't find out how to detect if the XML element is attached to a object on the page or not. It's a mistery ... Maybe future generations will remember this problem as the one problem that couldn't be solved ...

John