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

Delete XML node from XML file

Community Beginner ,
May 02, 2019 May 02, 2019

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?

TOPICS
Scripting
3.0K
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

Advocate , Jul 04, 2019 Jul 04, 2019

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

...
Translate
People's Champ ,
May 05, 2019 May 05, 2019

well if the element returned by expression is root node then no misteries here.

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
Community Beginner ,
May 06, 2019 May 06, 2019

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.name == attributename && source.xmlElements.xmlAttributes.value == attributevalue){ 

                        //source.xmlElements.xmlAttributes.remove(); 

                        source.xmlElements.untag(); 

                    } 

                } 

            }

   

        catch(e){} 

            removeAttributes(source.xmlElements,element,attributename,attributevalue); 

    } 

}catch(e){};

} //End Funciton

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
Community Beginner ,
May 11, 2019 May 11, 2019

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?

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
Community Beginner ,
May 11, 2019 May 11, 2019

hi Loic.Aigon,

So how can I get the targeted element when the attribute has required value? May be XPATH?

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
Guide ,
May 11, 2019 May 11, 2019

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.

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
Advocate ,
Jul 04, 2019 Jul 04, 2019
LATEST

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

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