Skip to main content
Inspiring
December 1, 2014
Question

How to delete xml content without deleting the content of the object?

  • December 1, 2014
  • 4 replies
  • 1248 views

Hey, with the following code I am marking up a page item and deleting it's xml content:

text_value = text_area.xmlElements.add('text', current)

text_value.contents = '';

current is the current app.selection. The code works, with one downside: It also deletes the content of my text frame in my document. I only want to delete the xml content. Is this somehow possible?

Thanks in advance!!

Regards

Fred

This topic has been closed for replies.

4 replies

Adobe-InDesign_CS4
Inspiring
December 9, 2014

Dear Fred,

Please use the below Code, this will helps you to achieve your target.

//============================== Script Starts ============================================//

var doc = app.documents[0];

//========================================================================//

/* Use Glue Code */

//==================================================================//

__RemoveXMLElments(doc);

function __RemoveXMLElments(doc)

{

      var elementXpath = "//text";

        var myRuleSet = new Array (new __RemoveXMLElments_XPath(elementXpath));

      with(doc){

        var elements = xmlElements;

        __processRuleSet(elements.item(0), myRuleSet);

        }

}

function __RemoveXMLElments_XPath (Txts)

{

        this.name = "RemoveTexts";

        this.xpath = Txts;  

        this.apply = function(myElement, myRuleProcessor){

            with(myElement){

                try{  

                        var myCnts = myElement.texts.everyItem();

                        myCnts.remove();

                  }catch(e)

                    {

                        }

              }

          }

    }

//============================== Script End============================================//

Thanks & Regards

T.R.Harihara SudhaN

StammAuthor
Inspiring
December 12, 2014

Hey,

I didn't use xml rules yet, I will look into this topic and come back here as soon as I know more! Thank you for your help!

Inspiring
December 8, 2014

Your original code works as you describe for me (assuming text_area is set to the Root element).

Participating Frequently
December 5, 2014

Hi,

1. Create a textframe and tag it with an xml Element (e.g. "myTextFrame")

2. Put some text in the textframe

3. Select the frame

4. run the script below

you added a new xml Element within the tagged frame and changed the content.

var curSel = app.selection[0];

var curXMLElement = curSel.associatedXMLElement;

var newXMLElement = curXMLElement.xmlElements.add('text');

newXMLElement.contents = "new Content";

Hope this makes sense.

Thanks Stefan

StammAuthor
Inspiring
December 5, 2014

I can't do this. I need to delete the contents of my associated xml element, without deleting the content of the text-frame. I need A) the textframe associated with my xml Element and B) the contents inside the xml tag <text>asd</text> gone, like: <text></text>, without deleting the frame content.

I did a little workaround now, althought this solution is not really clean.

I save the contents of the textframe in another variable, delete this content, and then assign the content to the xml attribute where I need it. Of course the user has to close the document without saving then.

text_value = xmlElements.add('text', current);

var textContents = text_value.contents;

var textSize = current.parentStory.characters.item(0).pointSize;

text_value.contents = '';

with(text_value.xmlAttributes){

    add('value', textContents);

}

Chinnadk
Legend
December 9, 2014

Is this you asking?

var doc = app.activeDocument;

var text = doc.xmlElements.item(0).evaluateXPathExpression("//text");

for(var i =0;i<text.length;i++)

{

        text.texts.everyItem().remove();

    }

Regards,

Chinna

StammAuthor
Inspiring
December 3, 2014

Does no one have a solution to this? I'm frustrated :/