Copy link to clipboard
Copied
I am trying to import XML data to InDesign and I have no problem with single template using tags but I would like to add attribute to <PAGE> node with template name or url to use during import.
Is using different template for each node even possible?
This is simple XML data which I am using:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<PAGES>
<PAGE>
<NAME>DIAG-1</NAME>
<Image href="file:///images/DIAG-1.jpg"/>
<FOOTNOTE>TEXT 1</FOOTNOTE>
</PAGE>
<PAGE>
<NAME>DIAG-2</NAME>
<Image href="file:///images/DIAG-2.jpg"/>
<FOOTNOTE>TEXT 2</FOOTNOTE>
</PAGE>
<PAGE>
<NAME>DIAG-3</NAME>
<Image href="file:///images/DIAG-3.jpg"/>
<FOOTNOTE>TEXT 3</FOOTNOTE>
</PAGE>
</PAGES>
Copy link to clipboard
Copied
Please try this.
var myDoc = app.documents[0];
addAttribute('PAGE')
function addAttribute(myTag) // To Place the Runnnig Header
{
myRuleSetinsertPMI = new Array (new insertPMI);
with(myDoc){
var elements = xmlElements;
__processRuleSet(elements.item(0), myRuleSetinsertPMI);
}
function insertPMI(){
this.name = "r";
this.xpath = "//"+myTag;
this.apply = function(myElement, myRuleProcessor){
with(myElement){
if (myElement.xmlAttributes.itemByName("temp:id").isValid == false) {
var myDocName = ''+ myDoc.name;
myElement.xmlAttributes.add({name:"temp:id",value: myDocName})
myElement.xmlAttributes.add({name:"href",value: 'c:/----'})
}
}
}
}
}
/////////////////////////////////
//========================================================================================
//
// $File: //depot/indesign_5.0/gm/build/scripts/xml rules/glue code.jsx $
//
// Owner: Lin Xia
//
// $Author: sstudley $
//
// $DateTime: 2007/02/15 13:37:33 $
//
// $Revision: #1 $
//
// $Change: 505969 $
//
// Copyright 2006 Adobe Systems Incorporated. All rights reserved.
//
// NOTICE: Adobe permits you to use, modify, and distribute this file in accordance
// with the terms of the Adobe license agreement accompanying it. If you have received
// this file from a source other than Adobe, then your use, modification, or
// distribution of it requires the prior written permission of Adobe.
//
// DESCRIPTION: JavaScript glue code for XML Rules Processing
//
//========================================================================================
//app.scriptPreferences.version = 6.0;
function ruleProcessorObject(ruleSet, ruleProcessor) {
this.ruleSet = ruleSet;
this.ruleProcessor = ruleProcessor;
}
function __makeRuleProcessor(ruleSet, prefixMappingTable){
// Get the condition paths of all the rules.
var pathArray = new Array();
for (i=0; i<ruleSet.length; i++)
{
pathArray.push(ruleSet[i].xpath);
}
// the following call can throw an exception, in which case
// no rules are processed
try{
var ruleProcessor = app.xmlRuleProcessors.add(pathArray, prefixMappingTable);
}
catch(e){
throw e;
}
var rProcessor = new ruleProcessorObject(ruleSet, ruleProcessor);
return rProcessor;
}
function __deleteRuleProcessor(rProcessor) {
// remove the XMLRuleProcessor object
rProcessor.ruleProcessor.remove();
// delete the object properties
delete rProcessor.ruleProcessor;
delete rProcessor.ruleSet;
// delete the object itself
delete rProcessor;
}
function __processRuleSet (root, ruleSet, prefixMappingTable)
{
var mainRProcessor = __makeRuleProcessor(ruleSet, prefixMappingTable);
// if __processTree() fails with an exception,
// delete ruleProcessor and throw e
try {
__processTree(root, mainRProcessor);
__deleteRuleProcessor(mainRProcessor);
} catch (e) {
__deleteRuleProcessor(mainRProcessor);
throw e;
}
}
function __processTree (root, rProcessor)
{
var ruleProcessor = rProcessor.ruleProcessor;
try
{
var matchData = ruleProcessor.startProcessingRuleSet(root);
__processMatchData(matchData, rProcessor);
ruleProcessor.endProcessingRuleSet();
}
catch (e)
{
// no longer deleting ruleProcessor within __processTree
// deletion occurs either in __processRuleSet, or external
// to glue code.
ruleProcessor.endProcessingRuleSet();
throw e;
}
}
function __processChildren(rProcessor)
{
var ruleProcessor = rProcessor.ruleProcessor;
try
{
var matchData = ruleProcessor.startProcessingSubtree();
__processMatchData(matchData, rProcessor);
}
catch (e)
{
ruleProcessor.halt();
throw e;
}
}
function __processMatchData(matchData, rProcessor)
{
var ruleProcessor = rProcessor.ruleProcessor;
var ruleSet = rProcessor.ruleSet;
while (matchData != undefined)
{
var element = matchData.element;
var matchRules = matchData.matchRules;
var applyMatchedRules = true;
// apply the action of the rule.
// Continue applying rules as long as the apply function returns false.
for (var i=0; i<matchRules.length && applyMatchedRules && !ruleProcessor.halted; i++)
{
applyMatchedRules = (false == ruleSet[matchRules[i]].apply(element, rProcessor));
}
matchData = ruleProcessor.findNextMatch();
}
}
function __skipChildren(rProcessor)
{
rProcessor.ruleProcessor.skipChildren();
}
function mCompare (el, array) // compare two arrays
{
while ( curEl = array.pop() )
if ( curEl == el)
return true;
return false;
}