6 Replies Latest reply on Mar 4, 2013 4:20 AM by designmon79

    replace text of starting attribute value

    designmon79 Level 1

      Hi Forum,

       

      How to globally replace starting value of an xml attribute?  I have an indesign document that has attribute name month and value jan13issue01.

       

      See example below of existing xml attribute and value.

       

      months=jan13issue01

       

      I need to change the above value to mar14issue01 globally (see below).

       

      months=mar14issue01.

       

      Thanks,

      Mon

        • 1. Re: replace text of starting attribute value
          R-Bala-Krishnan Level 2

          Hi Mon,

           

          Where ever in the document, if attributes start with "jan13" need to update as "mar14". am i right?

           

          please try b/m code

           

           

          var myDoc = app.documents.item(0);

          getAllXMLElements(myDoc);

          function getAllXMLElements(elm){      

              for(var i1=0; i1<elm.xmlElements.length; i1++){       

                  for(var i2=0; i2<elm.xmlElements[i1].xmlAttributes.length; i2++){                   

                      if((elm.xmlElements[i1].xmlAttributes.item(i2).value.match (RegExp('^jan13')) != null)){          

                          elm.xmlElements[i1].xmlAttributes.item(i2).value = String(elm.xmlElements[i1].xmlAttributes.item(i2).value).replace('jan13','mar14');

                          }

                      }

                  getAllXMLElements(elm.xmlElements[i1]);

                  }  

              }

           

          --

          Thanks

          Bala

          1 person found this helpful
          • 2. Re: replace text of starting attribute value
            designmon79 Level 1

            First of all, thanks for your quick response.

             

            I require to change the attributes value only in attribute months.

             

            Thanks,

            Mon

            • 3. Re: replace text of starting attribute value
              R-Bala-Krishnan Level 2

              Okay, In condition add the check for attribute name.

              if((elm.xmlElements[i1].xmlAttributes.item(i2).name == "months") && (elm.xmlElements[i1].xmlAttributes.item(i2).value.match (RegExp('^jan13')) != null)){          

              Regards,

              Bala

              • 4. Re: replace text of starting attribute value
                designmon79 Level 1

                Thanks, Bala.

                 

                One more request: I want to meet certain conditions. Like, if attribute months has attribute value jan13issue01 and attribute year has attribute value 2013 than months value should change mar13issue01 if attribute year has attribute value 2012 than months value should change mar12issue01. Is it possible? See screenshot.

                 

                date.png

                 

                Mon

                • 5. Re: replace text of starting attribute value
                  R-Bala-Krishnan Level 2

                  Yes, can....

                              if((elm.xmlElements[i1].xmlAttributes.item(i2).name == "months") && (elm.xmlElements[i1].xmlAttributes.item(i2).value.match (RegExp('^jan')) != null)){          

                                  switch(elm.xmlElements[i1].xmlAttributes.item("year").value){

                                      case "2012":                   

                                      elm.xmlElements[i1].xmlAttributes.item(i2).value = String(elm.xmlElements[i1].xmlAttributes.item(i2).value).replace('jan12','mar12');

                                      //you can directly assign value also

                                      //elm.xmlElements[i1].xmlAttributes.item(i2).value = "jan12issue01";

                                      break;

                                      case "2013":

                                      elm.xmlElements[i1].xmlAttributes.item(i2).value = String(elm.xmlElements[i1].xmlAttributes.item(i2).value).replace('jan13','mar13');

                                      //you can directly assign value also

                                      elm.xmlElements[i1].xmlAttributes.item(i2).value = "jan13issue01";

                                      break;

                                      default:

                                      break;

                                      }

                                  }

                   

                  Thanks

                  Bala