CHAPTER 13: XML Rules XML Rules Examples 202
function ProcessPrice(){
this.name = "ProcessPrice";
this.xpath = "/devices/device/price";
this.apply = function(myElement, myRuleProcessor){
var myDocument = app.documents.item(0);
with(myElement){
insertTextAsContent("Price: $", XMLElementPosition.beforeElement);
//Add a return at the end of the XML element.
insertTextAsContent("\r", XMLElementPosition.afterElement);
applyParagraphStyle(myDocument.paragraphStyles.item("Price"));
}
return true;
}
}
}
Creating page items with XML rules
The following script creates new page items, inserts the content of XML elements in the page items, adds
static text, and applies formatting. We include only the relevant XML-rule portions of the script here; for
more information, see the complete script (XMLRulesLayout).
The first rule creates a new text frame for each “device” XML element:
//Creates a new text frame on each page.
function ProcessDevice(){
this.name = "ProcessDevice";
this.xpath = "/devices/device";
this.apply = function(myElement, myRuleProcessor){
var myDocument = app.documents.item(0);
with(myElement){
insertTextAsContent("\r", XMLElementPosition.afterElement);
if(myDocument.pages.item(0).textFrames.length == 0){
myPage = myDocument.pages.item(0);
}
else{
myPage = myDocument.pages.add();
}
var myBounds = myGetBounds(myDocument, myPage);
var myTextFrame = placeIntoFrame(myPage, myBounds);
myTextFrame.textFramePreferences.firstBaselineOffset =
FirstBaseline.leadingOffset;
}
return true;
}
}
The “ProcessType” rule moves the “type” XML element to a new frame on the page: