Import multiple XML files not working
Copy link to clipboard
Copied
I'm working on a quiz booklet where each question is in its own xml file, so I'm trying to import multiple XML files into a single InDesign document. I'm attempting to append each XML inside the Root element, so I can just place the root into a text frame and have all the questions in a single story. My code is below, but it errors out and I can't figure out why.
var myDoc = app.activeDocument;
var myTransform = File.openDialog("Select XSL transform");
var myXMLImportPreferences = myDoc.xmlImportPreferences;
myXMLImportPreferences.allowTransform = true;
myXMLImportPreferences.createLinkToXML = true;
myXMLImportPreferences.ignoreUnmatchedIncoming = false;
myXMLImportPreferences.ignoreWhitespace = false;
myXMLImportPreferences.importCALSTables = true;
myXMLImportPreferences.importStyle = XMLImportStyles.appendImport;
myXMLImportPreferences.importTextIntoTables = false;
myXMLImportPreferences.importToSelected = true;
myXMLImportPreferences.removeUnmatchedExisting = false;
myXMLImportPreferences.repeatTextElements = false;
myXMLImportPreferences.transformFilename = myTransform;
var xmlFolder = Folder.selectDialog("Select the folder with the XML files");
if(xmlFolder !=null){
var xmlFiles = GetSubFolders(xmlFolder); //returns array of all xml files in xmlFolder and its subfolders
}
main();
function main(){
for(var x = 0; x < xmlFiles.length; x++){
$.writeln(xmlFiles
.name); if(xmlFiles
!= null){ app.select(myDoc.xmlElements.item(0));
myDoc.importXML(xmlFiles
); }
}
}
The error occurs on line 30, and all Extendscript does is highlight that line and display "importXML" where the error would normally appear. If I manually import the XML files, everything works. I don't have any issues with the xml or the transform. This code worked once, but only once, and every time since then, I get the same weird "importXML" error.
Any ideas?
Copy link to clipboard
Copied
Here's a fun new development, the code works great if I don't include a transform. If I set myXMLImportPreferences.allowTransform to false, it runs with no errors. When I set it back to true, the script errors after 0-2 xml files import.
The transform I'm using strips out some non-printing elements and strips excess whitespace. Without it, I get a bunch of extra hard returns and tabs and a bit of text.
Copy link to clipboard
Copied
May your XSLT file be Version 2.0 ?
Otherwise, you may make your code more solid by ensuring a file is actually selected and is XSLT format:
var main = function() {
var doc = app.properties.activeDocument,
xslt, xmlFiles;
if ( !doc ) {
alert("You need an open document" );
return;
}
xslt = getXSLT();
if ( !xslt ) return;
setXMLImportOptions(doc, xslt.fsName );
xmlFiles = getXMLFiles();
if ( !xmlFiles.length ) {
alert("No XML files found, sorry !");
return;
}
importXMLFiles ( doc, xmlFiles );
}
var u;
app.doScript ( "main()",u,u,UndoModes.ENTIRE_SCRIPT, "The Script" );
function importXMLFiles(doc,xmlFiles) {
var n = xmlFiles.length;
xmlFiles.reverse();
while ( n-- ) doc.importXML(xmlFiles
); };
function getXMLFiles(){
var xmlFolder = Folder.selectDialog("Select the folder with the XML files");
if ( !xmlFolder ) return [];
return GetSubFolders(xmlFolder); //returns array of all xml files in xmlFolder and its subfolders
}
function setXMLImportOptions(doc, xslt) {
doc.xmlImportPreferences.properties = {
allowTransform:true,
createLinkToXML:true,
ignoreUnmatchedIncoming:false,
ignoreWhitespace:false,
importCALSTables:true,
importStyle:XMLImportStyles.appendImport,
importTextIntoTables:false,
importToSelected:true,
removeUnmatchedExisting:false,
repeatTextElements:false,
transformFilename:xslt
};
}
function getXSLT(){
var mac = $.os[0]=="M",
macFilter = function(f){return /\.xslt?$/i.test(f.name) },
winFilter = "XSL Files : *.xsl;",
filter = mac ? macFilter : winFilter,
f = File.openDialog("Please select XSL file", filter );
return f;
}

