CHAPTER 13: XML Rules XML Rules Examples 190
}
else{
insertTextAsContent("\r", XMLElementPosition.afterElement);
}
}
return true;
} //End of apply function
}
function ProcessPrice(){
this.name = "ProcessPrice";
this.xpath = "/devices/device/price";
// Define the apply function.
this.apply = function(myElement, myRuleProcessor){
with(myElement){
insertTextAsContent("Price: $", XMLElementPosition.beforeElement);
//Add a return at the end of the XML element.
insertTextAsContent("\r", XMLElementPosition.afterElement);
}
return true;// Succeeded
} //End of apply function
}
}
NOTE: The above script uses scripting logic to add commas between repeating elements (in the
ProcessPackages XML rule). If you have a sequence of similar elements at the same level, you can use
forward-axis matching to do the same thing. Given the following example XML structure:
<xmlElement><item>1</item><item>2</item><item>3</item><item>4</item>
</xmlElement>
To add commas between each item XML element in a layout, you could use an XML rule like the
following (from the ListProcessing tutorial script):
var myRuleSet = new Array (new ListItems);
var myDocument = app.documents.item(0);
with(myDocument){
var elements = xmlElements;
__processRuleSet(elements.item(0), myRuleSet);
}
//Add commas between each "item" element.
function ListItems(){
this.name = "ListItems";
//Match all following sibling XML elements
//of the first "item" XML element.
this.xpath = "/xmlElement/item[1]/following-sibling::*";
this.apply = function(myElement, myRuleProcessor){
with(myElement){
insertTextAsContent(", ", XMLElementPosition.beforeElement);
}
return false; //Let other XML rules process the element.
}
}
Changing the XML structure using XML rules
Because the order of XML elements is significant in InDesign’s XML implementation, you might need to
use XML rules to change the sequence of elements in the structure. In general, large-scale changes to the