2 Replies Latest reply on Mar 5, 2015 3:46 AM by chrlsdrwn12

    [CS6][JS]XML cross-ref to InDesign hyperlinks

    chrlsdrwn12

      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

        • 1. Re: [CS6][JS]XML cross-ref to InDesign hyperlinks
          Loic.Aigon Adobe Community Professional

          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

          http://www.ozalto.com

          • 2. Re: [CS6][JS]XML cross-ref to InDesign hyperlinks
            chrlsdrwn12 Level 1

            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[i]);

                    }

                    catch(e){}

                }          

            }

             

             

            function createLink(whatTag){

                for(var i=0;i<whatTag.length;i++)

                {

                    try

                    {

                        var myContent = whatTag[i].texts[0];

                        var myType = whatTag[i].xmlAttributes.itemByName("ref-type").value;

                        var myDest = whatTag[i].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[i].texts[0];

                        // get the id of the destination

                        var myAtt = whatTag[i].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){}

                }      

            }