7 Replies Latest reply on Dec 12, 2014 1:58 AM by Stamm

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

    Stamm Level 1

      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

        • 1. Re: How to delete xml content without deleting the content of the object?
          Stamm Level 1

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

          • 2. Re: How to delete xml content without deleting the content of the object?
            steverocket2 Level 1

            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

            • 3. Re: How to delete xml content without deleting the content of the object?
              Stamm Level 1

              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);

              }

              • 4. Re: How to delete xml content without deleting the content of the object?
                cchimi Level 2

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

                • 5. Re: Re: How to delete xml content without deleting the content of the object?
                  Chinnadk Level 4

                  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[i].texts.everyItem().remove();
                      }
                  

                   

                  Regards,

                  Chinna

                  • 6. Re: How to delete xml content without deleting the content of the object?
                    Adobe-InDesign CS4

                    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

                    • 7. Re: How to delete xml content without deleting the content of the object?
                      Stamm Level 1

                      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!