1 Reply Latest reply on Aug 3, 2018 5:33 AM by Loic.Aigon

    XSL convert absolute file path to relative

    zeRafio Level 1

      Hi all,


      In a XML document, references to images are absolute like this example:

        <img href="V:\source\events\aerospace\images\Toulouse51.png"/>


      I would like to change them in relative paths:

        <img href="file://images/Toulouse51.png"/>


      Can someone help me to write the XSLT to make this conversion?

        • 1. Re: XSL convert absolute file path to relative
          Loic.Aigon Adobe Community Professional


          If it's all about pure XSLT, you may be luckier asking in StackOverflow unless you want to call that XSLT inside an InDesign script.

          Anyway, the only tricky part here in your case is that XSLT (Version 1.0) has no regexp function to capture the interesting part of the attribute. So you need to use recursion which is in XSLT always something scary.

          I can propose this approach but of course you will need to adjust to your needs  :


          <?xml version="1.0" encoding="UTF-8"?>
           <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
               <xsl:template match="/">
               <xsl:template match="img">
                   <xsl:variable name="fileName">
                       <xsl:call-template name="getFileName">
                           <xsl:with-param name="url">
                               <xsl:value-of select="@href"/>
                   <img href="file://images/{$fileName}"/>
               <xsl:template name="getFileName">
                   <xsl:param name="url"/>
                       <xsl:when test="contains($url, '\')">
                           <xsl:call-template name="getFileName">
                               <xsl:with-param name="url">
                                   <xsl:value-of select="substring-after($url, '\')"/>
                           <xsl:value-of select="$url"/>


          The idea is to isolate the file name by recursivingly splitting href attribute value as long as "\" character is found. Once that done, it's easy to create the new url…


          Hope that helps,



          Ozalto | Productivity Oriented - Loïc Aigon