Copy link to clipboard
Copied
I am trying to change position of nav-pointer tag as immediate sibling of term element as like shown below,
Hi @Karthik SG , i don‘t do much InDesign XML work, but I think you would use the XMLElement move method. I can’t see your root, but if index-entry was level 1 then:
//root
var xroot = app.activeDocument.xmlElements.item(0);
//root’s elements
var xml1 = xroot.xmlElements.item(0);
//xml1’s elements
var xml2 = xml1.xmlElements.item(0);
//the 1st element in xml2
var term = xml2.xmlElements.item(0)
//the 3rd element in xml2
var newpoint = xml2.xmlElements.item(2);
//move newpoint
newpoint.move(Lo
...
I tried using XPath.
If each element is a sibling, move.
var targetElementName = "nav-pointer";
var referenceElementName = "term";
// Target element with the reference element in the same hierarchy
var targetElementArray = app.activeDocument.xmlElements.firstItem().evaluateXPathExpression("//" + targetElementName + "[../" + referenceElementName + "]");
// Reference element with the target element in the same hierarchy
var referenceElementArray = app.activeDocument.xmlElements.firstItem().evaluat
...
Try this code:
moveTerm()
function moveTerm(){
var myDoc = app.documents[0];
var xPath = ['//term'];
var root = myDoc.xmlElements[0];
var node = null;
try{
var proc = app.xmlRuleProcessors.add(xPath);
var match = proc.startProcessingRuleSet(root);
while (match != undefined){
node = match.element;
match = proc.findNextMatch();
if(node != null && node != undefined){
if(node.parent.xmlElements.length > n
...
Copy link to clipboard
Copied
Hi @Karthik SG , i don‘t do much InDesign XML work, but I think you would use the XMLElement move method. I can’t see your root, but if index-entry was level 1 then:
//root
var xroot = app.activeDocument.xmlElements.item(0);
//root’s elements
var xml1 = xroot.xmlElements.item(0);
//xml1’s elements
var xml2 = xml1.xmlElements.item(0);
//the 1st element in xml2
var term = xml2.xmlElements.item(0)
//the 3rd element in xml2
var newpoint = xml2.xmlElements.item(2);
//move newpoint
newpoint.move(LocationOptions.after, term);
$.writeln(xml1.markupTag.name)
$.writeln(xml2.xmlElements.count(0))
$.writeln(xml2.xmlElements.item(2).markupTag.name)
move method
Copy link to clipboard
Copied
I tried using XPath.
If each element is a sibling, move.
var targetElementName = "nav-pointer";
var referenceElementName = "term";
// Target element with the reference element in the same hierarchy
var targetElementArray = app.activeDocument.xmlElements.firstItem().evaluateXPathExpression("//" + targetElementName + "[../" + referenceElementName + "]");
// Reference element with the target element in the same hierarchy
var referenceElementArray = app.activeDocument.xmlElements.firstItem().evaluateXPathExpression("//" + referenceElementName + "[../" + targetElementName + "]");
for(var i =0; i < referenceElementArray.length; i++){
targetElementArray[i].move(LocationOptions.AFTER,referenceElementArray[i]);
}
Copy link to clipboard
Copied
Try this code:
moveTerm()
function moveTerm(){
var myDoc = app.documents[0];
var xPath = ['//term'];
var root = myDoc.xmlElements[0];
var node = null;
try{
var proc = app.xmlRuleProcessors.add(xPath);
var match = proc.startProcessingRuleSet(root);
while (match != undefined){
node = match.element;
match = proc.findNextMatch();
if(node != null && node != undefined){
if(node.parent.xmlElements.length > node.index+1){
var nextSibling = null;
for(var i = node.index+1; i < node.parent.xmlElements.length; i++){
if(node.parent.xmlElements[i].markupTag.name == "nan-pointer"){
nextSibling = node.parent.xmlElements[i];
break;
}
}
if(nextSibling != null){
nextSibling.move(LocationOptions.after, node);
}
}
}
}
}
catch(ex){}
finally{
proc.endProcessingRuleSet();
proc.remove();
}
}
Best
Sunil
Copy link to clipboard
Copied
I have never used xmlRuleProcessors, so it was very helpful.
Thank you.
Copy link to clipboard
Copied
If you use xmlRuleProcessor, you will never have to be dependent on gluecode ever. Rule processor will find each & every node in sequential manner.
evaluateXPathExpression is also used for finding node (it won't be able find nodes in sequntial).