Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to convert anchor tag with href attribute to display only text with hyperlink

Explorer ,
May 22, 2020 May 22, 2020

Hi All,

 

In my current application I will have to parse the xml file and show its contents in different frames. In my xml file there is anchor element with href tag and I want to display only the text of anchor while href attribute should be converted into hyerplink for ex. my xml file has following element 

 

<Text>Doc. <a href="/doc/source/docs/A_63_435-E.pdf" target="_blank">A/63/435</a>; C.N.869.2009.TREATIES-34 of 11 December 2009 (Rectification of the original of the Protocol (French authentic text) and transmission of the Procès-verbal).</Text> and inside indesign generated pdf it should look like 

 

ashishktrig_0-1590145517681.png

Kindly suggest how can I acheive it and further if such anchor and href attributes are repeated inside the xml file how can I replace all such elements like  above

 

Regards,

 

TOPICS
Scripting , SDK
1.7K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Advocate , May 28, 2020 May 28, 2020

Try this one for that:

//////////////////////////////////////////////////////////////////////////////////////////////////////

app.findTextPreferences = NothingEnum.nothing;
app.changeTextPreferences = NothingEnum.nothing;
app.findTextPreferences.findWhat = "<a";
app.changeTextPreferences.changeTo = "@";
app.documents[0].changeText();
app.documents[0].recompose();
/////////////////////////////////
app.findTextPreferences = NothingEnum.nothing;
app.changeTextPreferences = NothingEnum.nothing;
app.findTextP

...
Translate
Advocate ,
May 22, 2020 May 22, 2020

Hi,

Try this code for adding external URL to text:

/////////////////////////////////////////////////////////////////////////////////////////

var myDoc = app.documents[0];
var xPath = ["//a[@href]"];
var root = myDoc.xmlElements[0];
var node = null;
try {
    var proc = app.xmlRuleProcessors.add(xPath);
    var match = proc.startProcessingRuleSet(root);
    while (match != undefined) {
        node = match.element;
        match = proc.findNextMatch();
        if (node != null && node != undefined) {
            if (node.xmlAttributes.item('href').isValid){
                var sourceText = myDoc.hyperlinkTextSources.add(node.texts[0]);
                var destinationURL = myDoc.hyperlinkURLDestinations.add(node.xmlAttributes.item('href').value.toString());
                myDoc.hyperlinks.add(sourceText, destinationURL);
                }
            }
        }
    }

catch (ex) {}
finally {
    proc.endProcessingRuleSet();
    proc.remove();
    }

/////////////////////////////////////////////////////////////////////////////////////////

 

Best

Sunil

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 25, 2020 May 25, 2020

Hi Sunil,

 

Thank you so much for the xml rules and other xml parsing way suggested by you, but the solution is not working for me. Basically in my application I have to parse the entire xml file and assign its content to different frames seperately and each frame data has different formatting requirement, so what I am looking here is something which works at the frame level, like parsed xml conetents are assigned to below two frames.

ashishktrig_0-1590475289120.png

Both are two different frames and for 2nd frame I want that anchor and ref tag should be replcaed as request. If there are multile frames with similar anchor and href tag then it should also be work as requested in my query

 

Attaching sample xml tags of xml file for your referencce

ashishktrig_1-1590475900774.png

 

 

Regards,

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
May 28, 2020 May 28, 2020

Try this one for that:

//////////////////////////////////////////////////////////////////////////////////////////////////////

app.findTextPreferences = NothingEnum.nothing;
app.changeTextPreferences = NothingEnum.nothing;
app.findTextPreferences.findWhat = "<a";
app.changeTextPreferences.changeTo = "@";
app.documents[0].changeText();
app.documents[0].recompose();
/////////////////////////////////
app.findTextPreferences = NothingEnum.nothing;
app.changeTextPreferences = NothingEnum.nothing;
app.findTextPreferences.findWhat = "</a>";
app.changeTextPreferences.changeTo = "@";
app.documents[0].changeText();
app.documents[0].recompose();
/////////////////////////////////
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.findWhat = "(?<=@)([^@]+)(?=@)";
var allFound = app.documents[0].findGrep();
for(var i = 0; i < allFound.length; i){
    app.select(allFound[i].texts[0]);
    var allContent = allFound[i].texts[0].contents;
    app.selection[0].contents = allContent.split(">")[1];
    app.selection[0].insertionPoints[0].parentStory.characters[app.selection[0].insertionPoints[0].index-1].remove();
    app.selection[0].insertionPoints[0].parentStory.characters[app.selection[0].insertionPoints[-1].index].remove();
    var sourceText = app.documents[0].hyperlinkTextSources.add(app.selection[0].texts[0]);
    var destinationURL = app.documents[0].hyperlinkURLDestinations.add(allContent.split(">")[0].split('”')[1]);
    app.documents[0].hyperlinks.add(sourceText, destinationURL);
    app.documents[0].recompose();
    var allFound = app.documents[0].findGrep();
    }

//////////////////////////////////////////////////////////////////////////////////////////////////////

Best

Sunil

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 29, 2020 May 29, 2020

Hi Sunil,

 

Thank you so much for your efforts!!. I will check and let you know if it works for me..

 

 

 

Regards,

Ashish

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 29, 2020 May 29, 2020
LATEST

Hi Sunil,

 

Thanks you so much for your help. The solution provided by you is working perfectly fine and I am really greatful to you for the time and efforts you have taken to provide solution to me.

 

 

Regards,

Ashish

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines