Skip to main content
Inspiring
May 8, 2023
Answered

Move a set of XML elements to another file.

  • May 8, 2023
  • 1 reply
  • 580 views

As a part of my workflow I need to move a set of existing XML elements from one document into the maching parent container of another document.

 

For example:

Doc1

<Root>

<container>

<Item1/>

<Item2/>

<Item3/>

</container>

</Root>

 

Doc2

<Root>

<container>

<Item1/>

<Item2/>

<Item3/>

</container>

</Root>

 

Should become:

 

Doc1

<Root>

<container>

<Item1/>

<Item2/>

<Item3/>

<Item4/>

<Item5/>

<Item6/>

</container>

</Root>

 

Where items 4-6 are moved over from Doc 2 into Doc 1.

 

I have been trying to modify the "ExtractSubset.jsx" script from the scripting SDK, but have not had any luck. Any help would be appreciated!

This topic has been closed for replies.
Correct answer Ben Ross - KP

I figured it out!

 

main();
    function main(){
        mySnippet();
        myTeardown();
    }
//ExtractSubset.jsx
//An InDesign JavaScript
// 
//You must copy the file "glue code.jsx" from the XML Rules folder (inside the Scripts
//folder inside your InDesign folder) to the folder containing this script, or provide a full
//path to the file in the next line.

function mySnippet(){
// Need to run this mySnippet function for each NUMBERED XML File

    var new_Tmp = app.open(File(templateFolder + "/KPCO_ProviderListing.indt"));
    var tmp = new_Tmp;
    importMyXML2();
    //Set XMP Info
    

    //![Extract subset.]
	if (app.documents.length != 0){
		var myDocument = new_Tmp;
        var pl = app.open(File(pl_path));
		var myRuleSet = new Array (new ExtractSpecialties);
		var myMarkupTag = pl.xmlTags.itemByName("providerSpecialtiesList");
		var myContainerElement = pl.xmlTags.itemByName("providerSpecialtiesList");
		with(myDocument){
			var elements = xmlElements;
			__processRuleSet(elements.item(0), myRuleSet);
		}
	}
	else{
		alert("No open document");
	}

    tmp.close(SaveOptions.NO);

}
function myTeardown(){
}

//![Extract subset - functions.]
function ExtractSpecialties(){
    var pl = app.open(File(bookFolder + "/2_CO_DNVRBLDR_Medicare_PL.indd"));
    var myNewElement;
    this.name = "ExtractSpecialties";
    this.xpath = "/Root/providerRolesList/providerRoleEntry/providerSpecialtiesList/providerSpecialty";	
    this.apply = function(myElement, myRuleProcessor){
        with(myElement){
            if(myElement.isValid){
                myNewElement = myElement.duplicate();
                myNewElement.move(LocationOptions.atEnd, pl.xmlElements.item(0).xmlElements.item(-1).xmlElements.item(-1).xmlElements.item(-1));
            }
        }
        return true;
    }

    
}

1 reply

Inspiring
May 8, 2023

My current script doesn't throw any errors, but also does not appear to actually DO anything.

 

//ExtractSubset.jsx
//An InDesign JavaScript
// 
//You must copy the file "glue code.jsx" from the XML Rules folder (inside the Scripts
//folder inside your InDesign folder) to the folder containing this script, or provide a full
//path to the file in the next line.
#include "glue code.jsx";
main();
function main(){
    mySetup();
    mySnippet();
    myTeardown();
}
function mySetup(){

var bookFolder = Folder.desktop + "/Indesign Automation POC/ID_ScriptOutput/May-2023_CO-Medicare_PD_MFX";

//File Name Variables

    var pl = app.open(File(bookFolder + "/pl.indd"));
    var tmp = app.open(File(bookFolder + "/tmp.indd"));

}
function mySnippet(){
    //![Extract subset.]
	if (app.documents.length != 0){
		var myRuleSet = new Array (new ExtractSpecialties);
		var myContainerElement = tmp.xmlElements.item(0).xmlElements.item(0).xmlElements.item(0);
		with(tmp){
			var elements = xmlElements;
			__processRuleSet(elements.item(0), myRuleSet);
		}
	}
	else{
		alert("No open document");
	}
    //![Extract subset.]
}
function myTeardown(){
}


//![Extract subset - functions.]
function ExtractSpecialties(){
    var myNewElement;
    this.name = "ExtractSpecialties";
    this.xpath = "/root/providerRolesList/ProviderRoleEntry/providerSpecialtiesList/providerSpecialty";	
    this.apply = function(myElement, myRuleProcessor){
        with(myElement){
            if(myElement.name == "providerSpecialty"){
                myNewElement = myElement.copy();
                myNewElement.paste(LocationOptions.atEnd, pl.xmlElements.item(0).xmlElements.item(0).xmlElements.item(0).xmlElements.item(-1));
            }
        }
        return true;
    }
}

 

The XML structrure looks like this in ID:

 

 

Ben Ross - KPAuthorCorrect answer
Inspiring
May 10, 2023

I figured it out!

 

main();
    function main(){
        mySnippet();
        myTeardown();
    }
//ExtractSubset.jsx
//An InDesign JavaScript
// 
//You must copy the file "glue code.jsx" from the XML Rules folder (inside the Scripts
//folder inside your InDesign folder) to the folder containing this script, or provide a full
//path to the file in the next line.

function mySnippet(){
// Need to run this mySnippet function for each NUMBERED XML File

    var new_Tmp = app.open(File(templateFolder + "/KPCO_ProviderListing.indt"));
    var tmp = new_Tmp;
    importMyXML2();
    //Set XMP Info
    

    //![Extract subset.]
	if (app.documents.length != 0){
		var myDocument = new_Tmp;
        var pl = app.open(File(pl_path));
		var myRuleSet = new Array (new ExtractSpecialties);
		var myMarkupTag = pl.xmlTags.itemByName("providerSpecialtiesList");
		var myContainerElement = pl.xmlTags.itemByName("providerSpecialtiesList");
		with(myDocument){
			var elements = xmlElements;
			__processRuleSet(elements.item(0), myRuleSet);
		}
	}
	else{
		alert("No open document");
	}

    tmp.close(SaveOptions.NO);

}
function myTeardown(){
}

//![Extract subset - functions.]
function ExtractSpecialties(){
    var pl = app.open(File(bookFolder + "/2_CO_DNVRBLDR_Medicare_PL.indd"));
    var myNewElement;
    this.name = "ExtractSpecialties";
    this.xpath = "/Root/providerRolesList/providerRoleEntry/providerSpecialtiesList/providerSpecialty";	
    this.apply = function(myElement, myRuleProcessor){
        with(myElement){
            if(myElement.isValid){
                myNewElement = myElement.duplicate();
                myNewElement.move(LocationOptions.atEnd, pl.xmlElements.item(0).xmlElements.item(-1).xmlElements.item(-1).xmlElements.item(-1));
            }
        }
        return true;
    }

    
}