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

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.
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);
}
}
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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);
}
}
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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);
}
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now