[CS6][JS]XML cross-ref to InDesign hyperlinks
Copy link to clipboard
Copied
Hi guys,
I'm trying to import xml and create hyperlinks with xml cross-references for tables, figures, and references. So i'm planning to create a script to select the certain xml elements and create hyperlink destination based on id attributes value then search for all xml elements citation in the body pertaining to those elements then create new hyperlink and link to them. Can you point me to the right direction please.
TIA.
-CharlesD
Copy link to clipboard
Copied
Hi
I am not sure I figure it all but from what I have understood you have two solutions. If the xpaths never change (you always work with the same nodes), you can use XML rules which are introduced in the scripting guide and also I think in the SDK (XMLRulesApplyFormatting.jsx). If you want something more dynamic, you may prefer using the XMLElement.evaluateXPathExpression. These two methods will target the elements matching teh xpath.
Then it's up to you to reach the inner text/objects elements and sets the hyperlinks.
HTH
Loic
Copy link to clipboard
Copied
Hi,
Thanks for the reply. I started something and I hope it might be of interest to somebody:
var myDocument = app.documents.item(0);
var myRoot = myDocument.xmlElements.item(0);
var myRefs = myRoot.evaluateXPathExpression("//ref"); //references element
var myFigs = myRoot.evaluateXPathExpression("//fig"); //figures element
var myXrefs = myRoot.evaluateXPathExpression("//xref"); //citations element
var myTagsToLink = [myRefs, myFigs];
createAllDest(myTagsToLink);
createLink(myXrefs);
//Loop through all tags that need to be referenced and call the
//function that will create the new hyperlink destination
function createAllDest(allTags){
for(var i=0;i<allTags.length;i++)
{
try
{
createDest(allTags);
}
catch(e){}
}
}
function createLink(whatTag){
for(var i=0;i<whatTag.length;i++)
{
try
{
var myContent = whatTag.texts[0];
var myType = whatTag.xmlAttributes.itemByName("ref-type").value;
var myDest = whatTag.xmlAttributes.itemByName("rid").value;
myHyperlinkURL = myDocument.hyperlinkTextDestinations.itemByName(myDest);
myHyperlinkSource = myDocument.hyperlinkTextSources.add(myContent);
myDocument.hyperlinks.add(myHyperlinkSource,myHyperlinkURL,{name: myDest});
//alert(myDest);
if (myType == "bibr"){
//alert("This is reference");
myContent.appliedCharacterStyle = "ref-citation";
}
else if (myType == "fig"){
//alert("This is figure");
myContent.appliedCharacterStyle = "fig-citation";
}
}
catch(e){}
}
}
// Create new hyperlink destination function
function createDest(whatTag){
for(var i=0;i<whatTag.length;i++)
{
try
{
// select the text of the element
var myContent = whatTag.texts[0];
// get the id of the destination
var myAtt = whatTag.xmlAttributes.itemByName("id").value;
// add the id of the destination to the list of the document's text hyperlink destination
var anchor = app.documents[0].hyperlinkTextDestinations.add(myContent);
// set the text's hyperlink destination's name to that of the id
anchor.name = myAtt;
//alert(myContent);
}
catch(e){}
}
}

