I want to import some images into an indesign file. I get the images and the xml from amazon custom orders, so the xml isnt quite what Indesign expects. For example, indesign would define and use an xml image tag like
The provided xml would define the image with more attributes, and if I try to import that xml in Indesign I cant use the image at all, I would get a text frame with the filename of the photo... The definition in the provided xml looks like:
<image> <imageName>somefilename.jpg</imageName> <buyerFilename>somefilename.jpg</buyerFilename> <dimension> <width>3360</width> <height>2240</height> </dimension> </image>
What options do I have? Can I edit the xml with a script before importing? Should I use a js script that would read a json file (also provided) and use that to replace the images? Or import the xml as it is and use another script to change the text frames containing the filename with the coresponding photos - I remember seeing such a script...
You may be interested in XSLT. The following xsl transforms xml to the expected structure :
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="1.0"> <xsl:output method="xml" indent="yes"/> <xsl:template match="/"> <images> <xsl:apply-templates select="image"/> </images> </xsl:template> <xsl:template match="image"> <image> <xsl:attribute name="href"> <xsl:text>file://</xsl:text> <xsl:text>some/local/path/</xsl:text> <xsl:value-of select="buyerFilename"/> </xsl:attribute> </image> </xsl:template> </xsl:stylesheet>
Note that you need to tweak this to match your requirements.
Hi Loic and thank you for the reply. So from what I understand I would use this XSLT instead of the normal xml and import the xslt file in indesign, right? Any text editor would be good to to this?
For my particular case, I would need several indesign files with a xml each that contains the information to import 10 images in each indesign file. So... editing each xml file would not be a time efficient way to do the job. So I'm thinking the best way would be to use the json file provided (basically the same info as the xml) and use a script to: look for any json file in the same folder as the indesign file, load that info and identify the names of the images, place them in their designated picture frames. That's the road map, if I could get a nudge to something similar I would be very grateful...
InDesign embarks its own XSLT processor (Xalan if I am not wrong). You can use XSL file to transform the incoming xml file (from Amazon) to InDesign compliant structure thanks to specific instructions.
The point is to write a XSL file down that can match all incoming files structures and have the structure you want on output. And XSLT transformation can also be attached to XML import options with scripting.
If you have zillions XML files, one per image, write a script to parse XML files and built your own XML output file.
Or sure use json data if that hosts all data to eventually output the incoming XML file.
Up to you.
Yes, this looks more promising, once I get my head around it. I'm an advanced Indesign user, but just never had to do any xml related stuff, and now I'm surprised at what a big chunk I was missing. So, from what I understand xslt is kind of a templating for transforming the xml into another xml that indesign would recognize. If I could get Indesign to apply the same templating every time (I'm hoping it does, in a similar way it remembers the page number of an imported pdf if you dont check 'show options' when placing the pdf) that would work for this situation. At the moment I am trying to get the XSLT to work. Yours gave this as a result:
<?xml version="1.0"?> <images/>
and I cant get to understand exactly what I need to change. Tried this
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="xml" indent="yes"/> <xsl:template match="image"> <image> <xsl:attribute name="href"> <xsl:text>file://</xsl:text> <xsl:value-of select="imageName"/> </xsl:attribute> </image> </xsl:template> </xsl:stylesheet>
and I do get the images with the href I need, but I also get a lot of other values without tags and that messes with the import, its not a valid xml I guess...
"So, from what I understand xslt is kind of a templating for transforming the xml into another xml that indesign would recognize."
Yes that's exactly that.
Ok so a reason why you would get a single <images/> node is that the XSLT processor cannot find node that fits the selection. Try to enlarge the selection using this xpath (see double slash in front of image):
<xsl:template match="/"> <images> <xsl:apply-templates select="//image"/> </images> </xsl:template>
That you get the whole structure added to transformed image node is more mysterious right now. At least your snippet works here. But I would recommend starting transformation at the root of the file to avoid bad output xml structure.
<xsl:template match="/"> <--! let's define a root node for output <output> <--! let's start transformation from root <--!calling images transformation template--> </output> <xsl:template>
On a final word, XSL is not really hard, just cumbersome.
Yes ! Working exactly as I wanted, no scripting needed. It does remember to apply the xslt I have saved in a different file - if I uncheck "show xml options". And all of a sudden, all the images are being replaced with the new ones. Magic ! 😄 Thank you so much !
Now I need to find resources to get to know xslt better...
Glad it helped.