Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Delete xml element not used in document

New Here ,
Oct 12, 2010 Oct 12, 2010

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

TOPICS
Scripting
3.7K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 12, 2010 Oct 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.)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 13, 2010 Oct 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 16, 2010 Nov 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Nov 17, 2010 Nov 17, 2010

Here is an absurd way :

var doc = app.activeDocument;
var root=doc.xmlElements[0];
var n1 = root.xmlElements[0];
app.select(n1.parentStory.characters[0]); //Crashes Indesign if n1 is not placed in the document

But it doesn't help that much sorry. Looking further...

Loic

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Nov 17, 2010 Nov 17, 2010

Maybe this way :

var doc = app.activeDocument;
var root=doc.xmlElements[0];
var n1 = root.xmlElements[0];
var n2= root.xmlElements[1];
try{
    n1.insertionPoints[0].parent.texts[0].parentTextFrames[0].constructor;
    alert("It's on the document !");
}
catch(e){
    alert("No, it isn't !");
}

If a node is placed on the document you get access to the parent text frame. On the contrary, this call will fail if the node isn't placed.

Loic

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 13, 2011 Jun 13, 2011

I heard this works but haven't tried myself:

app.activeDocument.deleteUnusedTags()

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 14, 2011 Jun 14, 2011

Thanks Rob,

It do exactly what you expect, it deletes Unused Tags (From the tag list)

But Unused XML Elements, still there, and still bugging me.

/Mikael

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jun 14, 2011 Jun 14, 2011

If the (text) element is unplaced, its parentStory will be an XmlStory rather than a Story.

So try:

var elements = app.activeDocument.xmlElements[0].xmlElements.everyItem().getElements();
for (var i = elements.length - 1;i >= 0;i--){
    if(elements.parentStory.constructor.name === "XmlStory"){
        elements.remove();
    }
}

This is untested. If you have tagged graphics frames it will need to be modified to not error on those.

Jeff

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 21, 2016 Jan 21, 2016

Does anyone solved this problem?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 04, 2016 Sep 04, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Sep 04, 2016 Sep 04, 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);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 05, 2016 Sep 05, 2016

Hi Loic,

good idea with texts[0]. It works.

The reason why I ended up using a recursive function instead of xpath: some of the elements in my document are not placed, but have placed children.

Roland

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jun 28, 2022 Jun 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 

Chrislene23353479s8qy_0-1656430473090.pngexpand image

Than you can delet ROOT.

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

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 28, 2022 Jun 28, 2022
LATEST

Hi @Deleted User ,

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 )

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines