Skip to main content
Participant
March 25, 2019
Answered

Mapping XML Tags to Styles Indesign Scripting

  • March 25, 2019
  • 1 reply
  • 2584 views

Hi guys, so i've uploaded a xml file to my current document. I'm working through this api and i'm currently trying to style the xml tags that I have imported using a template. Please take a look at my code and tell me what I am missing.

Setup.js:

var myDocument = app.open(File(app.activeScript.parent.fsName + '/firstTemplate.indt'));

var curFile = app.activeScript.parent.fsName + '/library.xml';

myDocument.importXML(File(curFile));

// need to apply styling to tags

myDocument.mapXMLTagsToStyles();

var root = myDocument.xmlElements[0]; // root

var element = root.xmlElements[0];

root.placeXML(myDocument.textFrames[0]);

myDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.points;

myDocument.viewPreferences.verticalMeasurementUnits = MeasurementUnits.points;

myDocument.viewPreferences.rulerOrigin = RulerOrigin.pageOrigin;

var price = root.evaluateXPathExpression('//pricing');

My Indt file has paragraph stylings that are to be applied to any document I run the script on just as long as the same xml tags match up to the styling.

Thank you!

This topic has been closed for replies.
Correct answer Loic.Aigon

mapXMLTagsToStyles doesn't work differently in scripting than in UI. You need to let InDesign know which styles have to be bound to tags through xmlImportMaps which can be translated by the map tags to styles in the tags panel :

var myImportMap = doc.xmlImportMaps.add(myTag,myStyle);

doc.mapXMLTagsToStyles();

1 reply

Loic.Aigon
Loic.AigonCorrect answer
Legend
March 26, 2019

mapXMLTagsToStyles doesn't work differently in scripting than in UI. You need to let InDesign know which styles have to be bound to tags through xmlImportMaps which can be translated by the map tags to styles in the tags panel :

var myImportMap = doc.xmlImportMaps.add(myTag,myStyle);

doc.mapXMLTagsToStyles();

Inspiring
May 17, 2023

@Loic.Aigon Is there a way to force Indesign to re-apply an existing map, or import a map from another file (i.e. a template file)?

 

I am moving XML elements between files, but the existing map is not being applied to the new elements properly.

Loic.Aigon
Legend
May 17, 2023

Given that the UI doesn't allow to import such rules, the DOM doesn't expose such functionnality.

That said, recall 

var myImportMap = doc.xmlImportMaps.add(myTag,myStyle);

doc.mapXMLTagsToStyles();

and that should do it (once again in the UI, if you reset styles maps, contents styling get updated).