0

I have xml in the following format:

   <WebsiteAsHyperLink><websitewithURL xlink:href="http://www.google.com"> Click to view website </websitewithURL></WebsiteAsHyperLink>

and I have the following script, when ran it converts hrefs into valid tags, but it does not work on xlinks

If I could get some help on how to alter it to look at all xlinks

main();

function main() {
   var doc = app.activeDocument;
   var elems = doc.xmlElements;
   var ruleSet = new Array (new Rule);
   __processRuleSet(elems[0], ruleSet);

   function Rule() {
      this.name = "Rule";
      this.xpath = "//websitewithURL [@href]";
      this.apply = function(elem, ruleProcessor) {
         var elemText = elem.texts[0];
         var linkURL = elem.xmlAttributes.itemByName("href").value;
         var linkSource = doc.hyperlinkTextSources.add(elemText);
         var linkDest = doc.hyperlinkURLDestinations.add(linkURL);
         doc.hyperlinks.add(linkSource, linkDest);
         return true;
      }
   }
}

2 Answers 2

1

XPath implementation in ExtendScript is far from complete. A lot of things and function that would work in a fully compliant XPath environment will fail.

Here is another approach that seems to do the work:

main();
function main() {
    var doc = app.activeDocument;
    var root = doc.xmlElements[0];
    var urls = root.evaluateXPathExpression ( "//websitewithURL[@*]" ), nUrl;
    var n = urls.length, xAttr, text;
    while ( n-- ) {
       nUrl = urls[n];
       xAttr = nUrl.xmlAttributes.item ( "xlink:href" );
       if ( xAttr.isValid ) {
            var linkSource = doc.hyperlinkTextSources.add(nUrl.xmlContent);
             var linkDest = doc.hyperlinkURLDestinations.add(xAttr.value);
             doc.hyperlinks.add(linkSource, linkDest);
        }
    }
}
0
this.xpath = "//websitewithURL [@xlink:href]"
1
  • I've tried that, throws an exception
    – lanes123
    Commented May 3, 2018 at 7:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.