0

I'm using Indesign to import StatCrew XML files and create post-game graphics. I should probably give more detail. StatCrew generates a different xml file. Below is the full file:

https://drive.google.com/file/d/11sGFbXlkTlEwmzBVE-yMZVBbYYPLEwLK/view?usp=sharing

Here is my current XSLT file:

https://drive.google.com/file/d/1NJ6ltaz-sxufFBV7QCsjFn7l36HxKeHJ/view?usp=sharing

Using the XSLT above, I can find the player that scored the most points and have indesign select a photo from a directory that is the same name as the @uni number. In this example, Indesign would display the .psd file named 05.psd.

Exported from Indesign as a JPG. I get this:

https://i.stack.imgur.com/j27cS.jpg

What I would like to do, however, is have Indesign select a .psd file at random from a FOLDER named 05. Is that possible? I'm learning all of this from information on the internet and have no real background in XML..

2 Answers2

1

I guess you need to find out which XSLT processor you can use with InDesign and if it supports some file system access and random number generation.

In XSLT 3 as supported by Saxon 9.8 and Altova 2017/2018 you can generate a random number https://www.w3.org/TR/xpath-functions/#func-random-number-generator e.g.

random-number-generator(current-dateTime())?number

which will give a double between 0 (including) and 1 (excluded) you could then multiply by the number of images you have and round to get an integer number. Another option is to use the permute function of the random number generator with e.g.

random-number-generator(current-dateTime())?permute(1 to 20)[1]

to give you a random integer between 1 and 20 where of course you would adjust the expression 1 to 20 to the number of images you have.

It should also be possible with Saxon 9.8 to determine the .psd files in a location using e.g. uri-collection('file:///C:/folder/subfolder?select=*.psd') so you could then use

random-number-generator(current-dateTime())?permute(uri-collection('file:///C:/folder/subfolder?select=*.psd'))[1]

to directly select a random file URI from that location.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • As a quick note, InDesign uses Xalan processor and only supports XSLT version 1.0 – Loic Dec 02 '17 at 14:25
  • It requires an XSLT 3 processor like Saxon 9.8 which depending on your project you might be able to plug in instead of Xalan. – Martin Honnen Dec 02 '17 at 15:56
0

I don't think you can do it with XSLT but an indesign script makes it easy. Here I add a url parameter that you can work with XSLT.

var main = function() {
 var m = $.os[0]=="M",
 wf = "XML files : *.xml;",
 mf = function(f){return (f instanceof Folder) || /\.xml$/.test(f.name)},
 f = File.openDialog( "Please select xml file…", m? mf : wf ),
 xo,
 ps, p, n,
 uni,
 baseFolder = Folder ( Folder.desktop ),
 playerFolder, psds, fileAttr;
 
 if ( !f ) return;
 
 f.open('r');
 f.encoding = "UTF-8";
 xo = XML( f.read() );
 f.close();
 
 ps = xo.player;
 n = ps.length();
 
 while ( n-- ) {
  p = ps[n];
  
  uni = String(p.@uni);
  playerFolder = Folder ( baseFolder+"/"+uni );
  if ( !playerFolder.exists ) {
   fileAttr = uni+".psd";
  }
  else {
   psds = playerFolder.getFiles ( "*.psd" );
   if ( !psds.length ) {
    fileAttr = uni+".psd"; 
   }
   else {
    fileAttr = uni+"/"+decodeURI(psds[ getRandomInt ( 0, psds.length )].name);
   }
  }
  p.@url = fileAttr;
 }
 
 f.open( 'w');
 f.write ( '<?xml version="1.0" encoding="UTF-8"?>\r'+xo.toXMLString() );
 f.close();
 
 f.execute();
 
}

function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1) + min);
}

var u;

app.doScript ( "main()",u,u,UndoModes.ENTIRE_SCRIPT, "The Script" );

Once run, it will prompt you for a XML file and will add a new attribute (url) that contains the random url.

<player url="05/A.psd" uni="05" code="05" name="ONE, Player" checkname="ONE,PLAYER" class="JR" gp="1">

Then you can tweak your xsl:

           <xsl:copy-of select="$WBB_player_location" />player/<xsl:value-of select="@url"/>
Loic
  • 2,173
  • 10
  • 13