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

          Hi,

          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"
               xmlns:xs="http://www.w3.org/2001/XMLSchema"
               exclude-result-prefixes="xs"
               version="2.0">
               <xsl:template match="/">
                   <xsl:apply-templates/>
               </xsl:template>
               
               <xsl:template match="img">
                   <xsl:variable name="fileName">
                       <xsl:call-template name="getFileName">
                           <xsl:with-param name="url">
                               <xsl:value-of select="@href"/>
                           </xsl:with-param>
                       </xsl:call-template>
                   </xsl:variable>
                   <img href="file://images/{$fileName}"/>
               </xsl:template>
               
               <xsl:template name="getFileName">
                   <xsl:param name="url"/>
                   <xsl:choose>
                       <xsl:when test="contains($url, '\')">
                           <xsl:call-template name="getFileName">
                               <xsl:with-param name="url">
                                   <xsl:value-of select="substring-after($url, '\')"/>
                               </xsl:with-param>
                           </xsl:call-template>
                       </xsl:when>
                       <xsl:otherwise>
                           <xsl:value-of select="$url"/>
                       </xsl:otherwise>
                   </xsl:choose>
                   
               </xsl:template>
           </xsl:stylesheet>
          
          

           

          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,

           

          Loic

          Ozalto | Productivity Oriented - Loïc Aigon