Copy link to clipboard
Copied
I have a XML like below, I want to remove a element when its attribute value match,
<a identifier="sample">
</a>
<a identifier="sample1">
</a>
<a identifier="sample1">
</a>
var tempsect1ID = "Sample"
if (csect1.(@identifier==tempsect1ID))
delete csect1.(@identifier==tempsect1ID)
where the "delete" will remove complete XML instead of the particular node. Can some one please help me here?
1 Correct answer
Hi prtamil,
You can find that node with x path and remove it like this:
var myXMLFile = File("~/Desktop/Test.xml");
myXMLFile.open('r');
var root = new XML(myXMLFile.read());
myXMLFile.close();
var nodes = root[0].xpath("//*[@identifier='sample']");
for(var i = 0; i < nodes.length(); i){
delete nodes[0];
nodes = root[0].xpath("//*[@identifier='sample']");
}
Line number seven is the solution for your problem if you are finding your node in some other way like this:
if you have stored in a variabl
...Copy link to clipboard
Copied
well if the element returned by expression is root node then no misteries here.
Copy link to clipboard
Copied
Hi prtamil
Try below code:
var doc = app.activeDocument;
//Elements, Attributes, Value;
removeAttributes(doc,'title','aid:pstyle','H1');
function removeAttributes(source,element,attributename,attributevalue){
try{
for (var i = 0; i < source.xmlElements.length; i++){
try{
for(j=0; j<source.xmlElements.xmlAttributes.length; j++){
if(source.xmlElements.markupTag.name == element && source.xmlElements.xmlAttributes
//source.xmlElements.xmlAttributes
source.xmlElements.untag();
}
}
}
catch(e){}
removeAttributes(source.xmlElements,element,attributename,attributevalue);
}
}catch(e){};
} //End Funciton
Copy link to clipboard
Copied
hi sk88746243 ,
Well this code works in indesign file structure itself, but my current script is to work in the external XML file, any idea on this?
Copy link to clipboard
Copied
hi Loic.Aigon,
So how can I get the targeted element when the attribute has required value? May be XPATH?
Copy link to clipboard
Copied
Just a guess without testing:
Check the node kind of your query result. You likely get a node list rather than an individual element - after all the condition could match any number of elements. To delete them you'd then iterate the list and delete each element separately.
Copy link to clipboard
Copied
Hi prtamil,
You can find that node with x path and remove it like this:
var myXMLFile = File("~/Desktop/Test.xml");
myXMLFile.open('r');
var root = new XML(myXMLFile.read());
myXMLFile.close();
var nodes = root[0].xpath("//*[@identifier='sample']");
for(var i = 0; i < nodes.length(); i){
delete nodes[0];
nodes = root[0].xpath("//*[@identifier='sample']");
}
Line number seven is the solution for your problem if you are finding your node in some other way like this:
if you have stored in a variable let's just say myFoundNode :
delete myFoundNode;
In line number 6 you can see in for loop I did not incremented variable i so this this loop will go on n on n on untill n unless it deletes all the node with the given xPath
Best
Sunil

