Skip to main content
Inspiring
July 20, 2010
Answered

[CS4 JS] Create HyperLinks from XMLattributes values

  • July 20, 2010
  • 1 reply
  • 1554 views

Hi,

Is  it possible to make a script to create hyperlinks from imported XML text where there are two elements named "hyperso" as the source of a hyperlink and "hyperde" as the destination of a hyperlink. Each "hyperso" element has got a unique attribute value that is corresponding with de attribute value of element "hyperde". For example:

<hyperso id="1">Source</hyperso>
<hyperde id="1">Destination</hyperde>


Sjoerd

This topic has been closed for replies.
Correct answer

XML will not allow "same id with two times". please see the code below, i have changed source attribute to 'ref'.

var scriptTitle="Hypherlink";
var sourceElement='hyperso';
var sourceAttribute='ref';
var destinationElement='hyperde';
var destinationAttribute='id';
var requiredElements=new Array();
var definedElement="";
var myCharStyle;

aDoc=app.activeDocument;

app.doScript (File(app.filePath+"/Scripts/Xml Rules/glue code.jsx"));

getXMLElements();

if (requiredElements.length > 0)
{
    //Processing on citation elements
    for(var xEle=0; xEle < requiredElements.length; xEle++)
    {
        var citationElement=requiredElements[xEle];
        citationElement.texts[0].fillColor=aDoc.swatches.item('Blue');
        definedElement="";
        GetElement(citationElement.xmlAttributes.item(sourceAttribute).value);
        if (definedElement.markupTag.name == destinationElement)
        {
            var myHyperlink=aDoc.hyperlinks.add(aDoc.hyperlinkTextSources.add(citationElement.texts[0]),aDoc.hyperlinkTextDestinations.add(definedElement.texts[0]));   
            myHyperlink.name="Link "+(xEle+1);
            myHyperlink.visible=false;
        }
        else
        {
            alert(definedElement.markupTag.name);
        }
    }
}

function GetElement(receivedId)
{
    //Using XML rule set we get required elements
    var xmlRuleSetDefinition = new Array(new FindDefinitionElement("//*[@"+destinationAttribute+"=\'"+receivedId+"\']"));
    __processRuleSet(aDoc.xmlElements.item(0), xmlRuleSetDefinition);
}

function FindDefinitionElement(myDefinitionRule)
{
    this.name = "FindDefinitionElement";
    this.xpath = myDefinitionRule;
    this.apply = function(myElement, myRuleProcessor)
    {
        definedElement=myElement;
    }
}

function getXMLElements()
{
    //Using XML rule set we can get required elements
    var xmlRuleSetCitation = new Array(new FindCitationElement("//"+sourceElement+"[@"+sourceAttribute+"]", requiredElements));
    __processRuleSet(aDoc.xmlElements.item(0), xmlRuleSetCitation);
}

function FindCitationElement(myCitationRule, myarray)
{
    this.name = "FindCitationElement";
    this.xpath = myCitationRule;
    this.apply = function(myElement, myRuleProcessor)
    {
        myarray.push(myElement);
    }
}

Arivu...

1 reply

stoereeeAuthor
Inspiring
July 22, 2010

Nobody?

Correct answer
July 22, 2010

XML will not allow "same id with two times". please see the code below, i have changed source attribute to 'ref'.

var scriptTitle="Hypherlink";
var sourceElement='hyperso';
var sourceAttribute='ref';
var destinationElement='hyperde';
var destinationAttribute='id';
var requiredElements=new Array();
var definedElement="";
var myCharStyle;

aDoc=app.activeDocument;

app.doScript (File(app.filePath+"/Scripts/Xml Rules/glue code.jsx"));

getXMLElements();

if (requiredElements.length > 0)
{
    //Processing on citation elements
    for(var xEle=0; xEle < requiredElements.length; xEle++)
    {
        var citationElement=requiredElements[xEle];
        citationElement.texts[0].fillColor=aDoc.swatches.item('Blue');
        definedElement="";
        GetElement(citationElement.xmlAttributes.item(sourceAttribute).value);
        if (definedElement.markupTag.name == destinationElement)
        {
            var myHyperlink=aDoc.hyperlinks.add(aDoc.hyperlinkTextSources.add(citationElement.texts[0]),aDoc.hyperlinkTextDestinations.add(definedElement.texts[0]));   
            myHyperlink.name="Link "+(xEle+1);
            myHyperlink.visible=false;
        }
        else
        {
            alert(definedElement.markupTag.name);
        }
    }
}

function GetElement(receivedId)
{
    //Using XML rule set we get required elements
    var xmlRuleSetDefinition = new Array(new FindDefinitionElement("//*[@"+destinationAttribute+"=\'"+receivedId+"\']"));
    __processRuleSet(aDoc.xmlElements.item(0), xmlRuleSetDefinition);
}

function FindDefinitionElement(myDefinitionRule)
{
    this.name = "FindDefinitionElement";
    this.xpath = myDefinitionRule;
    this.apply = function(myElement, myRuleProcessor)
    {
        definedElement=myElement;
    }
}

function getXMLElements()
{
    //Using XML rule set we can get required elements
    var xmlRuleSetCitation = new Array(new FindCitationElement("//"+sourceElement+"[@"+sourceAttribute+"]", requiredElements));
    __processRuleSet(aDoc.xmlElements.item(0), xmlRuleSetCitation);
}

function FindCitationElement(myCitationRule, myarray)
{
    this.name = "FindCitationElement";
    this.xpath = myCitationRule;
    this.apply = function(myElement, myRuleProcessor)
    {
        myarray.push(myElement);
    }
}

Arivu...

stoereeeAuthor
Inspiring
July 22, 2010

Thanks, this is very helpfull!!