Re: [xsl] Determine file extension of path stored in variable

Subject: Re: [xsl] Determine file extension of path stored in variable
From: Mukul Gandhi <mukul_gandhi@xxxxxxxxx>
Date: Wed, 29 Dec 2004 04:48:32 -0800 (PST)
> I am not sure what should be done in the case where
> the path doesn't contain a '.' (or how to trap that
>condition) 
I suggest the following XSL to trap this condition..

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
  
<xsl:output method="text" />

<xsl:variable name="lcpath"
select="'/images/dir.path/file.png'" />

<xsl:template match="/">
  <xsl:variable name="ext">
    <xsl:call-template name="get-file-extension">
      <xsl:with-param name="path" select="$lcpath" />
    </xsl:call-template>
 </xsl:variable>
 <xsl:value-of select="$ext" />
</xsl:template>

<xsl:template name="get-file-extension">
   <xsl:param name="path" />
   <xsl:choose>
     <xsl:when test="contains( $path, '/' )">
       <xsl:call-template name="get-file-extension">
         <xsl:with-param name="path"
select="substring-after($path, '/')" />
       </xsl:call-template>
     </xsl:when>
     <xsl:when test="contains( $path, '.' )">
       <xsl:call-template name="TEMP">
         <xsl:with-param name="x"
select="substring-after($path, '.')" />
       </xsl:call-template>
     </xsl:when>
     <xsl:otherwise>
       <xsl:text>No extension</xsl:text>
     </xsl:otherwise>
   </xsl:choose>
</xsl:template>

<xsl:template name="TEMP">
   <xsl:param name="x" />

   <xsl:choose>
     <xsl:when test="contains($x, '.')">
       <xsl:call-template name="TEMP">
         <xsl:with-param name="x"
select="substring-after($x, '.')" />
       </xsl:call-template>
     </xsl:when>
     <xsl:otherwise>
       <xsl:value-of select="$x" />
     </xsl:otherwise>
   </xsl:choose>
</xsl:template>

</xsl:stylesheet>

With the above XSL, if path contains a . after the
last / character, the stylesheet outputs just as
before (it prints the extension name). But if the path
is something like - <xsl:variable name="lcpath"
select="'/images/dir.path/filepng'" /> , the XSL
prints "No extension".. 

You can choose to write <xsl:text></xsl:text> instead
of <xsl:text>No extension</xsl:text> .. (to output
nothing, if there is no extension)..

>Is there a better way to code the two 
> xsl:when blocks below that basically do the same
> thing but with  different characters?
The way you have modified the code, looks fine to me!

Regards,
Mukul


	
		
__________________________________ 
Do you Yahoo!? 
Yahoo! Mail - You care about security. So do we. 
http://promotions.yahoo.com/new_mail

Current Thread