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

How to add contents into the xml element?

New Here ,
Jul 01, 2015 Jul 01, 2015

I need to insert a word sqrt(plain text without tags ) in the xml element of m:msqrt like below:

enter image description here

I tried ,

     

var path = (app.activeDocument.fullName.parent.fsName).toString().replace(/\\/g, '/');//path of active document(sample.indd)

     

    var xmlPath = (app.activeDocument.fullName.parent.fsName).toString().replace(/\\/g, '/')+"/with_aid2.xml";

    xmlFile = File (xmlPath);

    if(xmlFile.exists){

    xmlFile.open();

    var myString = xmlFile.read();

    xmlFile.close();

    myXml = new XML ();

    myXml = XML(myString);

    var msqrtCount = myXml.xpath("//m:msqrt");

  

    }else{alert(xmlPath+" doesn't exist!");}

  

  

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

    var added =    msqrtCount;

        added.contents="sqrt";     

      

        }

I get an error says invalid content property.

TOPICS
Scripting
914
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

correct answers 1 Correct answer

Enthusiast , Jul 01, 2015 Jul 01, 2015

Try this,

var myDoc = app.activeDocument;

mySqrtTag(myDoc)

function mySqrtTag(elm)

{

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

    {

        if(elm.xmlElements.markupTag.name.toString() == "m:msqrt")

        {

            elm.xmlElements.insertionPoints[0].contents = "sqrt";

        }

        mySqrtTag(elm.xmlElements);

    }

}

Translate
People's Champ ,
Jul 01, 2015 Jul 01, 2015

Hi,

Looks like you are messing two approaches here.

xpath is a method to be applied on a pure XML object when .contents is a InDesign Javascript property.

So either you edit "contents" of the xml node with

msqrtCount = "sqrt";

But it will remain in the XML object context so you need to later save back this xml object to file if you want to inject the modifications.

Or you want to work on the indesign context but then it's not xpath you want to use but either xmlRules or evaluateXPathExpression. Those will return xmlElements which have a contents property:

var doc = app.activeDocument,

xes = doc.xmlElements[0].evaluateXPathExpression ("//m:msqrt" ),

n = xes.length;

while ( n-- ) xes.contents = "sqrt";

HTH

Loic

http://www.ozalto.com

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 ,
Jul 01, 2015 Jul 01, 2015

I tried your code, but no output. It doesn't inject that word into the element. And when I alert n variable it outputs 0.

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 ,
Jul 01, 2015 Jul 01, 2015

Try this,

var myDoc = app.activeDocument;

mySqrtTag(myDoc)

function mySqrtTag(elm)

{

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

    {

        if(elm.xmlElements.markupTag.name.toString() == "m:msqrt")

        {

            elm.xmlElements.contents = "sqrt";

        }

        mySqrtTag(elm.xmlElements);

    }

}

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 ,
Jul 01, 2015 Jul 01, 2015

Apologies! Actually I need to append this content to existing content..Pls refer screenshot, Just I need to append in front of the existing contents and child elements

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 ,
Jul 01, 2015 Jul 01, 2015
LATEST

Try this,

var myDoc = app.activeDocument;

mySqrtTag(myDoc)

function mySqrtTag(elm)

{

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

    {

        if(elm.xmlElements.markupTag.name.toString() == "m:msqrt")

        {

            elm.xmlElements.insertionPoints[0].contents = "sqrt";

        }

        mySqrtTag(elm.xmlElements);

    }

}

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